diff --git a/core/modules/rest/src/Plugin/views/style/Serializer.php b/core/modules/rest/src/Plugin/views/style/Serializer.php index 2c1a28b..c3f823f 100644 --- a/core/modules/rest/src/Plugin/views/style/Serializer.php +++ b/core/modules/rest/src/Plugin/views/style/Serializer.php @@ -9,6 +9,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Routing\UrlGeneratorInterface; +use Drupal\Core\State\StateInterface; use Drupal\serialization\Collection; use Drupal\views\ViewExecutable; use Drupal\views\Plugin\views\display\DisplayPluginBase; @@ -62,6 +63,13 @@ class Serializer extends StylePluginBase { protected $urlGenerator; /** + * The state service. + * + * @var \Drupal\Core\State\StateInterface + */ + protected $state; + + /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { @@ -71,20 +79,22 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_definition, $container->get('serializer'), $container->getParameter('serializer.formats'), - $container->get('url_generator') + $container->get('url_generator'), + $container->get('state') ); } /** * Constructs a Plugin object. */ - public function __construct(array $configuration, $plugin_id, array $plugin_definition, SerializerInterface $serializer, array $serializer_formats, UrlGeneratorInterface $url_generator) { + public function __construct(array $configuration, $plugin_id, array $plugin_definition, SerializerInterface $serializer, array $serializer_formats, UrlGeneratorInterface $url_generator, StateInterface $state) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->definition = $plugin_definition + $configuration; $this->serializer = $serializer; $this->formats = $serializer_formats; $this->urlGenerator = $url_generator; + $this->state = $state; } /** @@ -155,11 +165,6 @@ public function getFormats() { public function getCollection() { $this->view = $this->view; - $rows = array(); - foreach ($this->view->result as $row) { - $rows[] = $this->view->rowPlugin->render($row); - } - $display = $this->view->getDisplay(); // Build full view-display id. $view_id = $this->view->storage->id(); @@ -172,8 +177,7 @@ public function getCollection() { $collection->setDescription($display->getOption('display_description')); // Route as defined in e.g. \Drupal\rest\Plugin\views\display\RestExport. - $state = \Drupal::state(); - $route_names = $state->get('views.view_route_names'); + $route_names = $this->state->get('views.view_route_names'); $route_name = $route_names["$view_id.$display_id"]; // Get base url path for the view; getUrl returns a path not an absolute @@ -187,8 +191,12 @@ public function getCollection() { else { $uri = $this->urlGenerator->generateFromRoute($route_name, array(), array('absolute' => TRUE)); } - $collection->setUri($uri); + + $rows = array(); + foreach ($this->view->result as $row) { + $rows[] = $this->view->rowPlugin->render($row); + } $collection->setItems($rows); $pager = $this->view->getPager(); diff --git a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php index 676cf8b..735bf06 100644 --- a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php +++ b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php @@ -91,7 +91,7 @@ protected function setUp() { * @param string $path * Path to request HAL+JSON from. * @param array $options - * Array of options to pass to url(). + * Array of options to pass to the URL generator. * @param array $headers * Array of headers. * @@ -176,13 +176,22 @@ public function testSerializerResponses() { $this->assertIdentical(Json::encode($actual_json), $expected, 'The expected HAL output was found.'); // Make assertions on the structure of the response. - $this->assertTrue(isset($actual_json['_embedded']) && isset($actual_json['_links']), 'Has _links and _embedded keys'); + $this->assertTrue(isset($actual_json['_embedded']), 'Has _embedded key.'); + $this->assertTrue(isset($actual_json['_links']), 'Has _links key.'); $this->assertEqual(count($actual_json['_embedded']['item']), 10); $this->assertEqual($actual_json['_links']['self']['href'], $this->viewUrl($view)); $this->assertEqual(array_keys($actual_json['_links']), array('self')); } + /** + * Build an absolute URL from a view path, accounting for paging. + * + * @param ViewExecutable $view + * @param null $page + * + * @return string + */ protected function viewUrl(ViewExecutable $view, $page = NULL) { $base_url = 'base://' . $view->getUrl(); $options = array( diff --git a/core/modules/serialization/src/Collection.php b/core/modules/serialization/src/Collection.php index e837b87..451af3d 100644 --- a/core/modules/serialization/src/Collection.php +++ b/core/modules/serialization/src/Collection.php @@ -19,9 +19,9 @@ class Collection implements \IteratorAggregate { protected $id; /** - * The entities in the collection. + * The items of the collection. * - * var array + * @var array */ protected $items; @@ -188,7 +188,7 @@ public function getLink($type) { * @param array $links * Associative array of link relation types and URIs. */ - public function setLinks($links) { + public function setLinks(array $links) { $this->links = $links; } @@ -209,6 +209,7 @@ public function getLinks() { * True if link relations have been added. */ public function hasLinks() { - return is_array($this->links) && count($this->links); + // $this->links should be an array, so no type checking is needed. + return (bool) count($this->links); } } diff --git a/core/modules/serialization/src/Tests/CollectionTest.php b/core/modules/serialization/src/Tests/CollectionTest.php index 53a7103..cedaddb 100644 --- a/core/modules/serialization/src/Tests/CollectionTest.php +++ b/core/modules/serialization/src/Tests/CollectionTest.php @@ -45,17 +45,17 @@ public function testConstructor() { $collection = new Collection($collection_id); $this->assertSame($collection_id, $collection->getCollectionId(), 'Id has been set accordingly'); - $this->assertEquals($collection->getTitle(), NULL, 'Collection title is not set'); + $this->assertEquals(NULL, $collection->getTitle(), 'Collection title is not set'); $collection->setTitle($collection_id); - $this->assertEquals($collection->getTitle(), $collection_id, 'Collection title has been set'); + $this->assertEquals($collection_id, $collection->getTitle(), 'Collection title has been set'); - $this->assertEquals($collection->getUri(), NULL, 'Collection URI is not set'); - $collection->setURI('http://example.com/' . $collection_id); - $this->assertEquals($collection->getUri(), 'http://example.com/' . $collection_id, 'Collection URI has been set'); + $this->assertEquals(NULL, $collection->getUri(), 'Collection URI is not set'); + $collection->setURI("http://example.com/$collection_id"); + $this->assertEquals("http://example.com/$collection_id", $collection->getUri(), 'Collection URI has been set'); - $this->assertEquals($collection->getDescription(), NULL, 'Collection description is not set'); + $this->assertEquals(NULL, $collection->getDescription(), 'Collection description is not set'); $collection->setDescription($collection_id); - $this->assertEquals($collection->getDescription(), $collection_id, 'Collection description has been set'); + $this->assertEquals($collection_id, $collection->getDescription(), 'Collection description has been set'); } /**