3 namespace Drupal\Core\Render;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Plugin\DefaultPluginManager;
10 use Drupal\Core\Render\Element\FormElementInterface;
11 use Drupal\Core\Theme\ThemeManagerInterface;
14 * Provides a plugin manager for element plugins.
16 * @see \Drupal\Core\Render\Annotation\RenderElement
17 * @see \Drupal\Core\Render\Annotation\FormElement
18 * @see \Drupal\Core\Render\Element\RenderElement
19 * @see \Drupal\Core\Render\Element\FormElement
20 * @see \Drupal\Core\Render\Element\ElementInterface
21 * @see \Drupal\Core\Render\Element\FormElementInterface
24 class ElementInfoManager extends DefaultPluginManager implements ElementInfoManagerInterface {
27 * Stores the available element information.
31 protected $elementInfo;
36 * @var \Drupal\Core\Theme\ThemeManagerInterface
38 protected $themeManager;
41 * The cache tag invalidator.
43 * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
45 protected $cacheTagInvalidator;
48 * Constructs a ElementInfoManager object.
50 * @param \Traversable $namespaces
51 * An object that implements \Traversable which contains the root paths
52 * keyed by the corresponding namespace to look for plugin implementations.
53 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
54 * Cache backend instance to use.
55 * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tag_invalidator
56 * The cache tag invalidator.
57 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
58 * The module handler to invoke the alter hook with.
59 * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
62 public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, CacheTagsInvalidatorInterface $cache_tag_invalidator, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager) {
63 $this->setCacheBackend($cache_backend, 'element_info');
64 $this->themeManager = $theme_manager;
65 $this->cacheTagInvalidator = $cache_tag_invalidator;
67 parent::__construct('Element', $namespaces, $module_handler, 'Drupal\Core\Render\Element\ElementInterface', 'Drupal\Core\Render\Annotation\RenderElement');
73 public function getInfo($type) {
74 $theme_name = $this->themeManager->getActiveTheme()->getName();
75 if (!isset($this->elementInfo[$theme_name])) {
76 $this->elementInfo[$theme_name] = $this->buildInfo($theme_name);
78 $info = isset($this->elementInfo[$theme_name][$type]) ? $this->elementInfo[$theme_name][$type] : [];
79 $info['#defaults_loaded'] = TRUE;
86 public function getInfoProperty($type, $property_name, $default = NULL) {
87 $info = $this->getInfo($type);
89 return isset($info[$property_name]) ? $info[$property_name] : $default;
93 * Builds up all element information.
95 * @param string $theme_name
100 protected function buildInfo($theme_name) {
101 // Get cached definitions.
102 $cid = $this->getCid($theme_name);
103 if ($cache = $this->cacheBackend->get($cid)) {
107 // Otherwise, rebuild and cache.
109 foreach ($this->getDefinitions() as $element_type => $definition) {
110 $element = $this->createInstance($element_type);
111 $element_info = $element->getInfo();
113 // If this is element is to be used exclusively in a form, denote that it
114 // will receive input, and assign the value callback.
115 if ($element instanceof FormElementInterface) {
116 $element_info['#input'] = TRUE;
117 $element_info['#value_callback'] = [$definition['class'], 'valueCallback'];
119 $info[$element_type] = $element_info;
122 foreach ($info as $element_type => $element) {
123 $info[$element_type]['#type'] = $element_type;
125 // Allow modules to alter the element type defaults.
126 $this->moduleHandler->alter('element_info', $info);
127 $this->themeManager->alter('element_info', $info);
129 $this->cacheBackend->set($cid, $info, Cache::PERMANENT, ['element_info_build']);
137 * @return \Drupal\Core\Render\Element\ElementInterface
139 public function createInstance($plugin_id, array $configuration = []) {
140 return parent::createInstance($plugin_id, $configuration);
146 public function clearCachedDefinitions() {
147 $this->elementInfo = NULL;
148 $this->cacheTagInvalidator->invalidateTags(['element_info_build']);
150 parent::clearCachedDefinitions();
154 * Returns the CID used to cache the element info.
156 * @param string $theme_name
161 protected function getCid($theme_name) {
162 return 'element_info_build:' . $theme_name;