annotate core/lib/Drupal/Core/Render/ElementInfoManager.php @ 19:fa3358dc1485 tip

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