Chris@0: setCacheBackend($cache_backend, 'element_info'); Chris@0: $this->themeManager = $theme_manager; Chris@0: $this->cacheTagInvalidator = $cache_tag_invalidator; Chris@0: Chris@0: parent::__construct('Element', $namespaces, $module_handler, 'Drupal\Core\Render\Element\ElementInterface', 'Drupal\Core\Render\Annotation\RenderElement'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getInfo($type) { Chris@0: $theme_name = $this->themeManager->getActiveTheme()->getName(); Chris@0: if (!isset($this->elementInfo[$theme_name])) { Chris@0: $this->elementInfo[$theme_name] = $this->buildInfo($theme_name); Chris@0: } Chris@0: $info = isset($this->elementInfo[$theme_name][$type]) ? $this->elementInfo[$theme_name][$type] : []; Chris@0: $info['#defaults_loaded'] = TRUE; Chris@0: return $info; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getInfoProperty($type, $property_name, $default = NULL) { Chris@0: $info = $this->getInfo($type); Chris@0: Chris@0: return isset($info[$property_name]) ? $info[$property_name] : $default; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Builds up all element information. Chris@0: * Chris@0: * @param string $theme_name Chris@0: * The theme name. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: protected function buildInfo($theme_name) { Chris@0: // Get cached definitions. Chris@0: $cid = $this->getCid($theme_name); Chris@0: if ($cache = $this->cacheBackend->get($cid)) { Chris@0: return $cache->data; Chris@0: } Chris@0: Chris@0: // Otherwise, rebuild and cache. Chris@0: $info = []; Chris@0: foreach ($this->getDefinitions() as $element_type => $definition) { Chris@0: $element = $this->createInstance($element_type); Chris@0: $element_info = $element->getInfo(); Chris@0: Chris@0: // If this is element is to be used exclusively in a form, denote that it Chris@0: // will receive input, and assign the value callback. Chris@0: if ($element instanceof FormElementInterface) { Chris@0: $element_info['#input'] = TRUE; Chris@0: $element_info['#value_callback'] = [$definition['class'], 'valueCallback']; Chris@0: } Chris@0: $info[$element_type] = $element_info; Chris@0: } Chris@0: Chris@0: foreach ($info as $element_type => $element) { Chris@0: $info[$element_type]['#type'] = $element_type; Chris@0: } Chris@0: // Allow modules to alter the element type defaults. Chris@0: $this->moduleHandler->alter('element_info', $info); Chris@0: $this->themeManager->alter('element_info', $info); Chris@0: Chris@0: $this->cacheBackend->set($cid, $info, Cache::PERMANENT, ['element_info_build']); Chris@0: Chris@0: return $info; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * @return \Drupal\Core\Render\Element\ElementInterface Chris@0: */ Chris@0: public function createInstance($plugin_id, array $configuration = []) { Chris@0: return parent::createInstance($plugin_id, $configuration); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function clearCachedDefinitions() { Chris@0: $this->elementInfo = NULL; Chris@0: $this->cacheTagInvalidator->invalidateTags(['element_info_build']); Chris@0: Chris@0: parent::clearCachedDefinitions(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the CID used to cache the element info. Chris@0: * Chris@0: * @param string $theme_name Chris@0: * The theme name. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: protected function getCid($theme_name) { Chris@0: return 'element_info_build:' . $theme_name; Chris@0: } Chris@0: Chris@0: }