4 * Contains \Drupal\bootstrap\Plugin\Preprocess\PreprocessBase.
7 namespace Drupal\bootstrap\Plugin\Preprocess;
9 use Drupal\bootstrap\Plugin\PluginBase;
10 use Drupal\bootstrap\Utility\Element;
11 use Drupal\bootstrap\Utility\Variables;
12 use Drupal\Core\Template\Attribute;
15 * Base preprocess class used to build the necessary variables for templates.
17 * @ingroup plugins_preprocess
19 class PreprocessBase extends PluginBase implements PreprocessInterface {
22 * The theme hook invoked.
29 * The theme hook info array from the theme registry.
36 * The Variables object.
38 * @type \Drupal\bootstrap\Utility\Variables
45 public function preprocess(array &$variables, $hook, array $info) {
48 $this->variables = Variables::create($variables);
49 if ($this->variables->element) {
50 // Check for errors and set the "has_error" property flag.
51 if (!$this->variables->element->hasProperty('has_error')) {
52 $errors = $this->variables->element->getProperty('errors');
53 $this->variables->element->setProperty('has_error', isset($errors) || ($this->variables->element->getProperty('required') && $this->theme->getSetting('forms_required_has_error')));
55 $this->preprocessElement($this->variables->element, $this->variables);
57 $this->preprocessVariables($this->variables);
61 * Ensures all attributes have been converted to an Attribute object.
63 protected function preprocessAttributes() {
64 foreach ($this->variables as $name => $value) {
65 if (strpos($name, 'attributes') !== FALSE && is_array($value)) {
66 $this->variables[$name] = new Attribute($value);
72 * Converts any set description variable into a traversable array.
74 * @see https://www.drupal.org/node/2324025
76 protected function preprocessDescription() {
77 if ($this->variables->offsetGet('description')) {
78 // Retrieve the description attributes.
79 $description_attributes = $this->variables->offsetGet('description_attributes', []);
81 // Remove standalone description attributes.
82 $this->variables->offsetUnset('description_attributes');
84 // Build the description attributes.
85 if ($id = $this->variables->getAttribute('id')) {
86 $this->variables->setAttribute('aria-describedby', "$id--description");
87 $description_attributes['id'] = "$id--description";
90 // Replace the description variable.
91 $this->variables->offsetSet('description', [
92 'attributes' => new Attribute($description_attributes),
93 'content' => $this->variables['description'],
94 'position' => $this->variables->offsetGet('description_display', 'after'),
100 * Preprocess the variables array if an element is present.
102 * @param \Drupal\bootstrap\Utility\Element $element
103 * The Element object.
104 * @param \Drupal\bootstrap\Utility\Variables $variables
105 * The Variables object.
107 protected function preprocessElement(Element $element, Variables $variables) {}
110 * Preprocess the variables array.
112 * @param \Drupal\bootstrap\Utility\Variables $variables
113 * The Variables object.
115 protected function preprocessVariables(Variables $variables) {}