diff --git a/core/modules/rest/src/Entity/RestEndpoint.php b/core/modules/rest/src/Entity/RestEndpoint.php index f1b7a30..6d07a20 100644 --- a/core/modules/rest/src/Entity/RestEndpoint.php +++ b/core/modules/rest/src/Entity/RestEndpoint.php @@ -8,6 +8,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Entity\Entity; use Drupal\Core\Plugin\DefaultLazyPluginCollection; +use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection; use Drupal\rest\RestEndpointInterface; /** @@ -219,9 +220,8 @@ public function removeSupportedFormat($method, $format) { * store their configuration. */ public function getPluginCollections() { - $plugin_configuration = [ $this->getResourcePluginID() => [ 'id' => $this->getResourcePluginID() ] ]; return [ - 'resource' => new DefaultLazyPluginCollection($this->pluginManager, $plugin_configuration) + 'resource' => new DefaultSingleLazyPluginCollection($this->pluginManager, $this->getResourcePluginID(), []) ]; } diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php index 2c5eea9..7dcec34 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php @@ -7,11 +7,15 @@ namespace Drupal\rest\Plugin\rest\resource; +use Drupal\Component\Plugin\DependentPluginInterface; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityStorageException; use Drupal\rest\Plugin\ResourceBase; use Drupal\rest\ResourceResponse; use Drupal\Component\Utility\SafeMarkup; +use Psr\Log\LoggerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\HttpException; @@ -32,7 +36,49 @@ * * @see \Drupal\rest\Plugin\Derivative\EntityDerivative */ -class EntityResource extends ResourceBase { +class EntityResource extends ResourceBase implements DependentPluginInterface { + /** + * Definition of the resources entity type. + * + * @var \Drupal\Core\Entity\EntityTypeInterface + */ + protected $entity_type_definition; + + /** + * Constructs a Drupal\rest\Plugin\rest\resource\EntityResource object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager + * The entity manager + * @param array $serializer_formats + * The available serialization formats. + * @param \Psr\Log\LoggerInterface $logger + * A logger instance. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, $serializer_formats, + LoggerInterface $logger) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger); + $this->entity_type_definition = $entity_manager->getDefinition($plugin_definition['entity_type']); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity.manager'), + $container->getParameter('serializer.formats'), + $container->get('logger.factory')->get('rest') + ); + } /** * Responds to entity GET requests. @@ -232,5 +278,12 @@ protected function getBaseRoute($canonical_path, $method) { return $route; } - + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + if (isset($this->entity_type_definition)) { + return [ 'module' => [ $this->entity_type_definition->getProvider() ] ]; + } + } }