3 namespace Drupal\rest\Plugin\Deriver;
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
10 * Provides a resource plugin definition for every entity type.
12 * @see \Drupal\rest\Plugin\rest\resource\EntityResource
14 class EntityDeriver implements ContainerDeriverInterface {
17 * List of derivative definitions.
21 protected $derivatives;
26 * @var \Drupal\Core\Entity\EntityManagerInterface
28 protected $entityManager;
31 * Constructs an EntityDeriver object.
33 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
36 public function __construct(EntityManagerInterface $entity_manager) {
37 $this->entityManager = $entity_manager;
43 public static function create(ContainerInterface $container, $base_plugin_id) {
45 $container->get('entity.manager')
52 public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
53 if (!isset($this->derivatives)) {
54 $this->getDerivativeDefinitions($base_plugin_definition);
56 if (isset($this->derivatives[$derivative_id])) {
57 return $this->derivatives[$derivative_id];
64 public function getDerivativeDefinitions($base_plugin_definition) {
65 if (!isset($this->derivatives)) {
66 // Add in the default plugin configuration and the resource type.
67 foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
68 $this->derivatives[$entity_type_id] = [
69 'id' => 'entity:' . $entity_type_id,
70 'entity_type' => $entity_type_id,
71 'serialization_class' => $entity_type->getClass(),
72 'label' => $entity_type->getLabel(),
76 'canonical' => "/entity/$entity_type_id/" . '{' . $entity_type_id . '}',
77 'create' => "/entity/$entity_type_id",
80 foreach ($default_uris as $link_relation => $default_uri) {
81 // Check if there are link templates defined for the entity type and
82 // use the path from the route instead of the default.
83 if ($link_template = $entity_type->getLinkTemplate($link_relation)) {
84 $this->derivatives[$entity_type_id]['uri_paths'][$link_relation] = $link_template;
87 $this->derivatives[$entity_type_id]['uri_paths'][$link_relation] = $default_uri;
91 $this->derivatives[$entity_type_id] += $base_plugin_definition;
94 return $this->derivatives;