class EntityManager

Manages entity type plugin definitions.

Each entity type definition array is set in the entity type's annotation and altered by hook_entity_info_alter().

The defaults for the plugin definition are provided in \Drupal\Core\Entity\EntityManager::defaults.

Hierarchy

Expanded class hierarchy of EntityManager

See also

\Drupal\Core\Entity\Annotation\EntityType

\Drupal\Core\Entity\EntityInterface

entity_get_info()

hook_entity_info_alter()

33 files declare their use of EntityManager
AggregatorController.php in drupal/core/modules/aggregator/lib/Drupal/aggregator/Controller/AggregatorController.php
Contains \Drupal\aggregator\Controller\AggregatorController.
BookManager.php in drupal/core/modules/book/lib/Drupal/book/BookManager.php
Contains \Drupal\book\BookManager.
BreakLockForm.php in drupal/core/modules/views_ui/lib/Drupal/views_ui/Form/BreakLockForm.php
Contains \Drupal\views_ui\Form\BreakLockForm.
BulkFormBase.php in drupal/core/modules/system/lib/Drupal/system/Plugin/views/field/BulkFormBase.php
Contains \Drupal\system\Plugin\views\field\BulkFormBase.
ConfigImporter.php in drupal/core/lib/Drupal/Core/Config/ConfigImporter.php
Contains \Drupal\Core\Config\ConfigImporter.

... See full list

1 string reference to 'EntityManager'
core.services.yml in drupal/core/core.services.yml
drupal/core/core.services.yml
1 service uses EntityManager

File

drupal/core/lib/Drupal/Core/Entity/EntityManager.php, line 34
Contains \Drupal\Core\Entity\EntityManager.

Namespace

Drupal\Core\Entity
View source
class EntityManager extends PluginManagerBase {

  /**
   * The injection container that should be passed into the controller factory.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerInterface
   */
  protected $container;

  /**
   * Contains instantiated controllers keyed by controller type and entity type.
   *
   * @var array
   */
  protected $controllers = array();

  /**
   * Constructs a new Entity plugin manager.
   *
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace to look for plugin implementations,
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   The service container this object should use.
   */
  public function __construct(\Traversable $namespaces, ContainerInterface $container) {

    // Allow the plugin definition to be altered by hook_entity_info_alter().
    $annotation_namespaces = array(
      'Drupal\\Core\\Entity\\Annotation' => DRUPAL_ROOT . '/core/lib',
    );
    $this->discovery = new AnnotatedClassDiscovery('Core/Entity', $namespaces, $annotation_namespaces, 'Drupal\\Core\\Entity\\Annotation\\EntityType');
    $this->discovery = new InfoHookDecorator($this->discovery, 'entity_info');
    $this->discovery = new AlterDecorator($this->discovery, 'entity_info');
    $this->discovery = new CacheDecorator($this->discovery, 'entity_info:' . language(Language::TYPE_INTERFACE)->langcode, 'cache', CacheBackendInterface::CACHE_PERMANENT, array(
      'entity_info' => TRUE,
    ));
    $this->factory = new DefaultFactory($this->discovery);
    $this->container = $container;
  }

  /**
   * 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);
    return !empty($definition['controllers'][$controller_type]);
  }

  /**
   * 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);
    $definition = $definition['controllers'];
    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])) {
      $class = $this
        ->getControllerClass($entity_type, 'storage');
      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);
      }
    }
    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])) {
      $class = $this
        ->getControllerClass($entity_type, 'list');
      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));
      }
    }
    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])) {
      $class = $this
        ->getControllerClass($entity_type, 'form', $operation);
      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));
        $this->controllers['form'][$operation][$entity_type]
          ->setOperation($operation);
      }
      else {
        $this->controllers['form'][$operation][$entity_type] = new $class($operation);
      }
    }
    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])) {
      $class = $this
        ->getControllerClass($entity_type, 'render');
      if (in_array('Drupal\\Core\\Entity\\EntityControllerInterface', class_implements($class))) {
        $this->controllers['render'][$entity_type] = $class::createInstance($this->container, $this
          ->getDefinition($entity_type), $entity_type);
      }
      else {
        $this->controllers['render'][$entity_type] = new $class($entity_type);
      }
    }
    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])) {
      $class = $this
        ->getControllerClass($entity_type, 'access');
      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);
      }
    }
    return $this->controllers['access'][$entity_type];
  }

  /**
   * 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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityManager::$container protected property The injection container that should be passed into the controller factory.
EntityManager::$controllers protected property Contains instantiated controllers keyed by controller type and entity type.
EntityManager::getAccessController public function Creates a new access controller instance.
EntityManager::getAdminPath public function Returns the administration path for an entity type's bundle.
EntityManager::getControllerClass public function Returns an entity controller class.
EntityManager::getFormController public function Creates a new form controller instance.
EntityManager::getListController public function Creates a new list controller instance.
EntityManager::getRenderController public function Creates a new render controller instance.
EntityManager::getStorageController public function Creates a new storage controller instance.
EntityManager::hasController public function Checks whether a certain entity type has a certain controller.
EntityManager::__construct public function Constructs a new Entity plugin manager.
PluginManagerBase::$defaults protected property A set of defaults to be referenced by $this->processDefinition() if additional processing of plugins is necessary or helpful for development purposes. 3
PluginManagerBase::$discovery protected property The object that discovers plugins managed by this manager.
PluginManagerBase::$factory protected property The object that instantiates plugins managed by this manager.
PluginManagerBase::$mapper protected property The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
PluginManagerBase::clearCachedDefinitions public function Clears static and persistent plugin definition caches. Overrides CachedDiscoveryInterface::clearCachedDefinitions
PluginManagerBase::createInstance public function Returns a preconfigured instance of a plugin. Overrides FactoryInterface::createInstance 6
PluginManagerBase::getDefinition public function Gets a specific plugin definition. Overrides DiscoveryInterface::getDefinition
PluginManagerBase::getDefinitions public function Gets the definition of all plugins for this type. Overrides DiscoveryInterface::getDefinitions
PluginManagerBase::getInstance public function Returns a preconfigured instance of a plugin. Overrides MapperInterface::getInstance 6
PluginManagerBase::processDefinition public function Performs extra processing on plugin definitions. 2