diff --git a/core/modules/user/src/Plugin/views/filter/Roles.php b/core/modules/user/src/Plugin/views/filter/Roles.php index 014425d..f878785 100644 --- a/core/modules/user/src/Plugin/views/filter/Roles.php +++ b/core/modules/user/src/Plugin/views/filter/Roles.php @@ -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; + } + } diff --git a/core/modules/user/src/Tests/Views/HandlerFilterRolesTest.php b/core/modules/user/src/Tests/Views/HandlerFilterRolesTest.php new file mode 100644 index 0000000..6ed889b --- /dev/null +++ b/core/modules/user/src/Tests/Views/HandlerFilterRolesTest.php @@ -0,0 +1,72 @@ + '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()); + } + +}