Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Render/ElementInfoManager.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Core\Render; | |
4 | |
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; | |
12 | |
13 /** | |
14 * Provides a plugin manager for element plugins. | |
15 * | |
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 | |
22 * @see plugin_api | |
23 */ | |
24 class ElementInfoManager extends DefaultPluginManager implements ElementInfoManagerInterface { | |
25 | |
26 /** | |
27 * Stores the available element information. | |
28 * | |
29 * @var array | |
30 */ | |
31 protected $elementInfo; | |
32 | |
33 /** | |
34 * The theme manager. | |
35 * | |
36 * @var \Drupal\Core\Theme\ThemeManagerInterface | |
37 */ | |
38 protected $themeManager; | |
39 | |
40 /** | |
41 * The cache tag invalidator. | |
42 * | |
43 * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface | |
44 */ | |
45 protected $cacheTagInvalidator; | |
46 | |
47 /** | |
48 * Constructs a ElementInfoManager object. | |
49 * | |
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 | |
60 * The theme manager. | |
61 */ | |
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; | |
66 | |
67 parent::__construct('Element', $namespaces, $module_handler, 'Drupal\Core\Render\Element\ElementInterface', 'Drupal\Core\Render\Annotation\RenderElement'); | |
68 } | |
69 | |
70 /** | |
71 * {@inheritdoc} | |
72 */ | |
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); | |
77 } | |
78 $info = isset($this->elementInfo[$theme_name][$type]) ? $this->elementInfo[$theme_name][$type] : []; | |
79 $info['#defaults_loaded'] = TRUE; | |
80 return $info; | |
81 } | |
82 | |
83 /** | |
84 * {@inheritdoc} | |
85 */ | |
86 public function getInfoProperty($type, $property_name, $default = NULL) { | |
87 $info = $this->getInfo($type); | |
88 | |
89 return isset($info[$property_name]) ? $info[$property_name] : $default; | |
90 } | |
91 | |
92 /** | |
93 * Builds up all element information. | |
94 * | |
95 * @param string $theme_name | |
96 * The theme name. | |
97 * | |
98 * @return array | |
99 */ | |
100 protected function buildInfo($theme_name) { | |
101 // Get cached definitions. | |
102 $cid = $this->getCid($theme_name); | |
103 if ($cache = $this->cacheBackend->get($cid)) { | |
104 return $cache->data; | |
105 } | |
106 | |
107 // Otherwise, rebuild and cache. | |
108 $info = []; | |
109 foreach ($this->getDefinitions() as $element_type => $definition) { | |
110 $element = $this->createInstance($element_type); | |
111 $element_info = $element->getInfo(); | |
112 | |
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']; | |
118 } | |
119 $info[$element_type] = $element_info; | |
120 } | |
121 | |
122 foreach ($info as $element_type => $element) { | |
123 $info[$element_type]['#type'] = $element_type; | |
124 } | |
125 // Allow modules to alter the element type defaults. | |
126 $this->moduleHandler->alter('element_info', $info); | |
127 $this->themeManager->alter('element_info', $info); | |
128 | |
129 $this->cacheBackend->set($cid, $info, Cache::PERMANENT, ['element_info_build']); | |
130 | |
131 return $info; | |
132 } | |
133 | |
134 /** | |
135 * {@inheritdoc} | |
136 * | |
137 * @return \Drupal\Core\Render\Element\ElementInterface | |
138 */ | |
139 public function createInstance($plugin_id, array $configuration = []) { | |
140 return parent::createInstance($plugin_id, $configuration); | |
141 } | |
142 | |
143 /** | |
144 * {@inheritdoc} | |
145 */ | |
146 public function clearCachedDefinitions() { | |
147 $this->elementInfo = NULL; | |
148 $this->cacheTagInvalidator->invalidateTags(['element_info_build']); | |
149 | |
150 parent::clearCachedDefinitions(); | |
151 } | |
152 | |
153 /** | |
154 * Returns the CID used to cache the element info. | |
155 * | |
156 * @param string $theme_name | |
157 * The theme name. | |
158 * | |
159 * @return string | |
160 */ | |
161 protected function getCid($theme_name) { | |
162 return 'element_info_build:' . $theme_name; | |
163 } | |
164 | |
165 } |