You are here

class EntityDeriver in Zircon Profile 8

Same name in this branch
  1. 8 core/modules/rest/src/Plugin/Deriver/EntityDeriver.php \Drupal\rest\Plugin\Deriver\EntityDeriver
  2. 8 core/lib/Drupal/Core/Entity/Plugin/DataType/Deriver/EntityDeriver.php \Drupal\Core\Entity\Plugin\DataType\Deriver\EntityDeriver
Same name and namespace in other branches
  1. 8.0 core/modules/rest/src/Plugin/Deriver/EntityDeriver.php \Drupal\rest\Plugin\Deriver\EntityDeriver

Provides a resource plugin definition for every entity type.

Hierarchy

Expanded class hierarchy of EntityDeriver

See also

\Drupal\rest\Plugin\rest\resource\EntityResource

File

core/modules/rest/src/Plugin/Deriver/EntityDeriver.php, line 19
Contains \Drupal\rest\Plugin\Deriver\EntityDeriver.

Namespace

Drupal\rest\Plugin\Deriver
View source
class EntityDeriver implements ContainerDeriverInterface {

  /**
   * List of derivative definitions.
   *
   * @var array
   */
  protected $derivatives;

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * Constructs an EntityDerivative object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   */
  public function __construct(EntityManagerInterface $entity_manager) {
    $this->entityManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($container
      ->get('entity.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
    if (!isset($this->derivatives)) {
      $this
        ->getDerivativeDefinitions($base_plugin_definition);
    }
    if (isset($this->derivatives[$derivative_id])) {
      return $this->derivatives[$derivative_id];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    if (!isset($this->derivatives)) {

      // Add in the default plugin configuration and the resource type.
      foreach ($this->entityManager
        ->getDefinitions() as $entity_type_id => $entity_type) {
        $this->derivatives[$entity_type_id] = array(
          'id' => 'entity:' . $entity_type_id,
          'entity_type' => $entity_type_id,
          'serialization_class' => $entity_type
            ->getClass(),
          'label' => $entity_type
            ->getLabel(),
        );
        $default_uris = array(
          'canonical' => "/entity/{$entity_type_id}/" . '{' . $entity_type_id . '}',
          'https://www.drupal.org/link-relations/create' => "/entity/{$entity_type_id}",
        );
        foreach ($default_uris as $link_relation => $default_uri) {

          // Check if there are link templates defined for the entity type and
          // use the path from the route instead of the default.
          if ($link_template = $entity_type
            ->getLinkTemplate($link_relation)) {
            $this->derivatives[$entity_type_id]['uri_paths'][$link_relation] = '/' . $link_template;
          }
          else {
            $this->derivatives[$entity_type_id]['uri_paths'][$link_relation] = $default_uri;
          }
        }
        $this->derivatives[$entity_type_id] += $base_plugin_definition;
      }
    }
    return $this->derivatives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityDeriver::$derivatives protected property List of derivative definitions.
EntityDeriver::$entityManager protected property The entity manager.
EntityDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
EntityDeriver::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
EntityDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverInterface::getDerivativeDefinitions
EntityDeriver::__construct public function Constructs an EntityDerivative object.