diff --git a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php index 8f68d0d1f74..13ff1681cf1 100644 --- a/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/DisplayPluginBase.php @@ -912,7 +912,7 @@ abstract class DisplayPluginBase extends PluginBase { * * @return \Drupal\views\Plugin\views\ViewsHandlerInterface[] */ - public function getHandlers($type) { + public function &getHandlers($type) { if (!isset($this->handlers[$type])) { $this->handlers[$type] = array(); $types = ViewExecutable::getHandlerTypes(); diff --git a/core/modules/views/src/Tests/Handler/AreaTest.php b/core/modules/views/src/Tests/Handler/AreaTest.php index cece05cce02..03b8b57cea1 100644 --- a/core/modules/views/src/Tests/Handler/AreaTest.php +++ b/core/modules/views/src/Tests/Handler/AreaTest.php @@ -23,7 +23,7 @@ class AreaTest extends HandlerTestBase { * * @var array */ - public static $testViews = array('test_example_area'); + public static $testViews = array('test_example_area', 'test_example_area_access'); /** * Modules to enable. @@ -112,6 +112,45 @@ class AreaTest extends HandlerTestBase { $this->assertTrue(strpos($output, $empty_string) !== FALSE); } + /** + * Tests the access for an area. + */ + public function testAreaAccess() { + // Test with access denied for the area handler. + $view = Views::getView('test_example_area_access'); + $view->initDisplay(); + $view->initHandlers(); + $handlers = $view->display_handler->getHandlers('empty'); + $this->assertEqual(0, count($handlers)); + + $output = $view->preview(); + $output = \Drupal::service('renderer')->render($output); + // The area output should not be present since access was denied. + $this->assertFalse(strpos($output, 'a custom string') !== FALSE); + $view->destroy(); + + // Test with access granted for the area handler. + $view = Views::getView('test_example_area_access'); + $view->initDisplay(); + $view->display_handler->overrideOption('empty', [ + 'test_example' => [ + 'field' => 'test_example', + 'id' => 'test_example', + 'table' => 'views', + 'plugin_id' => 'test_example', + 'string' => 'a custom string', + 'custom_access' => TRUE, + ], + ]); + $view->initHandlers(); + $handlers = $view->display_handler->getHandlers('empty'); + + $output = $view->preview(); + $output = \Drupal::service('renderer')->render($output); + $this->assertTrue(strpos($output, 'a custom string') !== FALSE); + $this->assertEqual(1, count($handlers)); + } + /** * Tests global tokens. */ diff --git a/core/modules/views/src/ViewExecutable.php b/core/modules/views/src/ViewExecutable.php index 8c61f35633d..72cd5435ca9 100644 --- a/core/modules/views/src/ViewExecutable.php +++ b/core/modules/views/src/ViewExecutable.php @@ -886,7 +886,7 @@ class ViewExecutable { */ protected function _initHandler($key, $info) { // Load the requested items from the display onto the object. - $this->$key = $this->display_handler->getHandlers($key); + $this->$key = &$this->display_handler->getHandlers($key); // This reference deals with difficult PHP indirection. $handlers = &$this->$key; diff --git a/core/modules/views/tests/modules/views_test_config/config/schema/views_test_config.views.schema.yml b/core/modules/views/tests/modules/views_test_config/config/schema/views_test_config.views.schema.yml new file mode 100644 index 00000000000..199cdfcbb74 --- /dev/null +++ b/core/modules/views/tests/modules/views_test_config/config/schema/views_test_config.views.schema.yml @@ -0,0 +1,12 @@ +# Schema for the views plugins of the Views test config module. + +views.area.test_example: + type: views_area + label: 'Test example' + mapping: + string: + type: text + label: 'The shown text of the area' + custom_access: + type: boolean + label: 'Should access to the area be allowed' diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_example_area_access.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_example_area_access.yml new file mode 100644 index 00000000000..d92e672ac2a --- /dev/null +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_example_area_access.yml @@ -0,0 +1,29 @@ +langcode: und +status: true +dependencies: { } +id: test_example_area_access +module: views +description: '' +tag: '' +base_table: node +base_field: nid +core: '8' +display: + default: + display_options: + access: + type: none + cache: + type: none + empty: + test_example: + field: test_example + id: test_example + table: views + plugin_id: test_example + custom_access: false + string: 'a custom string' + display_plugin: default + display_title: Master + id: default + position: 0 diff --git a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/area/TestExample.php b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/area/TestExample.php index e61997e6432..1bcf2f4d83c 100644 --- a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/area/TestExample.php +++ b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/area/TestExample.php @@ -8,6 +8,7 @@ namespace Drupal\views_test_data\Plugin\views\area; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Session\AccountInterface; use Drupal\views\Plugin\views\area\AreaPluginBase; /** @@ -19,12 +20,20 @@ use Drupal\views\Plugin\views\area\AreaPluginBase; */ class TestExample extends AreaPluginBase { + /** + * {@inheritdoc} + */ + public function access(AccountInterface $account) { + return $this->options['custom_access']; + } + /** * Overrides Drupal\views\Plugin\views\area\AreaPluginBase::option_definition(). */ public function defineOptions() { $options = parent::defineOptions(); $options['string'] = array('default' => ''); + $options['custom_access'] = array('default' => TRUE); return $options; }