diff --git a/core/modules/rest/src/Plugin/views/display/RestExport.php b/core/modules/rest/src/Plugin/views/display/RestExport.php index a00dafdc12..85a3abe381 100644 --- a/core/modules/rest/src/Plugin/views/display/RestExport.php +++ b/core/modules/rest/src/Plugin/views/display/RestExport.php @@ -21,9 +21,6 @@ /** * The plugin that handles Data response callbacks for REST resources. * - * Style plugins that work with this class must extend - * \Drupal\rest\Plugin\views\style\Serializer. - * * @ingroup views_display_plugins * * @ViewsDisplay( @@ -34,8 +31,6 @@ * admin = @Translation("REST export"), * returns_response = TRUE * ) - * - * @see \Drupal\rest\Plugin\views\style\Serializer */ class RestExport extends PathPluginBase implements ResponseDisplayPluginInterface { @@ -99,6 +94,13 @@ class RestExport extends PathPluginBase implements ResponseDisplayPluginInterfac */ protected $authenticationProviders; + /** + * The serialization format providers, keyed by format. + * + * @var string[] + */ + protected $formatProviders; + /** * Constructs a RestExport object. * @@ -116,12 +118,15 @@ class RestExport extends PathPluginBase implements ResponseDisplayPluginInterfac * The renderer. * @param string[] $authentication_providers * The authentication providers, keyed by ID. + * @param string[] $serializer_format_providers + * The serialization format providers, keyed by format. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, StateInterface $state, RendererInterface $renderer, array $authentication_providers) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, StateInterface $state, RendererInterface $renderer, array $authentication_providers, array $serializer_format_providers) { parent::__construct($configuration, $plugin_id, $plugin_definition, $route_provider, $state); $this->renderer = $renderer; $this->authenticationProviders = $authentication_providers; + $this->formatProviders = $serializer_format_providers; } /** @@ -135,8 +140,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('router.route_provider'), $container->get('state'), $container->get('renderer'), - $container->getParameter('authentication_providers') - + $container->getParameter('authentication_providers'), + $container->getParameter('serializer.format_providers') ); } /** @@ -334,10 +339,7 @@ public function collectRoutes(RouteCollection $collection) { if ($route = $collection->get("view.$view_id.$display_id")) { $style_plugin = $this->getPlugin('style'); - if (!$style_plugin instanceof Serializer) { - $class_name = get_class($style_plugin); - throw new \Exception("Views style plugins used with REST exports must use be an instance of \\Drupal\\rest\\Plugin\\views\\style\\Serializer, {$class_name} found."); - } + // REST exports should only respond to GET methods. $route->setMethods(['GET']); @@ -346,7 +348,7 @@ public function collectRoutes(RouteCollection $collection) { // If there are no configured formats, add all formats that the serializer // style plugin supports. if (!$formats) { - $formats = $style_plugin->getFormatOptions(); + $formats = $this->getFormatOptions(); } // Format as a string using pipes as a delimiter. @@ -479,4 +481,15 @@ public function calculateDependencies() { return $dependencies; } + /** + * Returns an array of format options. + * + * @return string[] + * An array of format options. Both key and value are the same. + */ + protected function getFormatOptions() { + $formats = array_keys($this->formatProviders); + return array_combine($formats, $formats); + } + } diff --git a/core/modules/rest/src/Plugin/views/style/Serializer.php b/core/modules/rest/src/Plugin/views/style/Serializer.php index c64c5a6a16..895b9fe1ae 100644 --- a/core/modules/rest/src/Plugin/views/style/Serializer.php +++ b/core/modules/rest/src/Plugin/views/style/Serializer.php @@ -12,9 +12,6 @@ /** * The style plugin for serialized output formats. * - * Style plugins must extend this class to work with the - * \Drupal\rest\Plugin\views\display\RestExport display plugin. - * * @ingroup views_style_plugins * * @ViewsStyle( @@ -23,8 +20,6 @@ * help = @Translation("Serializes views row data using the Serializer component."), * display_types = {"data"} * ) - * - * @see \Drupal\rest\Plugin\views\display\RestExport */ class Serializer extends StylePluginBase implements CacheableDependencyInterface { @@ -202,7 +197,7 @@ public function calculateDependencies() { * @return string[] * An array of format options. Both key and value are the same. */ - public function getFormatOptions() { + protected function getFormatOptions() { $formats = array_keys($this->formatProviders); return array_combine($formats, $formats); }