Commit 0a38107e authored by alexpott's avatar alexpott

Issue #2407107 by jan.stoeckler: Drupal\user\Plugin\views\filter\Roles should...

Issue #2407107 by jan.stoeckler: Drupal\user\Plugin\views\filter\Roles should implement calculateDependencies()
parent c1005af3
......@@ -7,7 +7,9 @@
namespace Drupal\user\Plugin\views\filter;
use Drupal\user\RoleStorageInterface;
use Drupal\views\Plugin\views\filter\ManyToOne;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Filter handler for user roles.
......@@ -18,6 +20,42 @@
*/
class Roles extends ManyToOne {
/**
* The role storage.
*
* @var \Drupal\user\RoleStorageInterface
*/
protected $roleStorage;
/**
* Constructs a Roles object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\user\RoleStorageInterface $role_storage
* The role storage.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, RoleStorageInterface $role_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->roleStorage = $role_storage;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.manager')->getStorage('user_role')
);
}
public function getValueOptions() {
$this->valueOptions = user_role_names(TRUE);
unset($this->valueOptions[DRUPAL_AUTHENTICATED_RID]);
......@@ -33,4 +71,16 @@ function operators() {
return $operators;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$dependencies = array();
foreach ($this->value as $role_id) {
$role = $this->roleStorage->load($role_id);
$dependencies[$role->getConfigDependencyKey()][] = $role->getConfigDependencyName();
}
return $dependencies;
}
}
<?php
/**
* @file
* Contains \Drupal\user\Tests\Views\HandlerFilterRolesTest.
*/
namespace Drupal\user\Tests\Views;
use Drupal\Component\Utility\String;
use Drupal\user\Entity\Role;
use Drupal\user\Tests\Views\UserUnitTestBase;
use Drupal\views\Entity\View;
use Drupal\views\Views;
/**
* Tests the roles filter handler.
*
* @group user
*
* @see \Drupal\user\Plugin\views\filter\Roles
*/
class HandlerFilterRolesTest extends UserUnitTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_user_name');
/**
* Tests that role filter dependencies are calculated correctly.
*/
public function testDependencies() {
$role = Role::create(['id' => 'test_user_role']);
$role->save();
$view = View::load('test_user_name');
$expected = [
'module' => ['user'],
];
$this->assertEqual($expected, $view->getDependencies());
$display = &$view->getDisplay('default');
$display['display_options']['filters']['roles_target_id'] = [
'id' => 'roles_target_id',
'table' => 'user__roles',
'field' => 'roles_target_id',
'value' => [
'test_user_role' => 'test_user_role',
],
'plugin_id' => 'user_roles',
];
$view->save();
$expected['config'][] = 'user.role.test_user_role';
$this->assertEqual($expected, $view->getDependencies());
$view = View::load('test_user_name');
$display = &$view->getDisplay('default');
$display['display_options']['filters']['roles_target_id'] = [
'id' => 'roles_target_id',
'table' => 'user__roles',
'field' => 'roles_target_id',
'value' => [],
'plugin_id' => 'user_roles',
];
$view->save();
unset($expected['config']);
$this->assertEqual($expected, $view->getDependencies());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment