Elgg  Version master
checkboxes.php
Go to the documentation of this file.
1 <?php
24  'align' => 'vertical',
25  'value' => [],
26  'default' => 0,
27  'disabled' => false,
28  'options' => [],
29  'name' => '',
30 ];
31 
32 $vars = array_merge($defaults, $vars);
33 
34 $options_values = elgg_extract('options_values', $vars, []);
35 unset($vars['options_values']);
36 
38 
39 foreach ($options_values as $key => $value) {
40  if (is_array($value)) {
41  $key = elgg_extract('text', $value, $key);
42  } else {
43  $value = [
44  'text' => $value,
45  'value' => $key,
46  ];
47  }
48 
50 }
51 
52 // turn options into options_values
53 $options = elgg_extract('options', $vars);
54 unset($vars['options']);
55 if (!empty($options) && empty($input_options)) {
56  foreach ($options as $label => $value) {
57  $input_options[$label] = ['value' => $value];
58  }
59 }
60 
61 if (empty($input_options)) {
62  return;
63 }
64 
66  'elgg-input-checkboxes',
67  "elgg-{$vars['align']}",
68 ]);
69 unset($vars['class']);
70 
71 $id = elgg_extract('id', $vars, '');
72 unset($vars['id']);
73 
74 if (is_array(elgg_extract('value', $vars))) {
75  $values = array_map('elgg_strtolower', elgg_extract('value', $vars));
76 } else {
77  $values = [elgg_strtolower((string) elgg_extract('value', $vars))];
78 }
79 
80 // include a default value so if nothing is checked 0 will be passed.
81 if ($vars['name'] && $vars['default'] !== false) {
82  echo elgg_view('input/hidden', [
83  'name' => elgg_extract('name', $vars),
84  'value' => elgg_extract('default', $vars),
85  'disabled' => elgg_extract('disabled', $vars),
86  ]);
87 }
88 
89 // prepare checkbox vars
91 $input_vars['default'] = false;
92 if (elgg_extract('name', $vars)) {
93  $input_vars['name'] = "{$vars['name']}[]";
94 }
95 
96 unset($input_vars['align']);
97 
99 foreach ($input_options as $label => $option) {
100  $checkbox_input_options = array_merge($input_vars, $option);
101  $checkbox_input_options['checked'] = in_array(elgg_strtolower((string) elgg_extract('value', $option)), $values);
102  $checkbox_input_options['label'] = elgg_extract('text', $checkbox_input_options, $label);
103  unset($checkbox_input_options['text']);
104 
105  $input = elgg_view('input/checkbox', $checkbox_input_options);
106 
107  $checkboxes .= elgg_format_element('li', [], $input);
108 }
109 
110 echo elgg_format_element('ul', ['class' => $list_class, 'id' => $id], $checkboxes);
if($view &&elgg_view_exists($view)) $label
Definition: field.php:26
$checkboxes
Definition: checkboxes.php:98
if(!empty($options)&&empty($input_options)) if(empty($input_options)) $list_class
Definition: checkboxes.php:65
$options_values
Definition: checkboxes.php:34
if($vars['name']&&$vars['default']!==false) $input_vars
Definition: checkboxes.php:90
elgg_strtolower()
Wrapper function for mb_strtolower().
Definition: mb_wrapper.php:161
elgg_extract_class(array $array, $existing=[], $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:276
$value
Definition: generic.php:51
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
$input_options
Definition: checkboxes.php:37
elgg_view(string $view, array $vars=[], string $viewtype= '')
Return a parsed view.
Definition: views.php:177
foreach($options_values as $key=> $value) $options
Definition: checkboxes.php:53
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
$defaults
Displays a checkbox input field.
Definition: checkboxes.php:23
$id
Definition: checkboxes.php:71
$input
Form field view.
Definition: field.php:13
elgg_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:130
$vars
Definition: checkboxes.php:32