From 1c61006d905631fa4d9ab208838f84645dcc8405 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 15 Dec 2014 17:04:58 +0000 Subject: [PATCH] Issue #2389275 by bforchhammer: Views render element #embed not working --- core/modules/views/src/Element/View.php | 4 +- .../views/src/Tests/ViewElementTest.php | 75 ++++++++++++++++++- .../test_views/views.view.test_view_embed.yml | 63 ++++++++++++++++ .../Form/ViewsTestDataElementEmbedForm.php | 44 +++++++++++ .../src/Form/ViewsTestDataElementForm.php | 3 +- .../views_test_data.routing.yml | 7 ++ 6 files changed, 190 insertions(+), 6 deletions(-) create mode 100644 core/modules/views/tests/modules/views_test_config/test_views/views.view.test_view_embed.yml create mode 100644 core/modules/views/tests/modules/views_test_data/src/Form/ViewsTestDataElementEmbedForm.php diff --git a/core/modules/views/src/Element/View.php b/core/modules/views/src/Element/View.php index a59aa16e00e..eb815cf0c8b 100644 --- a/core/modules/views/src/Element/View.php +++ b/core/modules/views/src/Element/View.php @@ -30,7 +30,7 @@ class View extends RenderElement { '#name' => NULL, '#display_id' => 'default', '#arguments' => array(), - '#embed' => FALSE, + '#embed' => TRUE, ); } @@ -48,7 +48,7 @@ class View extends RenderElement { } if ($view && $view->access($element['#display_id'])) { - if (!empty($element['embed'])) { + if (!empty($element['#embed'])) { $element += $view->preview($element['#display_id'], $element['#arguments']); } else { diff --git a/core/modules/views/src/Tests/ViewElementTest.php b/core/modules/views/src/Tests/ViewElementTest.php index ec85538d0a3..441f78ea851 100644 --- a/core/modules/views/src/Tests/ViewElementTest.php +++ b/core/modules/views/src/Tests/ViewElementTest.php @@ -21,7 +21,7 @@ class ViewElementTest extends ViewTestBase { * * @var array */ - public static $testViews = array('test_view'); + public static $testViews = array('test_view_embed'); /** * The raw render data array to use in tests. @@ -40,9 +40,10 @@ class ViewElementTest extends ViewTestBase { $this->render = array( 'view' => array( '#type' => 'view', - '#name' => 'test_view', + '#name' => 'test_view_embed', '#display_id' => 'default', '#arguments' => array(25), + '#embed' => FALSE, ), ); } @@ -51,7 +52,7 @@ class ViewElementTest extends ViewTestBase { * Tests the rendered output and form output of a view element. */ public function testViewElement() { - $view = Views::getView('test_view'); + $view = Views::getView('test_view_embed'); $view->setDisplay(); // Set the content as our rendered array. @@ -112,4 +113,72 @@ class ViewElementTest extends ViewTestBase { $this->assertEqual(count($xpath), 1); } + /** + * Tests the rendered output and form output of a view element, using the + * embed display plugin. + */ + public function testViewElementEmbed() { + $view = Views::getView('test_view_embed'); + $view->setDisplay('embed_1'); + + // Set the content as our rendered array. + $render = $this->render; + $render['#embed'] = TRUE; + $this->drupalSetContent(drupal_render($render)); + + $xpath = $this->xpath('//div[@class="views-element-container"]'); + $this->assertTrue($xpath, 'The view container has been found in the rendered output.'); + + $xpath = $this->xpath('//div[@class="view-content"]'); + $this->assertTrue($xpath, 'The view content has been found in the rendered output.'); + // There should be 5 rows in the results. + $xpath = $this->xpath('//div[@class="view-content"]/div'); + $this->assertEqual(count($xpath), 5); + + // Test a form. + $this->drupalGet('views_test_data_element_embed_form'); + + $xpath = $this->xpath('//div[@class="views-element-container form-wrapper"]'); + $this->assertTrue($xpath, 'The view container has been found on the form.'); + + $xpath = $this->xpath('//div[@class="view-content"]'); + $this->assertTrue($xpath, 'The view content has been found on the form.'); + // There should be 5 rows in the results. + $xpath = $this->xpath('//div[@class="view-content"]/div'); + $this->assertEqual(count($xpath), 5); + + // Add an argument and save the view. + $view->displayHandlers->get('default')->overrideOption('arguments', array( + 'age' => array( + 'default_action' => 'ignore', + 'title' => '', + 'default_argument_type' => 'fixed', + 'validate' => array( + 'type' => 'none', + 'fail' => 'not found', + ), + 'break_phrase' => FALSE, + 'not' => FALSE, + 'id' => 'age', + 'table' => 'views_test_data', + 'field' => 'age', + 'plugin_id' => 'numeric', + ) + )); + $view->save(); + + // Test the render array again. + $render = $this->render; + $render['#embed'] = TRUE; + $this->drupalSetContent(drupal_render($render)); + // There should be 1 row in the results, 'John' arg 25. + $xpath = $this->xpath('//div[@class="view-content"]/div'); + $this->assertEqual(count($xpath), 1); + + // Test that the form has the same expected result. + $this->drupalGet('views_test_data_element_embed_form'); + $xpath = $this->xpath('//div[@class="view-content"]/div'); + $this->assertEqual(count($xpath), 1); + } + } diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_view_embed.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_view_embed.yml new file mode 100644 index 00000000000..8ca9e5ca59a --- /dev/null +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_view_embed.yml @@ -0,0 +1,63 @@ +langcode: und +status: true +dependencies: { } +id: test_view_embed +label: 'Test view embed' +module: views +description: '' +tag: '' +base_table: views_test_data +base_field: nid +core: '8' +display: + default: + display_options: + defaults: + fields: false + pager: false + sorts: false + row: + type: fields + fields: + age: + field: age + id: age + relationship: none + table: views_test_data + plugin_id: numeric + id: + field: id + id: id + relationship: none + table: views_test_data + plugin_id: numeric + name: + field: name + id: name + relationship: none + table: views_test_data + plugin_id: string + pager: + options: + offset: 0 + type: none + sorts: + id: + field: id + id: id + order: ASC + relationship: none + table: views_test_data + plugin_id: numeric + field_langcode: '***LANGUAGE_language_content***' + field_langcode_add_to_query: null + display_plugin: default + display_title: Master + id: default + position: 0 + embed_1: + display_options: { } + display_plugin: embed + display_title: Embedded + id: embed_1 + position: 1 diff --git a/core/modules/views/tests/modules/views_test_data/src/Form/ViewsTestDataElementEmbedForm.php b/core/modules/views/tests/modules/views_test_data/src/Form/ViewsTestDataElementEmbedForm.php new file mode 100644 index 00000000000..1fb160e35d0 --- /dev/null +++ b/core/modules/views/tests/modules/views_test_data/src/Form/ViewsTestDataElementEmbedForm.php @@ -0,0 +1,44 @@ + 'view', + '#name' => 'test_view_embed', + '#display_id' => 'embed_1', + '#arguments' => array(25), + '#embed' => TRUE, + ); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + } +} diff --git a/core/modules/views/tests/modules/views_test_data/src/Form/ViewsTestDataElementForm.php b/core/modules/views/tests/modules/views_test_data/src/Form/ViewsTestDataElementForm.php index 27f71606c97..73f902bb189 100644 --- a/core/modules/views/tests/modules/views_test_data/src/Form/ViewsTestDataElementForm.php +++ b/core/modules/views/tests/modules/views_test_data/src/Form/ViewsTestDataElementForm.php @@ -27,9 +27,10 @@ class ViewsTestDataElementForm extends FormBase { public function buildForm(array $form, FormStateInterface $form_state) { $form['view'] = array( '#type' => 'view', - '#name' => 'test_view', + '#name' => 'test_view_embed', '#display_id' => 'default', '#arguments' => array(25), + '#embed' => FALSE, ); return $form; diff --git a/core/modules/views/tests/modules/views_test_data/views_test_data.routing.yml b/core/modules/views/tests/modules/views_test_data/views_test_data.routing.yml index e1c25eaea2b..63d8ac827f2 100644 --- a/core/modules/views/tests/modules/views_test_data/views_test_data.routing.yml +++ b/core/modules/views/tests/modules/views_test_data/views_test_data.routing.yml @@ -4,3 +4,10 @@ views_test_data.element: _form: '\Drupal\views_test_data\Form\ViewsTestDataElementForm' requirements: _access: 'TRUE' + +views_test_data.element_embed: + path: '/views_test_data_element_embed_form' + defaults: + _form: '\Drupal\views_test_data\Form\ViewsTestDataElementEmbedForm' + requirements: + _access: 'TRUE'