diff --git a/core/modules/rest/src/Plugin/views/style/Serializer.php b/core/modules/rest/src/Plugin/views/style/Serializer.php index 67a8364f618..f83d57fc0ae 100644 --- a/core/modules/rest/src/Plugin/views/style/Serializer.php +++ b/core/modules/rest/src/Plugin/views/style/Serializer.php @@ -122,9 +122,11 @@ class Serializer extends StylePluginBase implements CacheablePluginInterface { // which will transform it to arrays/scalars. If the Data field row plugin // is used, $rows will not contain objects and will pass directly to the // Encoder. - foreach ($this->view->result as $row) { + foreach ($this->view->result as $row_index => $row) { + $this->view->row_index = $row_index; $rows[] = $this->view->rowPlugin->render($row); } + unset($this->view->row_index); // Get the content type configured in the display or fallback to the // default. diff --git a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php index 498942f6bf2..c96c275793f 100644 --- a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php +++ b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php @@ -9,7 +9,10 @@ namespace Drupal\rest\Tests\Views; use Drupal\Component\Utility\Html; use Drupal\Core\Cache\Cache; +use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\entity_test\Entity\EntityTest; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait; use Drupal\views\Entity\View; use Drupal\views\Plugin\views\display\DisplayPluginBase; @@ -544,4 +547,67 @@ class StyleSerializerTest extends PluginTestBase { $this->assertEqual($result[1]['nid'], $node->id()); $this->assertTrue(strpos($this->getRawContent(), "container->get('renderer'); + $this->drupalCreateContentType(['type' => 'page']); + // Create a text field with cardinality set to unlimited. + $field_name = 'field_group_rows'; + $field_storage = FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'node', + 'type' => 'string', + 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, + ]); + $field_storage->save(); + // Create an instance of the text field on the content type. + $field = FieldConfig::create([ + 'field_storage' => $field_storage, + 'bundle' => 'page', + ]); + $field->save(); + $grouped_field_values = ['a', 'b', 'c']; + $edit = [ + 'title' => $this->randomMachineName(), + $field_name => $grouped_field_values, + ]; + $this->drupalCreateNode($edit); + $view = Views::getView('test_serializer_node_display_field'); + $view->setDisplay('rest_export_1'); + // Override the view's fields to include the field_group_rows field, set the + // group_rows setting to true. + $fields = [ + $field_name => [ + 'id' => $field_name, + 'table' => 'node__' . $field_name, + 'field' => $field_name, + 'type' => 'string', + 'group_rows' => TRUE, + ], + ]; + $view->displayHandlers->get('default')->overrideOption('fields', $fields); + $build = $view->preview(); + // Get the serializer service. + $serializer = $this->container->get('serializer'); + // Check if the field_group_rows field is grouped. + $expected = []; + $expected[] = [$field_name => implode(', ', $grouped_field_values)]; + $this->assertEqual($serializer->serialize($expected, 'json'), (string) $renderer->renderRoot($build)); + // Set the group rows setting to false. + $view = Views::getView('test_serializer_node_display_field'); + $view->setDisplay('rest_export_1'); + $fields[$field_name]['group_rows'] = FALSE; + $view->displayHandlers->get('default')->overrideOption('fields', $fields); + $build = $view->preview(); + // Check if the field_group_rows field is ungrouped and displayed per row. + $expected = []; + foreach ($grouped_field_values as $grouped_field_value) { + $expected[] = [$field_name => $grouped_field_value]; + } + $this->assertEqual($serializer->serialize($expected, 'json'), (string) $renderer->renderRoot($build)); + } }