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