diff --git a/core/modules/rest/src/Plugin/views/row/DataFieldRow.php b/core/modules/rest/src/Plugin/views/row/DataFieldRow.php index c92726f2ee9de5a8b6dffffb5d701c198f3ed114..8cebb738d1151d17d4c541450905b9ae41071782 100644 --- a/core/modules/rest/src/Plugin/views/row/DataFieldRow.php +++ b/core/modules/rest/src/Plugin/views/row/DataFieldRow.php @@ -137,10 +137,6 @@ public function render($row) { $output = array(); foreach ($this->view->field as $id => $field) { - // Don't render anything if this field is excluded. - if (!empty($field->options['exclude'])) { - continue; - } // If the raw output option has been set, just get the raw value. if (!empty($this->rawOutputOptions[$id])) { $value = $field->getValue($row); @@ -150,7 +146,10 @@ public function render($row) { $value = $field->advancedRender($row); } - $output[$this->getFieldKeyAlias($id)] = $value; + // Omit excluded fields from the rendered output. + if (empty($field->options['exclude'])) { + $output[$this->getFieldKeyAlias($id)] = $value; + } } return $output; diff --git a/core/modules/rest/src/Tests/Views/ExcludedFieldTokenTest.php b/core/modules/rest/src/Tests/Views/ExcludedFieldTokenTest.php new file mode 100644 index 0000000000000000000000000000000000000000..2a910fe285dcc44831a7606fcf1a5a18b623c8cc --- /dev/null +++ b/core/modules/rest/src/Tests/Views/ExcludedFieldTokenTest.php @@ -0,0 +1,88 @@ +<?php + +namespace Drupal\rest\Tests\Views; + +use Drupal\node\Entity\Node; +use Drupal\views\Tests\ViewTestBase; +use Drupal\views\Tests\ViewTestData; +use Drupal\views\Views; + +/** + * Tests the display of an excluded field that is used as a token. + * + * @group rest + * @see \Drupal\rest\Plugin\views\display\RestExport + * @see \Drupal\rest\Plugin\views\row\DataFieldRow + */ +class ExcludedFieldTokenTest extends ViewTestBase { + + /** + * @var \Drupal\views\ViewExecutable + */ + protected $view; + + /** + * The views that are used by this test. + * + * @var array + */ + public static $testViews = ['test_excluded_field_token_display']; + + /** + * The modules that need to be installed for this test. + * + * @var array + */ + public static $modules = [ + 'entity_test', + 'rest_test_views', + 'node', + 'field', + ]; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + ViewTestData::createTestViews(get_class($this), ['rest_test_views']); + + // Create some test content. + for ($i = 1; $i <= 10; $i++) { + Node::create([ + 'type' => 'article', + 'title' => 'Article test ' . $i, + ])->save(); + } + + $this->enableViewsTestModule(); + + $this->view = Views::getView('test_excluded_field_token_display'); + $this->view->setDisplay('rest_export_1'); + } + + /** + * Tests the display of an excluded title field when used as a token. + */ + public function testExcludedTitleTokenDisplay() { + $actual_json = $this->drupalGetWithFormat($this->view->getPath(), 'json'); + $this->assertResponse(200); + + $expected = [ + ['nothing' => 'Article test 10'], + ['nothing' => 'Article test 9'], + ['nothing' => 'Article test 8'], + ['nothing' => 'Article test 7'], + ['nothing' => 'Article test 6'], + ['nothing' => 'Article test 5'], + ['nothing' => 'Article test 4'], + ['nothing' => 'Article test 3'], + ['nothing' => 'Article test 2'], + ['nothing' => 'Article test 1'], + ]; + $this->assertIdentical($actual_json, json_encode($expected)); + } + + +} diff --git a/core/modules/rest/tests/modules/rest_test_views/test_views/views.view.test_excluded_field_token_display.yml b/core/modules/rest/tests/modules/rest_test_views/test_views/views.view.test_excluded_field_token_display.yml new file mode 100644 index 0000000000000000000000000000000000000000..e95f9533709d6455190699f1c3e7b23f9a26b630 --- /dev/null +++ b/core/modules/rest/tests/modules/rest_test_views/test_views/views.view.test_excluded_field_token_display.yml @@ -0,0 +1,277 @@ +langcode: en +status: true +dependencies: + config: + - node.type.article + module: + - node + - rest + - user +id: test_excluded_field_token_display +label: 'Test Excluded Field Token Display' +module: views +description: '' +tag: '' +base_table: node_field_data +base_field: nid +core: 8.x +display: + default: + display_plugin: default + id: default + display_title: Master + position: 0 + display_options: + access: + type: perm + options: + perm: 'access content' + cache: + type: tag + options: { } + query: + type: views_query + options: + disable_sql_rewrite: false + distinct: false + replica: false + query_comment: '' + query_tags: { } + exposed_form: + type: basic + options: + submit_button: Apply + reset_button: false + reset_button_label: Reset + exposed_sorts_label: 'Sort by' + expose_sort_order: true + sort_asc_label: Asc + sort_desc_label: Desc + pager: + type: mini + options: + items_per_page: 10 + offset: 0 + id: 0 + total_pages: null + expose: + items_per_page: false + items_per_page_label: 'Items per page' + items_per_page_options: '5, 10, 25, 50' + items_per_page_options_all: false + items_per_page_options_all_label: '- All -' + offset: false + offset_label: Offset + tags: + previous: ‹‹ + next: ›› + style: + type: serializer + row: + type: fields + options: + inline: { } + separator: '' + hide_empty: false + default_field_elements: true + fields: + title: + id: title + table: node_field_data + field: title + relationship: none + group_type: group + admin_label: '' + label: '' + exclude: true + alter: + alter_text: false + text: '' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: false + ellipsis: false + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: false + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + click_sort_column: value + type: string + settings: + link_to_entity: false + group_column: value + group_columns: { } + group_rows: true + delta_limit: 0 + delta_offset: 0 + delta_reversed: false + delta_first_last: false + multi_type: separator + separator: ', ' + field_api_classes: false + entity_type: node + entity_field: title + plugin_id: field + nothing: + id: nothing + table: views + field: nothing + relationship: none + group_type: group + admin_label: '' + label: '' + exclude: false + alter: + alter_text: true + text: '{{ title }}' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: true + ellipsis: true + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: false + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: false + plugin_id: custom + filters: + status: + value: '1' + table: node_field_data + field: status + plugin_id: boolean + entity_type: node + entity_field: status + id: status + expose: + operator: '' + group: 1 + type: + id: type + table: node_field_data + field: type + value: + article: article + entity_type: node + entity_field: type + plugin_id: bundle + sorts: + nid: + id: nid + table: node_field_data + field: nid + order: DESC + entity_type: node + entity_field: nid + plugin_id: standard + relationship: none + group_type: group + admin_label: '' + exposed: false + expose: + label: '' + header: { } + footer: { } + empty: { } + relationships: { } + arguments: { } + display_extenders: { } + cache_metadata: + max-age: -1 + contexts: + - 'languages:language_content' + - 'languages:language_interface' + - request_format + - url.query_args + - 'user.node_grants:view' + - user.permissions + tags: { } + rest_export_1: + display_plugin: rest_export + id: rest_export_1 + display_title: 'REST export' + position: 1 + display_options: + display_extenders: { } + path: rest/test/excluded-field-token + pager: + type: some + options: + items_per_page: 10 + offset: 0 + style: + type: serializer + options: + formats: + json: json + row: + type: data_field + options: + field_options: + title: + alias: '' + raw_output: false + cache_metadata: + max-age: -1 + contexts: + - 'languages:language_content' + - 'languages:language_interface' + - request_format + - 'user.node_grants:view' + - user.permissions + tags: { }