3 namespace Drupal\bootstrap\Plugin\Preprocess;
5 use Drupal\bootstrap\Utility\Element;
6 use Drupal\bootstrap\Utility\Variables;
9 * Pre-processes variables for the "form_element" theme hook.
11 * @ingroup plugins_preprocess
13 * @BootstrapPreprocess("form_element")
15 class FormElement extends PreprocessBase implements PreprocessInterface {
20 public function preprocessElement(Element $element, Variables $variables) {
21 // Set has_error flag.
22 $variables['has_error'] = $element->getProperty('has_error');
24 if ($element->getProperty('autocomplete_route_name')) {
25 $variables['is_autocomplete'] = TRUE;
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');
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'));
37 // Add label_display and label variables to template.
38 $display = $variables['label_display'] = $variables['title_display'] = $element->getProperty('title_display');
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']);
49 // Inform label if it is in checkbox/radio context.
50 $label->setProperty('is_checkbox', $checkbox);
51 $label->setProperty('is_radio', $radio);
53 // Pass the label attributes to the label, if available.
54 if ($element->hasProperty('label_attributes')) {
55 $label->setAttributes($element->getProperty('label_attributes'));
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']);