Elgg  Version master
radio.php
Go to the documentation of this file.
1 <?php
14  'align' => 'vertical',
15  'value' => [],
16  'options' => [],
17  'name' => '',
18  'type' => 'radio',
19 ];
20 
21 $vars = array_merge($defaults, $vars);
22 
23 $options_values = elgg_extract('options_values', $vars, []);
24 unset($vars['options_values']);
25 
27 
28 foreach ($options_values as $key => $value) {
29  if (is_array($value)) {
30  $key = elgg_extract('text', $value, $key);
31  } else {
32  $value = [
33  'text' => $value,
34  'value' => $key,
35  ];
36  }
37 
39 }
40 
41 // turn options into options_values
42 $options = elgg_extract('options', $vars);
43 unset($vars['options']);
44 if (!empty($options) && empty($input_options)) {
45  foreach ($options as $label => $value) {
46  $input_options[$label] = ['value' => $value];
47  }
48 }
49 
50 if (empty($input_options)) {
51  return;
52 }
53 
54 $id = elgg_extract('id', $vars, '');
55 unset($vars['id']);
56 
57 $list_class = elgg_extract_class($vars, ['elgg-input-radios', "elgg-{$vars['align']}"]);
58 
59 unset($vars['class']);
60 unset($vars['align']);
61 
62 $vars['class'] = 'elgg-input-radio';
63 
64 if (is_array(elgg_extract('value', $vars))) {
65  $selected_value = array_map('elgg_strtolower', elgg_extract('value', $vars));
66 } else {
67  $selected_value = [elgg_strtolower((string) elgg_extract('value', $vars))];
68 }
69 
70 unset($vars['value']);
71 
72 $radios = '';
73 foreach ($input_options as $label => $option) {
74  $radio_input_options = array_merge($vars, $option);
75  $radio_input_options['checked'] = in_array(elgg_strtolower((string) elgg_extract('value', $option)), $selected_value);
76 
77  $label = elgg_extract('text', $radio_input_options, $label);
78  unset($radio_input_options['text']);
79 
80  $radio = elgg_format_element('input', $radio_input_options);
81  $radios .= "<li><label>{$radio}{$label}</label></li>";
82 }
83 
84 echo elgg_format_element('ul', [
85  'class' => $list_class,
86  'id' => $id,
87 ], $radios);
if($view &&elgg_view_exists($view)) $label
Definition: field.php:26
elgg_strtolower()
Wrapper function for mb_strtolower().
Definition: mb_wrapper.php:125
$options_values
Definition: radio.php:23
$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
foreach($options_values as $key=> $value) $options
Definition: radio.php:42
$vars
Definition: radio.php:21
$input_options
Definition: radio.php:26
$defaults
Displays a radio input field.
Definition: radio.php:13
$list_class
Definition: radio.php:57
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
if(!empty($options)&&empty($input_options)) if(empty($input_options)) $id
Definition: radio.php:54
$radios
Definition: radio.php:72
elgg_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:145
elgg_extract_class(array $array, array|string $existing=[], string $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:277