EntityManager.php 9.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * @file
 * Contains \Drupal\Core\Entity\EntityManager.
 */

namespace Drupal\Core\Entity;

use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Factory\DefaultFactory;
12
use Drupal\Core\Language\Language;
13
use Drupal\Core\Plugin\Discovery\AlterDecorator;
14
use Drupal\Core\Plugin\Discovery\CacheDecorator;
15
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
16
use Drupal\Core\Plugin\Discovery\InfoHookDecorator;
17
use Drupal\Core\Cache\CacheBackendInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19 20 21 22

/**
 * Manages entity type plugin definitions.
 *
23 24
 * Each entity type definition array is set in the entity type's
 * annotation and altered by hook_entity_info_alter().
25 26 27 28
 *
 * The defaults for the plugin definition are provided in
 * \Drupal\Core\Entity\EntityManager::defaults.
 *
29 30
 * @see \Drupal\Core\Entity\Annotation\EntityType
 * @see \Drupal\Core\Entity\EntityInterface
31 32 33 34 35
 * @see entity_get_info()
 * @see hook_entity_info_alter()
 */
class EntityManager extends PluginManagerBase {

36 37 38 39 40 41 42
  /**
   * The injection container that should be passed into the controller factory.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

43 44 45 46 47 48 49
  /**
   * Contains instantiated controllers keyed by controller type and entity type.
   *
   * @var array
   */
  protected $controllers = array();

50 51
  /**
   * Constructs a new Entity plugin manager.
52
   *
53 54 55
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace to look for plugin implementations,
56 57
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service container this object should use.
58
   */
59
  public function __construct(\Traversable $namespaces, ContainerInterface $container) {
60
    // Allow the plugin definition to be altered by hook_entity_info_alter().
61 62 63
    $annotation_namespaces = array(
      'Drupal\Core\Entity\Annotation' => DRUPAL_ROOT . '/core/lib',
    );
64
    $this->discovery = new AnnotatedClassDiscovery('Core/Entity', $namespaces, $annotation_namespaces, 'Drupal\Core\Entity\Annotation\EntityType');
65
    $this->discovery = new InfoHookDecorator($this->discovery, 'entity_info');
66
    $this->discovery = new AlterDecorator($this->discovery, 'entity_info');
67
    $this->discovery = new CacheDecorator($this->discovery, 'entity_info:' . language(Language::TYPE_INTERFACE)->langcode, 'cache', CacheBackendInterface::CACHE_PERMANENT, array('entity_info' => TRUE));
68

69
    $this->factory = new DefaultFactory($this->discovery);
70
    $this->container = $container;
71 72
  }

73 74 75 76 77 78 79 80 81 82 83 84 85
  /**
   * Checks whether a certain entity type has a certain controller.
   *
   * @param string $entity_type
   *   The name of the entity type.
   * @param string $controller_type
   *   The name of the controller.
   *
   * @return bool
   *   Returns TRUE if the entity type has the controller, else FALSE.
   */
  public function hasController($entity_type, $controller_type) {
    $definition = $this->getDefinition($entity_type);
86
    return !empty($definition['controllers'][$controller_type]);
87 88
  }

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  /**
   * Returns an entity controller class.
   *
   * @param string $entity_type
   *   The name of the entity type
   * @param string $controller_type
   *   The name of the controller.
   * @param string|null $nested
   *   (optional) If this controller definition is nested, the name of the key.
   *   Defaults to NULL.
   *
   * @return string
   *   The class name for this controller instance.
   */
  public function getControllerClass($entity_type, $controller_type, $nested = NULL) {
    $definition = $this->getDefinition($entity_type);
105
    $definition = $definition['controllers'];
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    if (empty($definition[$controller_type])) {
      throw new \InvalidArgumentException(sprintf('The entity (%s) did not specify a %s.', $entity_type, $controller_type));
    }

    $class = $definition[$controller_type];

    // Some class definitions can be nested.
    if (isset($nested)) {
      if (empty($class[$nested])) {
        throw new \InvalidArgumentException(sprintf("Missing '%s: %s' for entity '%s'", $controller_type, $nested, $entity_type));
      }

      $class = $class[$nested];
    }

    if (!class_exists($class)) {
      throw new \InvalidArgumentException(sprintf('Entity (%s) %s "%s" does not exist.', $entity_type, $controller_type, $class));
    }

    return $class;
  }

  /**
   * Creates a new storage controller instance.
   *
   * @param string $entity_type
   *   The entity type for this storage controller.
   *
   * @return \Drupal\Core\Entity\EntityStorageControllerInterface
   *   A storage controller instance.
   */
  public function getStorageController($entity_type) {
    if (!isset($this->controllers['storage'][$entity_type])) {
139
      $class = $this->getControllerClass($entity_type, 'storage');
140 141 142 143 144 145
      if (in_array('Drupal\Core\Entity\EntityControllerInterface', class_implements($class))) {
        $this->controllers['storage'][$entity_type] = $class::createInstance($this->container, $entity_type, $this->getDefinition($entity_type));
      }
      else {
        $this->controllers['storage'][$entity_type] = new $class($entity_type);
      }
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    }
    return $this->controllers['storage'][$entity_type];
  }

