8687a9dcfc980cf85a367b752db3566f7f30cc67
[yaffs-website] / web / core / lib / Drupal / Core / Render / Element / Radio.php
1 <?php
2
3 namespace Drupal\Core\Render\Element;
4
5 use Drupal\Core\Render\Element;
6
7 /**
8  * Provides a form element for a single radio button.
9  *
10  * This is an internal element that is primarily used to render the radios form
11  * element. Refer to \Drupal\Core\Render\Element\Radios for more documentation.
12  *
13  * @see \Drupal\Core\Render\Element\Radios
14  * @see \Drupal\Core\Render\Element\Checkbox
15  *
16  * @FormElement("radio")
17  */
18 class Radio extends FormElement {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function getInfo() {
24     $class = get_class($this);
25     return [
26       '#input' => TRUE,
27       '#default_value' => NULL,
28       '#process' => [
29         [$class, 'processAjaxForm'],
30       ],
31       '#pre_render' => [
32         [$class, 'preRenderRadio'],
33       ],
34       '#theme' => 'input__radio',
35       '#theme_wrappers' => ['form_element'],
36       '#title_display' => 'after',
37     ];
38   }
39
40   /**
41    * Prepares a #type 'radio' render element for input.html.twig.
42    *
43    * @param array $element
44    *   An associative array containing the properties of the element.
45    *   Properties used: #required, #return_value, #value, #attributes, #title,
46    *   #description. The #name property will be sanitized before output. This is
47    *   currently done by initializing Drupal\Core\Template\Attribute with all
48    *   the attributes.
49    *
50    * @return array
51    *   The $element with prepared variables ready for input.html.twig.
52    */
53   public static function preRenderRadio($element) {
54     $element['#attributes']['type'] = 'radio';
55     Element::setAttributes($element, ['id', 'name', '#return_value' => 'value']);
56
57     if (isset($element['#return_value']) && $element['#value'] !== FALSE && $element['#value'] == $element['#return_value']) {
58       $element['#attributes']['checked'] = 'checked';
59     }
60     static::setAttributes($element, ['form-radio']);
61
62     return $element;
63   }
64
65 }