file PreprocessBase.php

Namespace

Drupal\bootstrap\Plugin\Preprocess
  1. <?php
  2. namespace Drupal\bootstrap\Plugin\Preprocess;
  3. use Drupal\bootstrap\Plugin\PluginBase;
  4. use Drupal\bootstrap\Utility\Element;
  5. use Drupal\bootstrap\Utility\Variables;
  6. use Drupal\Core\Template\Attribute;
  7. /**
  8. * Base preprocess class used to build the necessary variables for templates.
  9. *
  10. * @ingroup plugins_preprocess
  11. */
  12. class PreprocessBase extends PluginBase implements PreprocessInterface {
  13. /**
  14. * The theme hook invoked.
  15. *
  16. * @var string
  17. */
  18. protected $hook;
  19. /**
  20. * The theme hook info array from the theme registry.
  21. *
  22. * @var array
  23. */
  24. protected $info;
  25. /**
  26. * The Variables object.
  27. *
  28. * @var \Drupal\bootstrap\Utility\Variables
  29. */
  30. protected $variables;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function preprocess(array &$variables, $hook, array $info) {
  35. $this->hook = $hook;
  36. $this->info = $info;
  37. $this->variables = Variables::create($variables);
  38. if ($this->variables->element) {
  39. // Check for errors and set the "has_error" property flag.
  40. if (!$this->variables->element->hasProperty('has_error')) {
  41. $errors = $this->variables->element->getProperty('errors');
  42. $this->variables->element->setProperty('has_error', isset($errors) || ($this->variables->element->getProperty('required') && $this->theme->getSetting('forms_required_has_error')));
  43. }
  44. $this->preprocessElement($this->variables->element, $this->variables);
  45. }
  46. $this->preprocessVariables($this->variables);
  47. }
  48. /**
  49. * Ensures all attributes have been converted to an Attribute object.
  50. */
  51. protected function preprocessAttributes() {
  52. foreach ($this->variables as $name => $value) {
  53. if (strpos($name, 'attributes') !== FALSE && is_array($value)) {
  54. $this->variables[$name] = new Attribute($value);
  55. }
  56. }
  57. }
  58. /**
  59. * Converts any set description variable into a traversable array.
  60. *
  61. * @see https://www.drupal.org/node/2324025
  62. */
  63. protected function preprocessDescription() {
  64. if ($this->variables->offsetGet('description')) {
  65. // Retrieve the description attributes.
  66. $description_attributes = $this->variables->offsetGet('description_attributes', []);
  67. // Remove standalone description attributes.
  68. $this->variables->offsetUnset('description_attributes');
  69. // Build the description attributes.
  70. if ($id = $this->variables->getAttribute('id')) {
  71. $this->variables->setAttribute('aria-describedby', "$id--description");
  72. $description_attributes['id'] = "$id--description";
  73. }
  74. // Replace the description variable.
  75. $this->variables->offsetSet('description', [
  76. 'attributes' => new Attribute($description_attributes),
  77. 'content' => $this->variables['description'],
  78. 'position' => $this->variables->offsetGet('description_display', 'after'),
  79. ]);
  80. }
  81. }
  82. /**
  83. * Preprocess the variables array if an element is present.
  84. *
  85. * @param \Drupal\bootstrap\Utility\Element $element
  86. * The Element object.
  87. * @param \Drupal\bootstrap\Utility\Variables $variables
  88. * The Variables object.
  89. */
  90. protected function preprocessElement(Element $element, Variables $variables) {}
  91. /**
  92. * Preprocess the variables array.
  93. *
  94. * @param \Drupal\bootstrap\Utility\Variables $variables
  95. * The Variables object.
  96. */
  97. protected function preprocessVariables(Variables $variables) {}
  98. }

Classes

Name Description
PreprocessBase Base preprocess class used to build the necessary variables for templates.