  /**
   * Creates a new list controller instance.
   *
   * @param string $entity_type
   *   The entity type for this list controller.
   *
   * @return \Drupal\Core\Entity\EntityListControllerInterface
   *   A list controller instance.
   */
  public function getListController($entity_type) {
    if (!isset($this->controllers['listing'][$entity_type])) {
161
      $class = $this->getControllerClass($entity_type, 'list');
162 163 164 165 166 167
      if (in_array('Drupal\Core\Entity\EntityControllerInterface', class_implements($class))) {
        $this->controllers['listing'][$entity_type] = $class::createInstance($this->container, $entity_type, $this->getDefinition($entity_type));
      }
      else {
        $this->controllers['listing'][$entity_type] = new $class($entity_type, $this->getStorageController($entity_type));
      }
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    }
    return $this->controllers['listing'][$entity_type];
  }

  /**
   * Creates a new form controller instance.
   *
   * @param string $entity_type
   *   The entity type for this form controller.
   * @param string $operation
   *   The name of the operation to use, e.g., 'default'.
   *
   * @return \Drupal\Core\Entity\EntityFormControllerInterface
   *   A form controller instance.
   */
  public function getFormController($entity_type, $operation) {
    if (!isset($this->controllers['form'][$operation][$entity_type])) {
185
      $class = $this->getControllerClass($entity_type, 'form', $operation);
186 187
      if (in_array('Drupal\Core\Entity\EntityControllerInterface', class_implements($class))) {
        $this->controllers['form'][$operation][$entity_type] = $class::createInstance($this->container, $entity_type, $this->getDefinition($entity_type));
188
        $this->controllers['form'][$operation][$entity_type]->setOperation($operation);
189 190 191 192
      }
      else {
        $this->controllers['form'][$operation][$entity_type] = new $class($operation);
      }
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    }
    return $this->controllers['form'][$operation][$entity_type];
  }

  /**
   * Creates a new render controller instance.
   *
   * @param string $entity_type
   *   The entity type for this render controller.
   *
   * @return \Drupal\Core\Entity\EntityRenderControllerInterface.
   *   A render controller instance.
   */
  public function getRenderController($entity_type) {
    if (!isset($this->controllers['render'][$entity_type])) {
208
      $class = $this->getControllerClass($entity_type, 'render');
209
      if (in_array('Drupal\Core\Entity\EntityControllerInterface', class_implements($class))) {
210
        $this->controllers['render'][$entity_type] = $class::createInstance($this->container, $this->getDefinition($entity_type), $entity_type);
211 212 213 214
      }
      else {
        $this->controllers['render'][$entity_type] = new $class($entity_type);
      }
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    }
    return $this->controllers['render'][$entity_type];
  }

  /**
   * Creates a new access controller instance.
   *
   * @param string $entity_type
   *   The entity type for this access controller.
   *
   * @return \Drupal\Core\Entity\EntityRenderControllerInterface.
   *   A access controller instance.
   */
  public function getAccessController($entity_type) {
    if (!isset($this->controllers['access'][$entity_type])) {
230
      $class = $this->getControllerClass($entity_type, 'access');
231 232 233 234 235 236
      if (in_array('Drupal\Core\Entity\EntityControllerInterface', class_implements($class))) {
        $this->controllers['access'][$entity_type] = $class::createInstance($this->container, $entity_type, $this->getDefinition($entity_type));
      }
      else {
        $this->controllers['access'][$entity_type] = new $class($entity_type);
      }
237 238 239 240
    }
    return $this->controllers['access'][$entity_type];
  }

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  /**
   * Returns the administration path for an entity type's bundle.
   *
   * @param string $entity_type
   *   The entity type.
   * @param string $bundle
   *   The name of the bundle.
   *
   * @return string
   *   The administration path for an entity type bundle, if it exists.
   */
  public function getAdminPath($entity_type, $bundle) {
    $admin_path = '';
    $entity_info = $this->getDefinition($entity_type);
    // Check for an entity type's admin base path.
    if (isset($entity_info['route_base_path'])) {
      // If the entity type has a bundle prefix, strip it out of the path.
      if (isset($entity_info['bundle_prefix'])) {
        $bundle = str_replace($entity_info['bundle_prefix'], '', $bundle);
      }
      // Replace any dynamic 'bundle' portion of the path with the actual bundle.
      $admin_path = str_replace('{bundle}', $bundle, $entity_info['route_base_path']);
    }

    return $admin_path;
  }

268
}