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