EntityDeriver.php

Same filename in this branch
  1. main core/modules/rest/src/Plugin/Deriver/EntityDeriver.php
Same filename and directory in other branches
  1. 11.x core/modules/rest/src/Plugin/Deriver/EntityDeriver.php
  2. 11.x core/lib/Drupal/Core/Entity/Plugin/DataType/Deriver/EntityDeriver.php
  3. 10 core/modules/rest/src/Plugin/Deriver/EntityDeriver.php
  4. 10 core/lib/Drupal/Core/Entity/Plugin/DataType/Deriver/EntityDeriver.php
  5. 9 core/modules/rest/src/Plugin/Deriver/EntityDeriver.php
  6. 9 core/lib/Drupal/Core/Entity/Plugin/DataType/Deriver/EntityDeriver.php
  7. 8.9.x core/modules/rest/src/Plugin/Deriver/EntityDeriver.php
  8. 8.9.x core/lib/Drupal/Core/Entity/Plugin/DataType/Deriver/EntityDeriver.php

Namespace

Drupal\Core\Entity\Plugin\DataType\Deriver

File

core/lib/Drupal/Core/Entity/Plugin/DataType/Deriver/EntityDeriver.php

View source
<?php

namespace Drupal\Core\Entity\Plugin\DataType\Deriver;

use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides data type plugins for each existing entity type and bundle.
 */
class EntityDeriver implements ContainerDeriverInterface {
  
  /**
   * List of derivative definitions.
   *
   * @var array
   */
  protected $derivatives = [];
  
  /**
   * Constructs an EntityDeriver object.
   *
   * @param string $basePluginId
   *   The base plugin ID.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundleInfoService
   *   The bundle info service.
   */
  public function __construct(protected $basePluginId, protected EntityTypeManagerInterface $entityTypeManager, protected EntityTypeBundleInfoInterface $bundleInfoService) {
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($base_plugin_id, $container->get('entity_type.manager'), $container->get('entity_type.bundle.info'));
  }
  
  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
    if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
      return $this->derivatives[$derivative_id];
    }
    $this->getDerivativeDefinitions($base_plugin_definition);
    if (isset($this->derivatives[$derivative_id])) {
      return $this->derivatives[$derivative_id];
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    // Also keep the 'entity' defined as is.
    $this->derivatives[''] = $base_plugin_definition;
    // Add definitions for each entity type and bundle.
    foreach ($this->entityTypeManager
      ->getDefinitions() as $entity_type_id => $entity_type) {
      $class = $entity_type->entityClassImplements(ConfigEntityInterface::class) ? ConfigEntityAdapter::class : EntityAdapter::class;
      $this->derivatives[$entity_type_id] = [
        'class' => $class,
        'label' => $entity_type->getLabel(),
        'constraints' => $entity_type->getConstraints(),
        'internal' => $entity_type->isInternal(),
      ] + $base_plugin_definition;
      // Incorporate the bundles as entity:$entity_type:$bundle, if any.
      $bundle_info = $this->bundleInfoService
        ->getBundleInfo($entity_type_id);
      if (count($bundle_info) > 1 || $entity_type->getKey('bundle')) {
        foreach ($bundle_info as $bundle => $info) {
          $this->derivatives[$entity_type_id . ':' . $bundle] = [
            'class' => $class,
            'label' => $info['label'],
            'constraints' => $this->derivatives[$entity_type_id]['constraints'],
          ] + $base_plugin_definition;
        }
      }
    }
    return $this->derivatives;
  }

}

Classes

Title Deprecated Summary
EntityDeriver Provides data type plugins for each existing entity type and bundle.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.