diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/display/Data.php b/core/modules/views/lib/Drupal/views/Plugin/views/display/Data.php index 1e5433e..323c7c9 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/display/Data.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/display/Data.php @@ -2,7 +2,7 @@ /** * @file - * Contains Drupal\views\Plugin\views\display\Data. + * Contains \Drupal\views\Plugin\views\display\Data. */ namespace Drupal\views\Plugin\views\display; @@ -10,6 +10,7 @@ use Symfony\Component\HttpFoundation\Response; use Drupal\Core\Annotation\Plugin; use Drupal\Core\Annotation\Translation; +use Drupal\views\ViewExecutable; /** * The plugin that handles a Data response callbacks. @@ -27,51 +28,74 @@ class Data extends PathPluginBase { /** - * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAJAX. + * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAJAX. */ protected $usesAJAX = FALSE; /** - * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::$usesPager. + * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesPager. */ protected $usesPager = FALSE; /** - * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::$usesMore. + * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesMore. */ protected $usesMore = FALSE; /** - * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAreas. + * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAreas. */ protected $usesAreas = FALSE; /** - * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAreas. + * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::$usesAreas. */ protected $usesOptions = FALSE; /** * Overrides the content type of the data response, if needed. + * + * @var string */ protected $contentType; /** - * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::getStyleType(). + * The mime type for the response. + * + * @var string + */ + protected $mimeType; + + /** + * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::initDisplay(). + */ + public function initDisplay(ViewExecutable $view, array &$display, array &$options = NULL) { + parent::initDisplay($view, $display, $options); + + $container = drupal_container(); + $negotiation = $container->get('content_negotiation'); + $request = $container->get('request'); + + $this->contentType = $negotiation->getContentType($request); + $this->mimeType = $request->getMimeType($this->contentType); + } + + /** + * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::getStyleType(). */ protected function getStyleType() { return 'data'; } /** - * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::usesExposed(). + * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::usesExposed(). */ public function usesExposed() { return FALSE; } /** - * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::displaysExposed(). + * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::displaysExposed(). */ public function displaysExposed() { return FALSE; @@ -79,32 +103,49 @@ public function displaysExposed() { /** * Sets the request content type. + * + * @var string $mime_type + * The response mime type. E.g. 'application/json'. */ - public function setContentType($content_type) { - $this->contentType = $content_type; + public function setMimeType($mime_type) { + $this->mimeType = $mime_type; } /** - * Gets the request content type. + * Gets the mime type. * - * This will return the overriden content type if set, otherwise returns the + * This will return any overriden content type, otherwise returns the * content type from the request. + * + * @var string + * The response mime type. E.g. 'application/json'. */ - public function getContentType() { - if (!isset($this->contentType)) { - // Return the content type based on the request object. - $negotiation = drupal_container()->get('content_negotiation'); - $request = drupal_container()->get('request'); - $content_type = $negotiation->getContentType($request); + public function getMimeType() { + return $this->mimeType; + } - $this->setContentType($request->getMimeType($content_type)); - } + /** + * Sets the content type. + * + * @var string $content_type + * The content type machine name. E.g. 'json'. + */ + public function setContentType($content_type) { + $this->contentType = $content_type; + } + /** + * Gets the content type. + * + * @return string + * The content type machine name. E.g. 'json'. + */ + public function getContentType() { return $this->contentType; } /** - * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::defineOptions(). + * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::defineOptions(). */ protected function defineOptions() { $options = parent::defineOptions(); @@ -124,7 +165,7 @@ protected function defineOptions() { } /** - * Overrides Drupal\views\Plugin\views\display\PathPluginBase::optionsSummary(). + * Overrides \Drupal\views\Plugin\views\display\PathPluginBase::optionsSummary(). */ public function optionsSummary(&$categories, &$options) { parent::optionsSummary($categories, $options); @@ -153,20 +194,16 @@ public function optionsSummary(&$categories, &$options) { /** - * Overrides Drupal\views\Plugin\views\display\PathPluginBase::execute(). + * Overrides \Drupal\views\Plugin\views\display\PathPluginBase::execute(). */ public function execute() { parent::execute(); - $output = $this->view->render(); - - $response = new Response($output, 200, array('Content-type' => $this->getContentType())); - - return $response; + return new Response($this->view->render(), 200, array('Content-type' => $this->getMimeType())); } /** - * Overrides Drupal\views\Plugin\views\display\DisplayPluginBase::render(). + * Overrides \Drupal\views\Plugin\views\display\DisplayPluginBase::render(). */ public function render() { $output = $this->view->style_plugin->render(); diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/row/DataEntityRow.php b/core/modules/views/lib/Drupal/views/Plugin/views/row/DataEntityRow.php index c635e5e..3da5533 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/row/DataEntityRow.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/row/DataEntityRow.php @@ -2,7 +2,7 @@ /** * @file - * Contains Drupal\views\Plugin\views\row\DataEntityRow. + * Contains \Drupal\views\Plugin\views\row\DataEntityRow. */ namespace Drupal\views\Plugin\views\row; @@ -27,12 +27,12 @@ class DataEntityRow extends RowPluginBase { /** - * Overrides Drupal\views\Plugin\Plugin::$usesOptions. + * Overrides \Drupal\views\Plugin\Plugin::$usesOptions. */ protected $usesOptions = FALSE; /** - * Overrides Drupal\views\Plugin\views\row\RowPluginBase::render(). + * Overrides \Drupal\views\Plugin\views\row\RowPluginBase::render(). */ public function render($row) { return $row->_entity; diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/row/DataFieldRow.php b/core/modules/views/lib/Drupal/views/Plugin/views/row/DataFieldRow.php index 6b768fe..056d04b 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/row/DataFieldRow.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/row/DataFieldRow.php @@ -2,7 +2,7 @@ /** * @file - * Contains Drupal\views\Plugin\views\row\DataFieldRow. + * Contains \Drupal\views\Plugin\views\row\DataFieldRow. */ namespace Drupal\views\Plugin\views\row; diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/style/Serialize.php b/core/modules/views/lib/Drupal/views/Plugin/views/style/Serialize.php index 0b6bbc2..1fb1053 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/style/Serialize.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/style/Serialize.php @@ -2,7 +2,7 @@ /** * @file - * Contains Drupal\views\Plugin\views\style\Serialize. + * Contains \Drupal\views\Plugin\views\style\Serialize. */ namespace Drupal\views\Plugin\views\style; @@ -27,7 +27,7 @@ class Serialize extends StylePluginBase { /** - * Overrides Drupal\views\Plugin\views\style\StylePluginBase::$usesRowPlugin. + * Overrides \Drupal\views\Plugin\views\style\StylePluginBase::$usesRowPlugin. */ protected $usesRowPlugin = TRUE; @@ -44,34 +44,17 @@ class Serialize extends StylePluginBase { protected $serializer; /** - * The current active request object. - * - * @var \Symfony\Component\HttpFoundation\Request - */ - protected $request; - - /** - * The current content negotiation object registered on the container. - * - * @var \Drupal\Core\ContentNegotiation - */ - protected $negotiation; - - /** * Overrides \Drupal\views\Plugin\views\style\StylePluginBase::init(). */ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) { parent::init($view, $display, $options); - // Get services for serialization. - $container = drupal_container(); - $this->serializer = $container->get('serializer'); - $this->request = $container->get('request'); - $this->negotiation = $container->get('content_negotiation'); + // Get the serializer service. + $this->serializer = drupal_container()->get('serializer'); } /** - * Overrides Drupal\views\Plugin\views\style\StylePluginBase::render(). + * Overrides \Drupal\views\Plugin\views\style\StylePluginBase::render(). */ public function render() { $rows = array(); @@ -84,10 +67,7 @@ public function render() { $rows[] = $this->row_plugin->render($row); } - $content_type = $this->negotiation->getContentType($this->request); - $this->displayHandler->setContentType($this->request->getMimeType($content_type)); - - return $this->serializer->serialize($rows, $content_type); + return $this->serializer->serialize($rows, $this->displayHandler->getContentType()); } } diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleSerializeTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleSerializeTest.php index 33d74c6..fadba33 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleSerializeTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleSerializeTest.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\views\Tests\Plugin\StyleSerializeTest. + * Contains \Drupal\views\Tests\Plugin\StyleSerializeTest. */ namespace Drupal\views\Tests\Plugin;