4 * Contains \Drupal\bootstrap\Plugin\Preprocess\FormElement.
7 namespace Drupal\bootstrap\Plugin\Preprocess;
9 use Drupal\bootstrap\Annotation\BootstrapPreprocess;
10 use Drupal\bootstrap\Utility\Element;
11 use Drupal\bootstrap\Utility\Variables;
14 * Pre-processes variables for the "form_element" theme hook.
16 * @ingroup plugins_preprocess
18 * @BootstrapPreprocess("form_element")
20 class FormElement extends PreprocessBase implements PreprocessInterface {
25 public function preprocessElement(Element $element, Variables $variables) {
26 // Set has_error flag.
27 $variables['has_error'] = $element->getProperty('has_error');
29 if ($element->getProperty('autocomplete_route_name')) {
30 $variables['is_autocomplete'] = TRUE;
33 // See http://getbootstrap.com/css/#forms-controls.
34 $checkbox = $variables['is_checkbox'] = $element->isType('checkbox');
35 $radio = $variables['is_radio'] = $element->isType('radio');
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']));
42 // Add label_display and label variables to template.
43 $display = $variables['label_display'] = $variables['title_display'] = $element->getProperty('title_display');
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']);
54 // Inform label if it is in checkbox/radio context.
55 $label->setProperty('is_checkbox', $checkbox);
56 $label->setProperty('is_radio', $radio);
58 // Pass the label attributes to the label, if available.
59 if ($element->hasProperty('label_attributes')) {
60 $label->setAttributes($element->getProperty('label_attributes'));
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']);