class Role

Same name in this branch
  1. 10 core/modules/user/src/Entity/Role.php \Drupal\user\Entity\Role
  2. 10 core/modules/user/src/Plugin/migrate/source/d6/Role.php \Drupal\user\Plugin\migrate\source\d6\Role
  3. 10 core/modules/user/src/Plugin/migrate/source/d7/Role.php \Drupal\user\Plugin\migrate\source\d7\Role
Same name in other branches
  1. 9 core/modules/user/src/Entity/Role.php \Drupal\user\Entity\Role
  2. 9 core/modules/user/src/Plugin/views/access/Role.php \Drupal\user\Plugin\views\access\Role
  3. 9 core/modules/user/src/Plugin/migrate/source/d6/Role.php \Drupal\user\Plugin\migrate\source\d6\Role
  4. 9 core/modules/user/src/Plugin/migrate/source/d7/Role.php \Drupal\user\Plugin\migrate\source\d7\Role
  5. 8.9.x core/modules/user/src/Entity/Role.php \Drupal\user\Entity\Role
  6. 8.9.x core/modules/user/src/Plugin/views/access/Role.php \Drupal\user\Plugin\views\access\Role
  7. 8.9.x core/modules/user/src/Plugin/migrate/source/d6/Role.php \Drupal\user\Plugin\migrate\source\d6\Role
  8. 8.9.x core/modules/user/src/Plugin/migrate/source/d7/Role.php \Drupal\user\Plugin\migrate\source\d7\Role
  9. 11.x core/modules/user/src/Entity/Role.php \Drupal\user\Entity\Role
  10. 11.x core/modules/user/src/Plugin/views/access/Role.php \Drupal\user\Plugin\views\access\Role
  11. 11.x core/modules/user/src/Plugin/migrate/source/d6/Role.php \Drupal\user\Plugin\migrate\source\d6\Role
  12. 11.x core/modules/user/src/Plugin/migrate/source/d7/Role.php \Drupal\user\Plugin\migrate\source\d7\Role

Access plugin that provides role-based access control.

Hierarchy

Expanded class hierarchy of Role

Related topics

1 file declares its use of Role
AccessRoleTest.php in core/modules/user/tests/src/Functional/Views/AccessRoleTest.php
34 string references to 'Role'
Block::query in core/modules/block/src/Plugin/migrate/source/Block.php
ChangeUserRoleBase::buildConfigurationForm in core/modules/user/src/Plugin/Action/ChangeUserRoleBase.php
drupal6.php in core/modules/migrate_drupal/tests/fixtures/drupal6.php
A database agnostic dump for testing purposes.
drupal6.php in core/modules/book/tests/fixtures/drupal6.php
A database agnostic dump for testing purposes.
drupal6.php in core/modules/forum/tests/fixtures/drupal6.php
A database agnostic dump for testing purposes.

... See full list

File

core/modules/user/src/Plugin/views/access/Role.php, line 23

Namespace

Drupal\user\Plugin\views\access
View source
class Role extends AccessPluginBase implements CacheableDependencyInterface {
    
    /**
     * {@inheritdoc}
     */
    protected $usesOptions = TRUE;
    
    /**
     * The role storage.
     *
     * @var \Drupal\user\RoleStorageInterface
     */
    protected $roleStorage;
    
    /**
     * Constructs a Role 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_type.manager')
            ->getStorage('user_role'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function access(AccountInterface $account) {
        return !empty(array_intersect(array_filter($this->options['role']), $account->getRoles()));
    }
    
    /**
     * {@inheritdoc}
     */
    public function alterRouteDefinition(Route $route) {
        if ($this->options['role']) {
            $route->setRequirement('_role', (string) implode('+', $this->options['role']));
        }
    }
    public function summaryTitle() {
        $count = count($this->options['role']);
        if ($count < 1) {
            return $this->t('No role(s) selected');
        }
        elseif ($count > 1) {
            return $this->t('Multiple roles');
        }
        else {
            $rid = reset($this->options['role']);
            return $this->roleStorage
                ->load($rid)
                ->label();
        }
    }
    protected function defineOptions() {
        $options = parent::defineOptions();
        $options['role'] = [
            'default' => [],
        ];
        return $options;
    }
    public function buildOptionsForm(&$form, FormStateInterface $form_state) {
        parent::buildOptionsForm($form, $form_state);
        $form['role'] = [
            '#type' => 'checkboxes',
            '#title' => $this->t('Role'),
            '#default_value' => $this->options['role'],
            '#options' => array_map(fn(RoleInterface $role) => Html::escape($role->label()), $this->roleStorage
                ->loadMultiple()),
            '#description' => $this->t('Only the checked roles will be able to access this display.'),
        ];
    }
    public function validateOptionsForm(&$form, FormStateInterface $form_state) {
        $role = $form_state->getValue([
            'access_options',
            'role',
        ]);
        $role = array_filter($role);
        if (!$role) {
            $form_state->setError($form['role'], $this->t('You must select at least one role if type is "by role"'));
        }
        $form_state->setValue([
            'access_options',
            'role',
        ], $role);
    }
    
