Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\rest\Plugin\Deriver;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityManagerInterface;
|
Chris@0
|
6 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
|
Chris@0
|
7 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Provides a resource plugin definition for every entity type.
|
Chris@0
|
11 *
|
Chris@0
|
12 * @see \Drupal\rest\Plugin\rest\resource\EntityResource
|
Chris@0
|
13 */
|
Chris@0
|
14 class EntityDeriver implements ContainerDeriverInterface {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * List of derivative definitions.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var array
|
Chris@0
|
20 */
|
Chris@0
|
21 protected $derivatives;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * The entity manager.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var \Drupal\Core\Entity\EntityManagerInterface
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $entityManager;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Constructs an EntityDeriver object.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
Chris@0
|
34 * The entity manager.
|
Chris@0
|
35 */
|
Chris@0
|
36 public function __construct(EntityManagerInterface $entity_manager) {
|
Chris@0
|
37 $this->entityManager = $entity_manager;
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * {@inheritdoc}
|
Chris@0
|
42 */
|
Chris@0
|
43 public static function create(ContainerInterface $container, $base_plugin_id) {
|
Chris@0
|
44 return new static(
|
Chris@0
|
45 $container->get('entity.manager')
|
Chris@0
|
46 );
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * {@inheritdoc}
|
Chris@0
|
51 */
|
Chris@0
|
52 public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
|
Chris@0
|
53 if (!isset($this->derivatives)) {
|
Chris@0
|
54 $this->getDerivativeDefinitions($base_plugin_definition);
|
Chris@0
|
55 }
|
Chris@0
|
56 if (isset($this->derivatives[$derivative_id])) {
|
Chris@0
|
57 return $this->derivatives[$derivative_id];
|
Chris@0
|
58 }
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * {@inheritdoc}
|
Chris@0
|
63 */
|
Chris@0
|
64 public function getDerivativeDefinitions($base_plugin_definition) {
|
Chris@0
|
65 if (!isset($this->derivatives)) {
|
Chris@0
|
66 // Add in the default plugin configuration and the resource type.
|
Chris@0
|
67 foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
|
Chris@14
|
68 if ($entity_type->isInternal()) {
|
Chris@14
|
69 continue;
|
Chris@14
|
70 }
|
Chris@14
|
71
|
Chris@0
|
72 $this->derivatives[$entity_type_id] = [
|
Chris@0
|
73 'id' => 'entity:' . $entity_type_id,
|
Chris@0
|
74 'entity_type' => $entity_type_id,
|
Chris@0
|
75 'serialization_class' => $entity_type->getClass(),
|
Chris@0
|
76 'label' => $entity_type->getLabel(),
|
Chris@0
|
77 ];
|
Chris@0
|
78
|
Chris@0
|
79 $default_uris = [
|
Chris@0
|
80 'canonical' => "/entity/$entity_type_id/" . '{' . $entity_type_id . '}',
|
Chris@0
|
81 'create' => "/entity/$entity_type_id",
|
Chris@0
|
82 ];
|
Chris@0
|
83
|
Chris@0
|
84 foreach ($default_uris as $link_relation => $default_uri) {
|
Chris@0
|
85 // Check if there are link templates defined for the entity type and
|
Chris@0
|
86 // use the path from the route instead of the default.
|
Chris@0
|
87 if ($link_template = $entity_type->getLinkTemplate($link_relation)) {
|
Chris@0
|
88 $this->derivatives[$entity_type_id]['uri_paths'][$link_relation] = $link_template;
|
Chris@0
|
89 }
|
Chris@0
|
90 else {
|
Chris@0
|
91 $this->derivatives[$entity_type_id]['uri_paths'][$link_relation] = $default_uri;
|
Chris@0
|
92 }
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 $this->derivatives[$entity_type_id] += $base_plugin_definition;
|
Chris@0
|
96 }
|
Chris@0
|
97 }
|
Chris@0
|
98 return $this->derivatives;
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 }
|