Convenience function for generating a form from a view in a standard location.This function assumes that the body of the form is located at "forms/$action" and sets the action by default to "action/$action". Automatically wraps the forms/$action view with a <form> tag and inserts the anti-csrf security tokens.
@tip This automatically appends elgg-form-action-name to the form's class. It replaces any slashes with dashes (blog/save becomes elgg-form-blog-save)
echo elgg_view_form('login');
This would assume a "login" form body to be at "forms/login" and would set the action of the form to "http://yoursite.com/action/login".
If elgg_view('forms/login') is: <input type="text" name="username" > <input type="password" name="password" >
Then elgg_view_form('login') generates: <form action="http://yoursite.com/action/login" method="post"> ...security tokens... <input type="text" name="username" > <input type="password" name="password" > </form>
- Parameters
-
string | $action | The name of the action. An action name does not include the leading "action/". For example, "login" is an action name. |
array | $form_vars | $vars passed to the "input/form" view
- 'ajax' bool If true, the form will be submitted with an ajax request
|
array | $body_vars | $vars passed to the "forms/<action>" view |
- Returns
- string The complete form
<?php
use
Elgg\Traits\Loggable;
class FormsService {
protected bool $rendering = false;
public function __construct(
protected ViewsService
$views,
protected EventsService $events,
protected ESMService $esm
) {
}
'method' => 'post',
'ajax' => false,
'sticky_enabled' => false,
'sticky_ignored_fields' => [],
];
'elgg-form-' . preg_replace(
'/[^a-z0-9]/i',
'-',
$action)],
);
}
$this->esm->import('input/form-ajax');
}
$this->rendering = true;
$this->footer = '';
$body = $this->views->renderView(
'elements/forms/body', [
]);
$body .= $this->views->renderView(
'elements/forms/footer', [
'footer' => $this->getFooter(),
]);
}
$this->rendering = false;
}
}
public function setFooter(
string $footer =
''): void {
throw new LogicException('Form footer can only be set and retrieved during form rendering, anywhere in elgg_view_form() call stack (e.g. form view, extending views, or view events)');
}
}
public function getFooter(): string {
throw new LogicException('Form footer can only be set and retrieved during form rendering, anywhere in elgg_view_form() call stack (e.g. form view, extending views, or view events)');
}
}
}
trait Loggable
Enables adding a logger.
if(!elgg_view_exists("plugins/{$plugin_id}/settings")) $form_vars
Exception that represents error in the program logic.
Keeps track of ES modules.
elgg_extract_class(array $array, array|string $existing=[], string $extract_key='class')
Extract class names from an array, optionally merging into a preexisting set.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
elgg_view(string $view, array $vars=[], string $viewtype='')
Return a parsed view.
elgg_generate_action_url(string $action, array $query=[], bool $add_csrf_tokens=true)
Generate an action URL.
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.