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