ElementInfoManager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. $this->alterInfo('element_plugin');
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getInfo($type) {
  66. $theme_name = $this->themeManager->getActiveTheme()->getName();
  67. if (!isset($this->elementInfo[$theme_name])) {
  68. $this->elementInfo[$theme_name] = $this->buildInfo($theme_name);
  69. }
  70. $info = isset($this->elementInfo[$theme_name][$type]) ? $this->elementInfo[$theme_name][$type] : [];
  71. $info['#defaults_loaded'] = TRUE;
  72. return $info;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function getInfoProperty($type, $property_name, $default = NULL) {
  78. $info = $this->getInfo($type);
  79. return isset($info[$property_name]) ? $info[$property_name] : $default;
  80. }
  81. /**
  82. * Builds up all element information.
  83. *
  84. * @param string $theme_name
  85. * The theme name.
  86. *
  87. * @return array
  88. */
  89. protected function buildInfo($theme_name) {
  90. // Get cached definitions.
  91. $cid = $this->getCid($theme_name);
  92. if ($cache = $this->cacheBackend->get($cid)) {
  93. return $cache->data;
  94. }
  95. // Otherwise, rebuild and cache.
  96. $info = [];
  97. foreach ($this->getDefinitions() as $element_type => $definition) {
  98. $element = $this->createInstance($element_type);
  99. $element_info = $element->getInfo();
  100. // If this is element is to be used exclusively in a form, denote that it
  101. // will receive input, and assign the value callback.
  102. if ($element instanceof FormElementInterface) {
  103. $element_info['#input'] = TRUE;
  104. $element_info['#value_callback'] = [$definition['class'], 'valueCallback'];
  105. }
  106. $info[$element_type] = $element_info;
  107. }
  108. foreach ($info as $element_type => $element) {
  109. $info[$element_type]['#type'] = $element_type;
  110. }
  111. // Allow modules to alter the element type defaults.
  112. $this->moduleHandler->alter('element_info', $info);
  113. $this->themeManager->alter('element_info', $info);
  114. $this->cacheBackend->set($cid, $info, Cache::PERMANENT, ['element_info_build']);
  115. return $info;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. *
  120. * @return \Drupal\Core\Render\Element\ElementInterface
  121. */
  122. public function createInstance($plugin_id, array $configuration = []) {
  123. return parent::createInstance($plugin_id, $configuration);
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function clearCachedDefinitions() {
  129. $this->elementInfo = NULL;
  130. $this->cacheTagInvalidator->invalidateTags(['element_info_build']);
  131. parent::clearCachedDefinitions();
  132. }
  133. /**
  134. * Returns the CID used to cache the element info.
  135. *
  136. * @param string $theme_name
  137. * The theme name.
  138. *
  139. * @return string
  140. */
  141. protected function getCid($theme_name) {
  142. return 'element_info_build:' . $theme_name;
  143. }
  144. }