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