EntityDeriver.php

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

Namespace

Drupal\rest\Plugin\Deriver

File

core/modules/rest/src/Plugin/Deriver/EntityDeriver.php

View source
<?php

namespace Drupal\rest\Plugin\Deriver;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a resource plugin definition for every entity type.
 *
 * @see \Drupal\rest\Plugin\rest\resource\EntityResource
 */
class EntityDeriver implements ContainerDeriverInterface {
    
    /**
     * List of derivative definitions.
     *
     * @var array
     */
    protected $derivatives;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Constructs an EntityDeriver object.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager) {
        $this->entityTypeManager = $entity_type_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, $base_plugin_id) {
        return new static($container->get('entity_type.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->entityTypeManager
                ->getDefinitions() as $entity_type_id => $entity_type) {
                if ($entity_type->isInternal()) {
                    continue;
                }
                $this->derivatives[$entity_type_id] = [
                    'id' => 'entity:' . $entity_type_id,
                    'entity_type' => $entity_type_id,
                    'serialization_class' => $entity_type->getClass(),
                    'label' => $entity_type->getLabel(),
                ];
                $default_uris = [
                    'canonical' => "/entity/{$entity_type_id}/" . '{' . $entity_type_id . '}',
                    '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;
    }

}

Classes

Title Deprecated Summary
EntityDeriver Provides a resource plugin definition for every entity type.

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