file ElementInfo.php

Namespace

Drupal\bootstrap\Plugin\Alter
  1. <?php
  2. namespace Drupal\bootstrap\Plugin\Alter;
  3. use Drupal\bootstrap\Bootstrap;
  4. use Drupal\bootstrap\Plugin\PluginBase;
  5. use Drupal\bootstrap\Plugin\PrerenderManager;
  6. use Drupal\bootstrap\Plugin\ProcessManager;
  7. /**
  8. * Implements hook_element_info_alter().
  9. *
  10. * @ingroup plugins_alter
  11. *
  12. * @BootstrapAlter("element_info")
  13. */
  14. class ElementInfo extends PluginBase implements AlterInterface {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function alter(&$types, &$context1 = NULL, &$context2 = NULL) {
  19. // Sort the types for easier debugging.
  20. ksort($types, SORT_NATURAL);
  21. $extra_variables = Bootstrap::extraVariables();
  22. $process_manager = new ProcessManager($this->theme);
  23. $pre_render_manager = new PrerenderManager($this->theme);
  24. foreach (array_keys($types) as $type) {
  25. $element = &$types[$type];
  26. // By default, the "checkboxes" and "radios" element types invoke
  27. // CompositeFormElementTrait::preRenderCompositeFormElement which wraps
  28. // the element in a fieldset and thus ultimately a panel. This isn't
  29. // (usually) the desired effect for these elements, so to avoid rendering
  30. // them as Bootstrap panels, the #bootstrap_panel should be set to FALSE
  31. // by default. This allows those who wish to opt back in to do so.
  32. if ($type === 'checkboxes' || $type === 'radios') {
  33. $element['#bootstrap_panel'] = FALSE;
  34. }
  35. // Core does not actually use the "description_display" property on the
  36. // "details" or "fieldset" element types because the positioning of the
  37. // description is never used in core templates. However, the form builder
  38. // automatically applies the value of "after", thus making it impossible
  39. // to detect a valid value later in the rendering process. It looks better
  40. // for the "details" and "fieldset" element types to display as "before".
  41. // @see \Drupal\Core\Form\FormBuilder::doBuildForm()
  42. if ($type === 'details' || $type === 'fieldset') {
  43. $element['#description_display'] = 'before';
  44. $element['#panel_type'] = 'default';
  45. }
  46. // Add extra variables as defaults to all elements.
  47. foreach ($extra_variables as $key => $value) {
  48. if (!isset($element["#$key"])) {
  49. $element["#$key"] = $value;
  50. }
  51. }
  52. // Only continue if the type isn't "form" (as it messes up AJAX).
  53. if ($type !== 'form') {
  54. $regex = "/^$type/";
  55. // Add necessary #process callbacks.
  56. $element['#process'][] = [get_class($process_manager), 'process'];
  57. $definitions = $process_manager->getDefinitionsLike($regex);
  58. foreach ($definitions as $definition) {
  59. Bootstrap::addCallback($element['#process'], [$definition['class'], 'process'], $definition['replace'], $definition['action']);
  60. }
  61. // Add necessary #pre_render callbacks.
  62. $element['#pre_render'][] = [get_class($pre_render_manager), 'preRender'];
  63. foreach ($pre_render_manager->getDefinitionsLike($regex) as $definition) {
  64. Bootstrap::addCallback($element['#pre_render'], [$definition['class'], 'preRender'], $definition['replace'], $definition['action']);
  65. }
  66. }
  67. }
  68. }
  69. }

Classes

Name Description
ElementInfo Implements hook_element_info_alter().