e296d83b12af36c470f28e28219b7bf02a02f12c
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Preprocess / FormElement.php
1 <?php
2
3 namespace Drupal\bootstrap\Plugin\Preprocess;
4
5 use Drupal\bootstrap\Utility\Element;
6 use Drupal\bootstrap\Utility\Variables;
7
8 /**
9  * Pre-processes variables for the "form_element" theme hook.
10  *
11  * @ingroup plugins_preprocess
12  *
13  * @BootstrapPreprocess("form_element")
14  */
15 class FormElement extends PreprocessBase implements PreprocessInterface {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function preprocessElement(Element $element, Variables $variables) {
21     // Set has_error flag.
22     $variables['has_error'] = $element->getProperty('has_error');
23
24     if ($element->getProperty('autocomplete_route_name')) {
25       $variables['is_autocomplete'] = TRUE;
26     }
27
28     // See https://getbootstrap.com/docs/3.3/css/#forms-controls.
29     $checkbox = $variables['is_checkbox'] = $element->isType('checkbox');
30     $radio = $variables['is_radio'] = $element->isType('radio');
31
32     // Determine if the form element should have the "form-group" class added.
33     // Use an explicitly set property from the element or use its other
34     // properties as the criteria to determine if it should be set.
35     $variables['is_form_group'] = $element->getProperty('form_group', !$checkbox && !$radio && !$element->isType('hidden'));
36
37     // Add label_display and label variables to template.
38     $display = $variables['label_display'] = $variables['title_display'] = $element->getProperty('title_display');
39
40     // Place single checkboxes and radios in the label field.
41     if (($checkbox || $radio)) {
42       $label = Element::create($variables['label']);
43       $children = &$label->getProperty('children', '');
44       $children .= $variables['children'];
45       if ($label->getProperty('title_display') != 'none') {
46         unset($variables['children']);
47       }
48
49       // Inform label if it is in checkbox/radio context.
50       $label->setProperty('is_checkbox', $checkbox);
51       $label->setProperty('is_radio', $radio);
52
53       // Pass the label attributes to the label, if available.
54       if ($element->hasProperty('label_attributes')) {
55         $label->setAttributes($element->getProperty('label_attributes'));
56       }
57     }
58
59     // Remove the #field_prefix and #field_suffix values set in
60     // template_preprocess_form_element(). These are handled at the input level.
61     // @see \Drupal\bootstrap\Plugin\Preprocess\Input::preprocess().
62     unset($variables['prefix']);
63     unset($variables['suffix']);
64   }
65
66 }