ElementInfoManager.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Drupal\Core\Render;
  3. use Drupal\Core\Cache\Cache;
  4. use Drupal\Core\Cache\CacheBackendInterface;
  5. use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
  6. use Drupal\Core\Extension\ModuleHandlerInterface;
  7. use Drupal\Core\Plugin\DefaultPluginManager;
  8. use Drupal\Core\Render\Element\FormElementInterface;
  9. use Drupal\Core\Theme\ThemeManagerInterface;
  10. /**
  11. * Provides a plugin manager for element plugins.
  12. *
  13. * @see \Drupal\Core\Render\Annotation\RenderElement
  14. * @see \Drupal\Core\Render\Annotation\FormElement
  15. * @see \Drupal\Core\Render\Element\RenderElement
  16. * @see \Drupal\Core\Render\Element\FormElement
  17. * @see \Drupal\Core\Render\Element\ElementInterface
  18. * @see \Drupal\Core\Render\Element\FormElementInterface
  19. * @see plugin_api
  20. */
  21. class ElementInfoManager extends DefaultPluginManager implements ElementInfoManagerInterface {
  22. /**
  23. * Stores the available element information.
  24. *
  25. * @var array
  26. */
  27. protected $elementInfo;
  28. /**
  29. * The theme manager.
  30. *
  31. * @var \Drupal\Core\Theme\ThemeManagerInterface
  32. */
  33. protected $themeManager;
  34. /**
  35. * The cache tag invalidator.
  36. *
  37. * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
  38. */
  39. protected $cacheTagInvalidator;
  40. /**
  41. * Constructs a ElementInfoManager object.
  42. *
  43. * @param \Traversable $namespaces
  44. * An object that implements \Traversable which contains the root paths
  45. * keyed by the corresponding namespace to look for plugin implementations.
  46. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  47. * Cache backend instance to use.
  48. * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tag_invalidator
  49. * The cache tag invalidator.
  50. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  51. * The module handler to invoke the alter hook with.
  52. * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
  53. * The theme manager.
  54. */
  55. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, CacheTagsInvalidatorInterface $cache_tag_invalidator, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager) {
  56. $this->setCacheBackend($cache_backend, 'element_info');
  57. $this->themeManager = $theme_manager;
  58. $this->cacheTagInvalidator = $cache_tag_invalidator;
  59. parent::__construct('Element', $namespaces, $module_handler, 'Drupal\Core\Render\Element\ElementInterface', 'Drupal\Core\Render\Annotation\RenderElement');
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getInfo($type) {
  65. $theme_name = $this->themeManager->getActiveTheme()->getName();
  66. if (!isset($this->elementInfo[$theme_name])) {
  67. $this->elementInfo[$theme_name] = $this->buildInfo($theme_name);
  68. }
  69. $info = isset($this->elementInfo[$theme_name][$type]) ? $this->elementInfo[$theme_name][$type] : [];
  70. $info['#defaults_loaded'] = TRUE;
  71. return $info;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getInfoProperty($type, $property_name, $default = NULL) {
  77. $info = $this->getInfo($type);
  78. return isset($info[$property_name]) ? $info[$property_name] : $default;
  79. }
  80. /**
  81. * Builds up all element information.
  82. *
  83. * @param string $theme_name
  84. * The theme name.
  85. *
  86. * @return array
  87. */
  88. protected function buildInfo($theme_name) {
  89. // Get cached definitions.
  90. $cid = $this->getCid($theme_name);
  91. if ($cache = $this->cacheBackend->get($cid)) {
  92. return $cache->data;
  93. }
  94. // Otherwise, rebuild and cache.
  95. $info = [];
  96. foreach ($this->getDefinitions() as $element_type => $definition) {
  97. $element = $this->createInstance($element_type);
  98. $element_info = $element->getInfo();
  99. // If this is element is to be used exclusively in a form, denote that it
  100. // will receive input, and assign the value callback.
  101. if ($element instanceof FormElementInterface) {
  102. $element_info['#input'] = TRUE;
  103. $element_info['#value_callback'] = [$definition['class'], 'valueCallback'];
  104. }
  105. $info[$element_type] = $element_info;
  106. }
  107. foreach ($info as $element_type => $element) {
  108. $info[$element_type]['#type'] = $element_type;
  109. }
  110. // Allow modules to alter the element type defaults.
  111. $this->moduleHandler->alter('element_info', $info);
  112. $this->themeManager->alter('element_info', $info);
  113. $this->cacheBackend->set($cid, $info, Cache::PERMANENT, ['element_info_build']);
  114. return $info;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. *
  119. * @return \Drupal\Core\Render\Element\ElementInterface
  120. */
  121. public function createInstance($plugin_id, array $configuration = []) {
  122. return parent::createInstance($plugin_id, $configuration);
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function clearCachedDefinitions() {
  128. $this->elementInfo = NULL;
  129. $this->cacheTagInvalidator->invalidateTags(['element_info_build']);
  130. parent::clearCachedDefinitions();
  131. }
  132. /**
  133. * Returns the CID used to cache the element info.
  134. *
  135. * @param string $theme_name
  136. * The theme name.
  137. *
  138. * @return string
  139. */
  140. protected function getCid($theme_name) {
  141. return 'element_info_build:' . $theme_name;
  142. }
  143. }