    /**
     * {@inheritdoc}
     */
    public function calculateDependencies() {
        $dependencies = parent::calculateDependencies();
        foreach (array_keys($this->options['role']) as $rid) {
            if ($role = $this->roleStorage
                ->load($rid)) {
                $dependencies[$role->getConfigDependencyKey()][] = $role->getConfigDependencyName();
            }
        }
        return $dependencies;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheMaxAge() {
        return Cache::PERMANENT;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheContexts() {
        return [
            'user.roles',
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCacheTags() {
        return [];
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
DerivativeInspectionInterface::getBaseId public function Gets the base_plugin_id of the plugin instance. 1
DerivativeInspectionInterface::getDerivativeId public function Gets the derivative_id of the plugin instance. 1
PluginBase::$definition public property Plugins&#039; definition.
PluginBase::$displayHandler public property The display object this plugin is for.
PluginBase::$options public property Options for this plugin will be held here.
PluginBase::$position public property The handler position.
PluginBase::$renderer protected property Stores the render API renderer. 3
PluginBase::$view public property The top object of a view. 1
PluginBase::destroy public function Overrides ViewsPluginInterface::destroy 2
PluginBase::doFilterByDefinedOptions protected function Do the work to filter out stored options depending on the defined options.
PluginBase::filterByDefinedOptions public function Overrides ViewsPluginInterface::filterByDefinedOptions
PluginBase::getAvailableGlobalTokens public function Overrides ViewsPluginInterface::getAvailableGlobalTokens
PluginBase::getProvider public function Overrides ViewsPluginInterface::getProvider
PluginBase::getRenderer protected function Returns the render API renderer. 1
PluginBase::globalTokenForm public function Overrides ViewsPluginInterface::globalTokenForm
PluginBase::globalTokenReplace public function Overrides ViewsPluginInterface::globalTokenReplace
PluginBase::INCLUDE_ENTITY constant Include entity row languages when listing languages.
PluginBase::INCLUDE_NEGOTIATED constant Include negotiated languages when listing languages.
PluginBase::init public function Overrides ViewsPluginInterface::init 6
PluginBase::listLanguages protected function Makes an array of languages, optionally including special languages.
PluginBase::pluginTitle public function Overrides ViewsPluginInterface::pluginTitle
PluginBase::preRenderAddFieldsetMarkup public static function Overrides ViewsPluginInterface::preRenderAddFieldsetMarkup
PluginBase::preRenderFlattenData public static function Overrides ViewsPluginInterface::preRenderFlattenData
PluginBase::query public function Overrides ViewsPluginInterface::query 8
PluginBase::queryLanguageSubstitutions public static function Returns substitutions for Views queries for languages.
PluginBase::setOptionDefaults protected function Fills up the options of the plugin with defaults.
PluginBase::submitOptionsForm public function Overrides ViewsPluginInterface::submitOptionsForm 16
PluginBase::themeFunctions public function Overrides ViewsPluginInterface::themeFunctions 1
PluginBase::trustedCallbacks public static function Overrides TrustedCallbackInterface::trustedCallbacks 6
PluginBase::unpackOptions public function Overrides ViewsPluginInterface::unpackOptions
PluginBase::usesOptions public function Overrides ViewsPluginInterface::usesOptions 8
PluginBase::validate public function Overrides ViewsPluginInterface::validate 6
PluginBase::viewsTokenReplace protected function Replaces Views&#039; tokens in a given string. 1
PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT constant Query string to indicate the site default language.
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin ID of the plugin instance. 2
Role::$roleStorage protected property The role storage.
Role::$usesOptions protected property Overrides PluginBase::$usesOptions
Role::access public function Overrides AccessPluginBase::access
Role::alterRouteDefinition public function Overrides AccessPluginBase::alterRouteDefinition
Role::buildOptionsForm public function Overrides PluginBase::buildOptionsForm
Role::calculateDependencies public function Overrides PluginBase::calculateDependencies
Role::create public static function Overrides PluginBase::create
Role::defineOptions protected function Overrides PluginBase::defineOptions
Role::getCacheContexts public function Overrides CacheableDependencyInterface::getCacheContexts
Role::getCacheMaxAge public function Overrides CacheableDependencyInterface::getCacheMaxAge
Role::getCacheTags public function Overrides CacheableDependencyInterface::getCacheTags
Role::summaryTitle public function Overrides AccessPluginBase::summaryTitle
Role::validateOptionsForm public function Overrides PluginBase::validateOptionsForm
Role::__construct public function Constructs a Role object. Overrides PluginBase::__construct
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING Deprecated constant Untrusted callbacks trigger E_USER_WARNING errors.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.