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