Version 1
[yaffs-website] / web / themes / contrib / bootstrap / src / Plugin / Preprocess / PreprocessBase.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Plugin\Preprocess\PreprocessBase.
5  */
6
7 namespace Drupal\bootstrap\Plugin\Preprocess;
8
9 use Drupal\bootstrap\Plugin\PluginBase;
10 use Drupal\bootstrap\Utility\Element;
11 use Drupal\bootstrap\Utility\Variables;
12 use Drupal\Core\Template\Attribute;
13
14 /**
15  * Base preprocess class used to build the necessary variables for templates.
16  *
17  * @ingroup plugins_preprocess
18  */
19 class PreprocessBase extends PluginBase implements PreprocessInterface {
20
21   /**
22    * The theme hook invoked.
23    *
24    * @type string
25    */
26   protected $hook;
27
28   /**
29    * The theme hook info array from the theme registry.
30    *
31    * @type array
32    */
33   protected $info;
34
35   /**
36    * The Variables object.
37    *
38    * @type \Drupal\bootstrap\Utility\Variables
39    */
40   protected $variables;
41
42   /**
43    * {@inheritdoc}
44    */
45   public function preprocess(array &$variables, $hook, array $info) {
46     $this->hook = $hook;
47     $this->info = $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')));
54       }
55       $this->preprocessElement($this->variables->element, $this->variables);
56     }
57     $this->preprocessVariables($this->variables);
58   }
59
60   /**
61    * Ensures all attributes have been converted to an Attribute object.
62    */
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);
67       }
68     }
69   }
70
71   /**
72    * Converts any set description variable into a traversable array.
73    *
74    * @see https://www.drupal.org/node/2324025
75    */
76   protected function preprocessDescription() {
77     if ($this->variables->offsetGet('description')) {
78       // Retrieve the description attributes.
79       $description_attributes = $this->variables->offsetGet('description_attributes', []);
80
81       // Remove standalone description attributes.
82       $this->variables->offsetUnset('description_attributes');
83
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";
88       }
89
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'),
95       ]);
96     }
97   }
98
99   /**
100    * Preprocess the variables array if an element is present.
101    *
102    * @param \Drupal\bootstrap\Utility\Element $element
103    *   The Element object.
104    * @param \Drupal\bootstrap\Utility\Variables $variables
105    *   The Variables object.
106    */
107   protected function preprocessElement(Element $element, Variables $variables) {}
108
109   /**
110    * Preprocess the variables array.
111    *
112    * @param \Drupal\bootstrap\Utility\Variables $variables
113    *   The Variables object.
114    */
115   protected function preprocessVariables(Variables $variables) {}
116
117 }