Elgg  Version master
select.php
Go to the documentation of this file.
1 <?php
25 $vars['class'] = elgg_extract_class($vars, ['elgg-input-select']);
26 
28  'value' => '',
29 ];
30 
31 $vars = array_merge($defaults, $vars);
32 
34 
35 // turn options into options_values
36 $options = elgg_extract('options', $vars, []);
37 unset($vars['options']);
38 
39 foreach ($options as $option) {
40  if (is_array($option)) {
41  if (!isset($option['value'])) {
42  $option['value'] = elgg_extract('text', $option);
43  }
44  } else {
45  $option = [
46  'text' => $option,
47  'value' => $option,
48  ];
49  }
50 
51  $options_values[] = $option;
52 }
53 
54 // provided options_values trump options
56 unset($vars['options_values']);
57 
59  foreach ($options as $key => $option) {
60  if (!is_array($option)) {
61  $option = [
62  'text' => $option,
63  'value' => $key,
64  ];
65  }
66 
67  $optgroup = elgg_extract('options', $option);
68  if (is_array($optgroup)) {
69  $option['options'] = $normalize_options($optgroup);
70  }
71 
72  $options[$key] = $option;
73  }
74 
75  return $options;
76 };
77 
79 
80 $value = is_array($vars['value']) ? $vars['value'] : [$vars['value']];
81 $value = array_map('strval', $value);
82 unset($vars['value']);
83 
84 $vars['multiple'] = !empty($vars['multiple']);
85 
86 // Add trailing [] to name if multiple is enabled to allow the form to send multiple values
87 if ($vars['multiple'] && !empty($vars['name']) && is_string($vars['name'])) {
88  if (!str_ends_with($vars['name'], '[]')) {
89  $vars['name'] = elgg_extract('name', $vars) . '[]';
90  }
91 }
92 
93 $render_option = function($option) use ($value) {
94  $opt_value = elgg_extract('value', $option);
95 
96  $text = elgg_extract('text', $option);
97  unset($option['text']);
98 
99  if (!is_string($text) && !is_numeric($text)) {
100  elgg_log('No text defined for input/select option with value "' . $opt_value . '"', \Psr\Log\LogLevel::NOTICE);
101  }
102 
103  if (!isset($option['selected'])) {
104  $option['selected'] = isset($opt_value) && in_array((string) $opt_value, $value);
105  }
106 
107  if (!isset($option['title'])) {
108  $option['title'] = $text;
109  }
110 
111  return elgg_format_element('option', $option, $text);
112 };
113 
115 
116 foreach ($options_values as $option) {
117  $options = elgg_extract('options', $option);
118 
119  if (is_array($options)) {
120  $optgroup_attrs = $option;
121  unset($optgroup_attrs['options']);
122 
123  $optgroup = '';
124  foreach ($options as $group_option) {
125  $optgroup .= $render_option($group_option);
126  }
127 
128  $options_list .= elgg_format_element('optgroup', $optgroup_attrs, $optgroup);
129  } else {
130  $options_list .= $render_option($option);
131  }
132 }
133 
134 echo elgg_format_element('select', $vars, $options_list);
$defaults
Definition: select.php:27
$value
Definition: select.php:80
$options_list
Definition: select.php:114
$options_values
Definition: select.php:33
$options
Definition: select.php:36
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:254
$normalize_options
Definition: select.php:58
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:86
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
$vars['class']
Elgg select input Displays a select input field.
Definition: select.php:25
elgg_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:145
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:277
if($vars['multiple']&&!empty($vars['name'])&&is_string($vars['name'])) $render_option
Definition: select.php:93
$text
Definition: button.php:33