ElementInfoManager.php 5.04 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\Render;

5
use Drupal\Core\Cache\Cache;
6
use Drupal\Core\Cache\CacheBackendInterface;
7
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
8 9 10
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Render\Element\FormElementInterface;
11
use Drupal\Core\Theme\ThemeManagerInterface;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

/**
 * Provides a plugin manager for element plugins.
 *
 * @see \Drupal\Core\Render\Annotation\RenderElement
 * @see \Drupal\Core\Render\Annotation\FormElement
 * @see \Drupal\Core\Render\Element\RenderElement
 * @see \Drupal\Core\Render\Element\FormElement
 * @see \Drupal\Core\Render\Element\ElementInterface
 * @see \Drupal\Core\Render\Element\FormElementInterface
 * @see plugin_api
 */
class ElementInfoManager extends DefaultPluginManager implements ElementInfoManagerInterface {

  /**
   * Stores the available element information.
   *
   * @var array
   */
  protected $elementInfo;

33 34 35 36 37 38 39 40 41 42 43 44 45 46
  /**
   * The theme manager.
   *
   * @var \Drupal\Core\Theme\ThemeManagerInterface
   */
  protected $themeManager;

  /**
   * The cache tag invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheTagInvalidator;

47 48 49 50 51 52 53 54
  /**
   * Constructs a ElementInfoManager object.
   *
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace to look for plugin implementations.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
55 56
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tag_invalidator
   *   The cache tag invalidator.
57 58
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke the alter hook with.
59 60
   * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
   *   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 65
    $this->themeManager = $theme_manager;
    $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 72 73
  }

  /**
   * {@inheritdoc}
   */
  public function getInfo($type) {
74 75 76
    $theme_name = $this->themeManager->getActiveTheme()->getName();
    if (!isset($this->elementInfo[$theme_name])) {
      $this->elementInfo[$theme_name] = $this->buildInfo($theme_name);
77
    }
78
    $info = isset($this->elementInfo[$theme_name][$type]) ? $this->elementInfo[$theme_name][$type] : [];
79 80
    $info['#defaults_loaded'] = TRUE;
    return $info;
81 82
  }

83 84 85 86 87 88 89 90 91
  /**
   * {@inheritdoc}
   */
  public function getInfoProperty($type, $property_name, $default = NULL) {
    $info = $this->getInfo($type);

    return isset($info[$property_name]) ? $info[$property_name] : $default;
  }

92 93
  /**
   * Builds up all element information.
94 95 96 97 98
   *
   * @param string $theme_name
   *   The theme name.
   *
   * @return array
99
   */
100
  protected function buildInfo($theme_name) {
101
    // Get cached definitions.
102 103
    $cid = $this->getCid($theme_name);
    if ($cache = $this->cacheBackend->get($cid)) {
104 105 106 107
      return $cache->data;
    }

    // Otherwise, rebuild and cache.
108
    $info = [];
109 110 111 112 113 114 115 116
    foreach ($this->getDefinitions() as $element_type => $definition) {
      $element = $this->createInstance($element_type);
      $element_info = $element->getInfo();

      // If this is element is to be used exclusively in a form, denote that it
      // will receive input, and assign the value callback.
      if ($element instanceof FormElementInterface) {
        $element_info['#input'] = TRUE;
117
        $element_info['#value_callback'] = [$definition['class'], 'valueCallback'];
118 119 120
      }
      $info[$element_type] = $element_info;
    }
121

122 123 124 125 126
    foreach ($info as $element_type => $element) {
      $info[$element_type]['#type'] = $element_type;
    }
    // Allow modules to alter the element type defaults.
    $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 132 133 134 135 136 137 138
    return $info;
  }

  /**
   * {@inheritdoc}
   *
   * @return \Drupal\Core\Render\Element\ElementInterface
   */
139
  public function createInstance($plugin_id, array $configuration = []) {
140 141 142
    return parent::createInstance($plugin_id, $configuration);
  }

143 144 145 146 147
  /**
   * {@inheritdoc}
   */
  public function clearCachedDefinitions() {
    $this->elementInfo = NULL;
148
    $this->cacheTagInvalidator->invalidateTags(['element_info_build']);
149 150 151 152

    parent::clearCachedDefinitions();
  }

153 154 155 156 157 158 159 160 161 162 163
  /**
   * Returns the CID used to cache the element info.
   *
   * @param string $theme_name
   *   The theme name.
   *
   * @return string
   */
  protected function getCid($theme_name) {
    return 'element_info_build:' . $theme_name;
  }
164

165
}