Elgg  Version 4.3
/root/Elgg/engine/lib/views.php

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.

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$actionThe name of the action. An action name does not include the leading "action/". For example, "login" is an action name.
array$form_vars$vars environment passed to the "input/form" view
  • 'ajax' bool If true, the form will be submitted with an Ajax request
array$body_vars$vars environment passed to the "forms/$action" view
Returns
string The complete form
<?php
function elgg_set_viewtype($viewtype = '') {
return _elgg_services()->views->setViewtype($viewtype);
}
function elgg_get_viewtype() {
return _elgg_services()->views->getViewtype();
}
_elgg_services()->views->registerViewtypeFallback($viewtype);
}
function elgg_register_ajax_view($view): void {
_elgg_services()->ajax->registerView($view);
}
_elgg_services()->ajax->unregisterView($view);
}
_elgg_services()->views->setViewDir($view, $location, $viewtype);
}
function elgg_view_exists($view, $viewtype = '', $recurse = true) {
return _elgg_services()->views->viewExists($view, $viewtype, $recurse);
}
function elgg_view($view, $vars = [], $viewtype = '') {
if (func_num_args() == 5) {
elgg_log(__FUNCTION__ . ' now has only 3 arguments. Update your usage.', 'ERROR');
$viewtype = func_get_arg(4);
}
return _elgg_services()->views->renderView($view, $vars, $viewtype);
}
function elgg_extend_view($view, $view_extension, $priority = 501) {
_elgg_services()->views->extendView($view, $view_extension, $priority);
}
function elgg_unextend_view($view, $view_extension) {
return _elgg_services()->views->unextendView($view, $view_extension);
}
$list = _elgg_services()->views->getViewList($view);
unset($list[500]);
return $list;
}
function elgg_view_page($title, $body, $page_shell = 'default', $vars = []) {
if (elgg_is_xhr() && get_input('_elgg_ajax_list')) {
// requested by ajaxed pagination
return is_array($body) ? elgg_extract('content', $body) : $body;
}
if (is_array($body)) {
$vars['entity'] = elgg_extract('entity', $body, elgg_extract('entity', $vars));
$body['title'] = elgg_extract('title', $body, $title);
$body = elgg_view_layout('default', $body);
}
$timer = _elgg_services()->timer;
if (!$timer->hasEnded(['build page'])) {
$timer->end(['build page']);
}
$timer->begin([__FUNCTION__]);
$params = [];
$params['identifier'] = _elgg_services()->request->getFirstUrlSegment();
$params['segments'] = _elgg_services()->request->getUrlSegments();
array_shift($params['segments']);
$page_shell = elgg_trigger_plugin_hook('shell', 'page', $params, $page_shell);
$system_messages = _elgg_services()->system_messages;
$messages = null;
if ($system_messages->count()) {
$messages = $system_messages->dumpRegister();
if (isset($messages['error'])) {
// always make sure error is the first type
$errors = [
'error' => $messages['error']
];
unset($messages['error']);
$messages = array_merge($errors, $messages);
}
}
$vars['title'] = $title;
$vars['body'] = $body;
$vars['sysmessages'] = $messages;
$vars['page_shell'] = $page_shell;
$vars['head'] = elgg_trigger_plugin_hook('head', 'page', $vars, ['metas' => [], 'links' => []]);
$vars = elgg_trigger_plugin_hook('output:before', 'page', null, $vars);
$output = elgg_view("page/$page_shell", $vars);
// Allow plugins to modify the output
$timer->end([__FUNCTION__]);
return $output;
}
function elgg_view_resource($name, array $vars = []) {
$view = "resources/$name";
return _elgg_services()->views->renderView($view, $vars);
}
if (elgg_get_viewtype() !== 'default' && elgg_view_exists($view, 'default')) {
return _elgg_services()->views->renderView($view, $vars, 'default');
}
_elgg_services()->logger->error("The view $view is missing.");
// only works for default viewtype
throw new PageNotFoundException();
}
function elgg_view_layout($layout_name, $vars = []) {
$timer = _elgg_services()->timer;
if (!$timer->hasEnded(['build page'])) {
$timer->end(['build page']);
}
$timer->begin([__FUNCTION__]);
$vars['identifier'] = _elgg_services()->request->getFirstUrlSegment();
$vars['segments'] = _elgg_services()->request->getUrlSegments();
array_shift($vars['segments']);
$layout_name = elgg_trigger_plugin_hook('layout', 'page', $vars, $layout_name);
$vars['layout'] = $layout_name;
$layout_views = [
"page/layouts/$layout_name",
"page/layouts/default",
];
$output = '';
foreach ($layout_views as $layout_view) {
if (elgg_view_exists($layout_view)) {
$output = elgg_view($layout_view, $vars);
break;
}
}
$timer->end([__FUNCTION__]);
return $output;
}
function elgg_view_menu($menu, array $vars = []) {
$menu_view = elgg_extract('menu_view', $vars);
unset($vars['menu_view']);
if (is_string($menu)) {
$menu = _elgg_services()->menus->getMenu($menu, $vars);
} elseif ($menu instanceof UnpreparedMenu) {
$menu = _elgg_services()->menus->prepareMenu($menu);
}
if (!$menu instanceof Menu) {
throw new ElggInvalidArgumentException('$menu must be a menu name, a Menu, or UnpreparedMenu');
}
$name = $menu->getName();
$params = $menu->getParams();
$views = [
"navigation/menu/$name",
'navigation/menu/default',
];
foreach ($views as $view) {
if (elgg_view_exists($view)) {
return elgg_view($view, $params);
}
}
}
function elgg_view_entity(\ElggEntity $entity, array $vars = []) {
'full_view' => true,
];
$vars = array_merge($defaults, $vars);
if (elgg_extract('register_rss_link', $vars, elgg_extract('full_view', $vars))) {
}
$vars['entity'] = $entity;
$entity_type = $entity->getType();
$entity_subtype = $entity->getSubtype();
$entity_views = [
elgg_extract('item_view', $vars, ''),
"$entity_type/$entity_subtype",
"$entity_type/default",
];
$contents = '';
foreach ($entity_views as $view) {
if (elgg_view_exists($view)) {
break;
}
}
return $contents;
}
function elgg_view_entity_icon(\ElggEntity $entity, $size = 'medium', $vars = []) {
$vars['entity'] = $entity;
$vars['size'] = $size;
$entity_type = $entity->getType();
$subtype = $entity->getSubtype();
$contents = '';
if (elgg_view_exists("icon/$entity_type/$subtype")) {
$contents = elgg_view("icon/$entity_type/$subtype", $vars);
}
if (empty($contents) && elgg_view_exists("icon/$entity_type/default")) {
$contents = elgg_view("icon/$entity_type/default", $vars);
}
if (empty($contents)) {
$contents = elgg_view("icon/default", $vars);
}
return $contents;
}
'full_view' => true,
];
$vars = array_merge($defaults, $vars);
$vars['annotation'] = $annotation;
$name = $annotation->name;
if (empty($name)) {
return false;
}
$annotation_views = [
elgg_extract('item_view', $vars, ''),
"annotation/$name",
"annotation/default",
];
$contents = '';
foreach ($annotation_views as $view) {
if (elgg_view_exists($view)) {
break;
}
}
return $contents;
}
function elgg_view_entity_list($entities, array $vars = []) {
$offset = (int) get_input('offset', 0);
// list type can be passed as request parameter
$list_type = get_input('list_type', 'list');
'items' => $entities,
'list_class' => 'elgg-list-entity',
'full_view' => true,
'pagination' => true,
'list_type' => $list_type,
'offset' => $offset,
'limit' => null,
];
$vars = array_merge($defaults, $vars);
if (!$vars["limit"] && !$vars["offset"]) {
// no need for pagination if listing is unlimited
$vars["pagination"] = false;
}
$view = "page/components/{$vars['list_type']}";
$view = 'page/components/list';
}
return elgg_view($view, $vars);
}
function elgg_view_annotation_list($annotations, array $vars = []) {
// list type can be passed as request parameter
$list_type = get_input('list_type', 'list');
'items' => $annotations,
'offset' => null,
'limit' => null,
'list_class' => 'elgg-list-annotation',
'full_view' => true,
'list_type' => $list_type,
'offset_key' => 'annoff',
];
$vars = array_merge($defaults, $vars);
if (!$vars["limit"] && !$vars["offset"]) {
// no need for pagination if listing is unlimited
$vars["pagination"] = false;
}
$view = "page/components/{$vars['list_type']}";
$view = 'page/components/list';
}
return elgg_view($view, $vars);
}
function elgg_view_relationship_list($relationships, array $vars = []) {
// list type can be passed as request parameter
$list_type = get_input('list_type', 'list');
'items' => $relationships,
'offset' => null,
'limit' => null,
'list_class' => 'elgg-list-relationship',
'full_view' => false,
'list_type' => $list_type,
'offset_key' => 'reloff',
];
$vars = array_merge($defaults, $vars);
if (!$vars['limit'] && !$vars['offset']) {
// no need for pagination if listing is unlimited
$vars['pagination'] = false;
}
$view = "page/components/{$vars['list_type']}";
$view = 'page/components/list';
}
return elgg_view($view, $vars);
}
'full_view' => true,
];
$vars = array_merge($defaults, $vars);
$vars['relationship'] = $relationship;
$name = $relationship->relationship;
if (empty($name)) {
return false;
}
$relationship_views = [
elgg_extract('item_view', $vars, ''),
"relationship/$name",
"relationship/default",
];
$contents = '';
foreach ($relationship_views as $view) {
if (elgg_view_exists($view)) {
break;
}
}
return $contents;
}
function elgg_view_title($title, array $vars = []) {
$vars['title'] = $title;
return elgg_view('page/elements/title', $vars);
}
$view = 'output/friendlytime';
$vars = [
'time' => $time,
'time_updated' => $time_updated,
];
$viewtype = elgg_view_exists($view) ? '' : 'default';
}
function elgg_view_comments($entity, $add_comment = true, array $vars = []) {
if (!$entity instanceof \ElggEntity) {
return false;
}
if (!$entity->hasCapability('commentable')) {
return false;
}
$vars['entity'] = $entity;
$vars['show_add_form'] = $add_comment;
$vars['class'] = elgg_extract('class', $vars, "{$entity->getSubtype()}-comments");
$output = elgg_trigger_plugin_hook('comments', $entity->getType(), $vars, false);
if ($output !== false) {
return $output;
}
return elgg_view('page/elements/comments', $vars);
}
$vars['image'] = $image;
$vars['body'] = $body;
return elgg_view('page/components/image_block', $vars);
}
function elgg_view_module($type, $title, $body, array $vars = []) {
$vars['type'] = $type;
$vars['title'] = $title;
$vars['body'] = $body;
return elgg_view('page/components/module', $vars);
}
function elgg_view_message($type, $body, array $vars = []) {
$vars['type'] = $type;
$vars['body'] = $body;
return elgg_view('page/components/message', $vars);
}
function elgg_view_river_item($item, array $vars = []) {
if (!($item instanceof \ElggRiverItem)) {
return '';
}
// checking default viewtype since some viewtypes do not have unique views per item (rss)
$view = $item->getView();
$subject = $item->getSubjectEntity();
$object = $item->getObjectEntity();
if (!$subject || !$object) {
// subject is disabled or subject/object deleted
return '';
}
if (elgg_extract('register_rss_link', $vars)) {
}
$vars['item'] = $item;
// create river view logic
$type = $object->getType();
$subtype = $object->getSubtype();
$action = $item->action_type;
$river_views = [
elgg_extract('item_view', $vars, ''),
'river/item', // important for other viewtypes, e.g. "rss"
"river/{$type}/{$subtype}/{$action}",
"river/{$type}/{$subtype}/default",
"river/{$type}/{$action}",
"river/{$type}/default",
'river/elements/layout',
];
$contents = '';
foreach ($river_views as $view) {
if (elgg_view_exists($view)) {
break;
}
}
return $contents;
}
return _elgg_services()->forms->render($action, $form_vars, $body_vars);
}
function elgg_set_form_footer($footer = '') {
return _elgg_services()->forms->setFooter($footer);
}
return _elgg_services()->forms->getFooter();
}
function _elgg_split_vars(array $vars = [], array $prefixes = null) {
if (!isset($prefixes)) {
$prefixes = ['#'];
}
$return = [];
$default_section = ''; // something weird with PHP 8.1 compatibility
foreach ($vars as $key => $value) {
foreach ($prefixes as $prefix) {
if (substr($key, 0, 1) === $prefix) {
$key = substr($key, 1);
$return[$prefix][$key] = $value;
break;
} else {
$return[$default_section][$key] = $value;
}
}
}
return $return;
}
function elgg_view_field(array $params = []) {
if (!empty($params['#html'])) {
return $params['#html'];
}
if (empty($params['#type'])) {
_elgg_services()->logger->error(__FUNCTION__ . '(): $params["#type"] is required.');
return '';
}
$input_type = $params['#type'];
if (!elgg_view_exists("input/$input_type")) {
return '';
}
$hidden_types = ['hidden', 'securitytoken'];
if (in_array($input_type, $hidden_types)) {
return elgg_view("input/$input_type", $params['']);
}
if (!$id) {
$id = "elgg-field-" . base_convert(mt_rand(), 10, 36);
$params['id'] = $id;
}
$make_special_checkbox_label = false;
if ($input_type == 'checkbox' && (isset($params['label']) || isset($params['#label']))) {
if (isset($params['#label']) && isset($params['label'])) {
$params['label_tag'] = 'div';
} else {
$params['#label'] = $label;
unset($params['label']);
// Single checkbox input view gets special treatment
// We don't want the field label to appear a checkbox without a label
$make_special_checkbox_label = true;
}
}
// Need to set defaults to prevent input keys with same name ending up in element vars if not provided
];
$params = array_merge($defaults, $params);
// first pass non-hash keys into both
$split_params = _elgg_split_vars($params);
// $vars passed to input/$input_name
$input_vars = $split_params[''];
// $vars passed to label, help and field wrapper views
$element_vars = array_merge($split_params[''], $split_params['#']);
// field input view needs this
$input_vars['input_type'] = $input_type;
// field views get more data
$element_vars['input_type'] = $input_type;
// wrap if present
$element_vars['label'] = elgg_view('elements/forms/label', $element_vars);
$element_vars['help'] = elgg_view('elements/forms/help', $element_vars);
if ($make_special_checkbox_label) {
$input_vars['label'] = $element_vars['label'];
$input_vars['label_tag'] = 'div';
unset($element_vars['label']);
}
$element_vars['input'] = elgg_view("elements/forms/input", $input_vars);
return elgg_view('elements/forms/field', $element_vars);
}
function elgg_view_tagcloud(array $options = []) {
$type = $subtype = '';
if (isset($options['type'])) {
$type = $options['type'];
}
if (isset($options['subtype'])) {
$subtype = $options['subtype'];
}
$tag_data = elgg_get_tags($options);
return elgg_view("output/tagcloud", [
'value' => $tag_data,
'type' => $type,
'subtype' => $subtype,
]);
}
function elgg_view_list_item($item, array $vars = []) {
if ($item instanceof \ElggEntity) {
} else if ($item instanceof \ElggAnnotation) {
} else if ($item instanceof \ElggRiverItem) {
} else if ($item instanceof ElggRelationship) {
}
$view = elgg_extract('item_view', $vars);
$vars['item'] = $item;
return elgg_view($view, $vars);
}
return '';
}
function elgg_view_icon($name, $vars = []) {
if (empty($vars)) {
$vars = [];
}
if (is_string($vars)) {
$vars = ['class' => $vars];
}
if (!is_array($vars)) {
throw new ElggInvalidArgumentException('$vars needs to be a string or an array');
}
$vars['class'] = elgg_extract_class($vars, "elgg-icon-$name");
return elgg_view('output/icon', $vars);
}
_elgg_services()->config->_elgg_autofeed = true;
}
_elgg_services()->config->_elgg_autofeed = false;
}
function _elgg_has_rss_link() {
if (_elgg_services()->config->disable_rss) {
return false;
}
return (bool) _elgg_services()->config->_elgg_autofeed;
}
function elgg_views_boot() {
_elgg_services()->viewCacher->registerCoreViews();
// jQuery and UI must come before require. See #9024
elgg_load_external_file('js', 'jquery');
elgg_extend_view('require.js', 'elgg/require_config.js', 100);
elgg_register_external_file('js', 'require', elgg_get_simplecache_url('require.js'));
elgg_load_external_file('js', 'require');
elgg_load_external_file('js', 'elgg');
elgg_register_external_file('css', 'font-awesome', elgg_get_simplecache_url('font-awesome/css/all.min.css'));
elgg_load_external_file('css', 'font-awesome');
elgg_define_js('cropperjs', [
'src' => elgg_get_simplecache_url('cropperjs/cropper.min.js'),
]);
elgg_define_js('jquery-cropper/jquery-cropper', [
'src' => elgg_get_simplecache_url('jquery-cropper/jquery-cropper.min.js'),
]);
elgg_extend_view('initialize_elgg.js', 'elgg/prevent_clicks.js', 1);
elgg_extend_view('elgg.css', 'lightbox/elgg-colorbox-theme/colorbox.css');
elgg_extend_view('elgg.css', 'entity/edit/icon/crop.css');
elgg_define_js('jquery.ui.autocomplete.html', [
'deps' => ['jquery-ui/widgets/autocomplete'],
]);
elgg_register_simplecache_view('elgg/touch_punch.js');
elgg_define_js('jquery-ui/widgets/sortable', [
'deps' => ['elgg/touch_punch'],
]);
elgg_register_ajax_view('languages.js');
}
$message_delay = (int) elgg_get_config('message_delay');
if ($message_delay < 1) {
$message_delay = 6;
}
return [
'elgg.data' => (object) elgg_trigger_plugin_hook('elgg.data', 'site', null, []),
'elgg.release' => elgg_get_release(),
'elgg.config.wwwroot' => elgg_get_site_url(),
'elgg.config.message_delay' => $message_delay * 1000,
// refresh token 3 times during its lifetime (in microseconds 1000 * 1/3)
'elgg.security.interval' => (int) _elgg_services()->csrf->getActionTokenTimeout() * 333,
'elgg.config.language' => _elgg_services()->config->language ?: 'en',
];
}
$data = elgg_trigger_plugin_hook('elgg.data', 'page', null, []);
if (!is_array($data)) {
elgg_log('"elgg.data" plugin hook handlers must return an array. Returned ' . gettype($data) . '.', 'ERROR');
$data = [];
}
$elgg = [
'config' => [
'lastcache' => (int) _elgg_services()->config->lastcache,
'viewtype' => elgg_get_viewtype(),
'simplecache_enabled' => (int) elgg_is_simplecache_enabled(),
'current_language' => elgg_get_current_language(),
],
'security' => [
'token' => [
'__elgg_ts' => $ts = _elgg_services()->csrf->getCurrentTime()->getTimestamp(),
'__elgg_token' => _elgg_services()->csrf->generateActionToken($ts),
],
],
'session' => [
'user' => null,
'token' => _elgg_services()->session->get('__elgg_session'),
],
'_data' => (object) $data,
];
if ($page_owner instanceof ElggEntity) {
$elgg['page_owner'] = $page_owner->toObject(); // @todo remove in Elgg 5.0
}
if ($user instanceof ElggUser) {
$user_object = $user->toObject();
$user_object->admin = $user->isAdmin();
$elgg['session']['user'] = $user_object; // @todo remove in Elgg 5.0
$elgg['user'] = (array) $user_object;
}
return $elgg;
}
$current_view_type = null;
if ($viewtype) {
$current_view_type = elgg_get_viewtype();
}
$ret = elgg_view($view, $vars);
if (isset($current_view_type)) {
elgg_set_viewtype($current_view_type);
}
return $ret;
}
function elgg_view_url(string $href, string $text = null, array $options = []): string {
$options['is_trusted'] = elgg_extract('is_trusted', $options, true);
$options['href'] = $href;
$options['text'] = $text;
return elgg_view('output/url', $options);
}
function elgg_view_entity_url(\ElggEntity $entity, array $options = []): string {
return elgg_view_url($entity->getURL(), $entity->getDisplayName(), $options);
}