Elgg  Version 2.3
select.php
Go to the documentation of this file.
1 <?php
26 $vars['class'] = elgg_extract_class($vars, 'elgg-input-dropdown');
27 
28 $defaults = array(
29  'disabled' => false,
30  'value' => '',
31  'options_values' => array(),
32  'options' => array(),
33 );
34 
35 $vars = array_merge($defaults, $vars);
36 
37 $options_values = $vars['options_values'];
38 unset($vars['options_values']);
39 
40 $options = $vars['options'];
41 unset($vars['options']);
42 
43 $value = is_array($vars['value']) ? $vars['value'] : array($vars['value']);
44 $value = array_map('strval', $value);
45 unset($vars['value']);
46 
47 $vars['multiple'] = !empty($vars['multiple']);
48 
49 // Add trailing [] to name if multiple is enabled to allow the form to send multiple values
50 if ($vars['multiple'] && !empty($vars['name']) && is_string($vars['name'])) {
51  if (substr($vars['name'], -2) != '[]') {
52  $vars['name'] = $vars['name'] . '[]';
53  }
54 }
55 
57 
58 if ($options_values) {
59  foreach ($options_values as $opt_value => $option) {
60 
61  $option_attrs = array(
62  'value' => $opt_value,
63  'selected' => in_array((string)$opt_value, $value),
64  );
65 
66  if (is_array($option)) {
67  $text = elgg_extract('text', $option, '');
68  unset($option['text']);
69  if (!$text) {
70  elgg_log('No text defined for input/select option with value "' . $opt_value . '"', 'ERROR');
71  }
72 
73  $option_attrs = array_merge($option_attrs, $option);
74  } else {
75  $text = $option;
76  }
77 
78  $options_list .= elgg_format_element('option', $option_attrs, $text);
79  }
80 } else {
81  if (is_array($options)) {
82  foreach ($options as $option) {
83 
84  if (is_array($option)) {
85  $text = elgg_extract('text', $option, '');
86  unset($option['text']);
87 
88  if (!$text) {
89  elgg_log('No text defined for input/select option', 'ERROR');
90  }
91 
92  $option_attrs = [
93  'selected' => in_array((string)$text, $value),
94  ];
95  $option_attrs = array_merge($option_attrs, $option);
96  } else {
97  $option_attrs = [
98  'selected' => in_array((string)$option, $value),
99  ];
100 
101  $text = $option;
102  }
103 
104  $options_list .= elgg_format_element('option', $option_attrs, $text);
105  }
106  }
107 }
108 
$defaults
Definition: select.php:28
$value
Definition: select.php:43
elgg_extract_class(array $array, $existing=[])
Extract class names from an array with key "class", optionally merging into a preexisting set...
Definition: elgglib.php:1396
$options_values
Definition: select.php:37
$options
Definition: select.php:40
elgg_format_element($tag_name, array $attributes=array(), $text= '', array $options=array())
Format an HTML element.
Definition: output.php:208
$text
Definition: default.php:25
if($vars['multiple']&&!empty($vars['name'])&&is_string($vars['name'])) $options_list
Definition: select.php:56
elgg echo
Translates a string.
Definition: languages.js:48
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:1375
elgg_log($message, $level= 'NOTICE')
Display or log a message.
Definition: elgglib.php:1028
$vars['class']
Definition: select.php:26