Issue #2272001 by dsnopek, Devin Carlson, tim.plunkett, xjm, martin107: Views display plugin's list of handlers is not filtered by access

8.0.x
Alex Pott 2014-12-11 10:16:07 +01:00
parent 4250561315
commit 3dcbc22381
6 changed files with 92 additions and 3 deletions

View File

@ -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();

View File

@ -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.
*/

View File

@ -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;

View File

@ -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'

View File

@ -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

View File

@ -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;
}