Elgg  Version master
entitypicker.php
Go to the documentation of this file.
1 <?php
21 $name = elgg_extract('name', $vars, 'entities', false);
22 
23 $guids = (array) elgg_extract('values', $vars, elgg_extract('value', $vars, []));
24 $limit = (int) elgg_extract('limit', $vars, 0);
25 
26 $save_as_array = (bool) elgg_extract('save_as_array', $vars, true);
27 if ($limit !== 1) {
28  $save_as_array = true;
29 }
30 
31 $params = elgg_extract('options', $vars, []);
32 
33 if (!empty($params)) {
34  ksort($params);
35 
36  // We sign custom parameters, so that plugins can validate
37  // that the request is unaltered, if needed
39  $params['mac'] = $mac->getToken();
40 }
41 
42 $params['view'] = 'json'; // force json viewtype
43 $params['save_as_array'] = $save_as_array;
44 
46  'class' => elgg_extract_class($vars, ['elgg-entity-picker']),
47  'data-limit' => $limit,
48  'data-name' => $name,
49  'data-match-on' => elgg_extract('match_on', $vars, 'entities', false),
50  'data-handler' => elgg_http_add_url_query_elements(elgg_extract('handler', $vars, 'livesearch'), $params),
51 ];
52 
54  'type' => 'text',
55  'class' => [
56  'elgg-input-entity-picker',
57  ],
58  'size' => 30,
59  'id' => elgg_extract('id', $vars),
60  'placeholder' => elgg_extract('placeholder', $vars),
61 ]);
62 
63 $picker .= elgg_view('input/hidden', ['name' => $name]);
64 $picker .= elgg_extract('picker_extras', $vars, '');
65 
66 $item_view = elgg_extract('item_view', $vars, 'input/autocomplete/item');
67 $items = '';
68 foreach ($guids as $guid) {
69  $entity = get_entity((int) $guid);
70  if (!$entity instanceof \ElggEntity) {
71  continue;
72  }
73 
75  'entity' => $entity,
76  'input_name' => $name,
77  'save_as_array' => $save_as_array,
78  ]);
79 }
80 
82  'elgg-list',
83  'elgg-entity-picker-list',
84 ];
85 if (elgg_extract('sortable', $vars)) {
86  $list_class[] = 'elgg-entity-picker-sortable';
87 }
88 
89 $picker .= elgg_format_element('ul', ['class' => $list_class], $items);
90 
92 
93 ?>
94 <script>
95  import('input/entitypicker').then((entitypicker) => {
96  entitypicker.default.setup('.elgg-entity-picker[data-name=<?= json_encode($name) ?>]');
97  });
98 </script>
$vars
Definition: theme.php:3
$entity
Definition: reset.php:8
$guid
Reset an ElggUpgrade.
Definition: reset.php:6
elgg_extract_class(array $array, array|string $existing=[], string $extract_key='class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:269
elgg_http_add_url_query_elements(string $url, array $elements)
Sets elements in a URL's query string.
Definition: elgglib.php:173
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:246
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:68
$save_as_array
if($limit !==1) $params
$item_view
$items
$picker
$name
Entity Picker.
$wrapper_options
$guids
$limit
foreach($guids as $guid) $list_class
elgg_build_hmac($data)
Get an HMAC token builder/validator object.
Definition: actions.php:57
elgg_view(string $view, array $vars=[], string $viewtype='')
Return a parsed view.
Definition: views.php:156
$mac
Definition: contents.php:14
elgg_format_element(string $tag_name, array $attributes=[], string $text='', array $options=[])
Format an HTML element.
Definition: output.php:145