You are here

class Roles in Drupal 10

Same name in this branch
  1. 10 core/modules/user/src/Plugin/views/filter/Roles.php \Drupal\user\Plugin\views\filter\Roles
  2. 10 core/modules/user/src/Plugin/views/field/Roles.php \Drupal\user\Plugin\views\field\Roles
Same name and namespace in other branches
  1. 8 core/modules/user/src/Plugin/views/field/Roles.php \Drupal\user\Plugin\views\field\Roles
  2. 9 core/modules/user/src/Plugin/views/field/Roles.php \Drupal\user\Plugin\views\field\Roles

Field handler to provide a list of roles.

Plugin annotation

@ViewsField("user_roles");

Hierarchy

Expanded class hierarchy of Roles

19 string references to 'Roles'
AccountForm::form in core/modules/user/src/AccountForm.php
Gets the actual form array to be built.
BlockForm::buildVisibilityInterface in core/modules/block/src/BlockForm.php
Helper function for building the visibility UI form.
drupal6.php in core/modules/migrate_drupal/tests/fixtures/drupal6.php
A database agnostic dump for testing purposes.
drupal7.php in core/modules/tracker/tests/fixtures/drupal7.php
A database agnostic dump for testing purposes.
drupal7.php in core/modules/rdf/tests/fixtures/drupal7.php
A database agnostic dump for testing purposes.

... See full list

File

core/modules/user/src/Plugin/views/field/Roles.php, line 18

Namespace

Drupal\user\Plugin\views\field
View source
class Roles extends PrerenderList {

  /**
   * Database Service Object.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Constructs a \Drupal\user\Plugin\views\field\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\Core\Database\Connection $database
   *   Database Service Object.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('database'));
  }

  /**
   * {@inheritdoc}
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
    $this->additional_fields['uid'] = [
      'table' => 'users_field_data',
      'field' => 'uid',
    ];
  }
  public function query() {
    $this
      ->addAdditionalFields();
    $this->field_alias = $this->aliases['uid'];
  }
  public function preRender(&$values) {
    $uids = [];
    $this->items = [];
    foreach ($values as $result) {
      $uids[] = $this
        ->getValue($result);
    }
    if ($uids) {
      $roles = user_roles();
      $result = $this->database
        ->query('SELECT [u].[entity_id] AS [uid], [u].[roles_target_id] AS [rid] FROM {user__roles} [u] WHERE [u].[entity_id] IN ( :uids[] ) AND [u].[roles_target_id] IN ( :rids[] )', [
        ':uids[]' => $uids,
        ':rids[]' => array_keys($roles),
      ]);
      foreach ($result as $role) {
        $this->items[$role->uid][$role->rid]['role'] = $roles[$role->rid]
          ->label();
        $this->items[$role->uid][$role->rid]['rid'] = $role->rid;
      }

      // Sort the roles for each user by role weight.
      $ordered_roles = array_flip(array_keys($roles));
      foreach ($this->items as &$user_roles) {

        // Create an array of rids that the user has in the role weight order.
        $sorted_keys = array_intersect_key($ordered_roles, $user_roles);

        // Merge with the unsorted array of role information which has the
        // effect of sorting it.
        $user_roles = array_merge($sorted_keys, $user_roles);
      }
    }
  }
  public function render_item($count, $item) {
    return $item['role'];
  }
  protected function documentSelfTokens(&$tokens) {
    $tokens['{{ ' . $this->options['id'] . '__role' . ' }}'] = $this
      ->t('The name of the role.');
    $tokens['{{ ' . $this->options['id'] . '__rid' . ' }}'] = $this
      ->t('The role machine-name of the role.');
  }
  protected function addSelfTokens(&$tokens, $item) {
    if (!empty($item['role'])) {
      $tokens['{{ ' . $this->options['id'] . '__role }}'] = $item['role'];
      $tokens['{{ ' . $this->options['id'] . '__rid }}'] = $item['rid'];
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DerivativeInspectionInterface::getBaseId public function Gets the base_plugin_id of the plugin instance.
DerivativeInspectionInterface::getDerivativeId public function Gets the derivative_id of the plugin instance.
FieldHandlerInterface::advancedRender public function Renders a field using advanced settings.
FieldHandlerInterface::clickSort public function Adds an ORDER BY clause to the query for click sort columns.
FieldHandlerInterface::clickSortable public function Determines if this field is click sortable.
FieldHandlerInterface::elementClasses public function Returns the class of the field.
FieldHandlerInterface::elementLabelClasses public function Returns the class of the field's label.
FieldHandlerInterface::elementLabelType public function Returns an HTML element for the label based upon the field's element type.
FieldHandlerInterface::elementType public function Returns an HTML element based upon the field's element type.
FieldHandlerInterface::elementWrapperClasses public function Returns the class of the field's wrapper.
FieldHandlerInterface::elementWrapperType public function Returns an HTML element for the wrapper based upon the field's element type.
FieldHandlerInterface::getElements public function Provides a list of elements valid for field HTML.
FieldHandlerInterface::getEntity public function Gets the entity matching the current row and relationship.
FieldHandlerInterface::getRenderTokens public function Gets the 'render' tokens to use for advanced rendering.
FieldHandlerInterface::getValue public function Gets the value that's supposed to be rendered.
FieldHandlerInterface::isValueEmpty public function Checks if a field value is empty.
FieldHandlerInterface::label public function Gets this field's label.
FieldHandlerInterface::postRender public function Runs after every field has been rendered.
FieldHandlerInterface::render public function Renders the field.
FieldHandlerInterface::renderText public function Performs an advanced text render for the item.
FieldHandlerInterface::theme public function Renders row values using $this->themeFunctions() as #theme.
FieldHandlerInterface::tokenizeValue public function Replaces a value with tokens from the last field.
FieldHandlerInterface::useStringGroupBy public function Determines if this field will be available as an option to group the result by in the style settings.
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 1
PluginInspectionInterface::getPluginId public function Gets the plugin_id of the plugin instance. 1
PrerenderList::$items public property Stores all items which are used to render the items.
PrerenderList::buildOptionsForm public function Provide a form to edit options for this plugin. Overrides ViewsPluginInterface::buildOptionsForm 1
PrerenderList::defineOptions protected function 1
PrerenderList::getItems public function Items should be stored in the result array, if possible, as an array with 'value' as the actual displayable value of the item, plus any items that might be found in the 'alter' options array for creating links, such as… Overrides MultiItemsFieldHandlerInterface::getItems
PrerenderList::renderItems public function Render all items in this field together. Overrides MultiItemsFieldHandlerInterface::renderItems
Roles::$database protected property Database Service Object.
Roles::addSelfTokens protected function
Roles::create public static function Overrides ViewsPluginInterface::create
Roles::documentSelfTokens protected function
Roles::init public function Initialize the plugin. Overrides ViewsPluginInterface::init
Roles::preRender public function Runs before any fields are rendered. Overrides FieldHandlerInterface::preRender
Roles::query public function Add anything to the query that we might need to. Overrides ViewsPluginInterface::query
Roles::render_item public function Renders a single item of a row. Overrides MultiItemsFieldHandlerInterface::render_item
Roles::__construct public function Constructs a \Drupal\user\Plugin\views\field\Roles object.
ViewsHandlerInterface::access public function Check whether given user has access to this handler.
ViewsHandlerInterface::adminLabel public function Return a string representing this handler's name in the UI.
ViewsHandlerInterface::adminSummary public function Provide text for the administrative summary.
ViewsHandlerInterface::breakString public static function Breaks x,y,z and x+y+z into an array.
ViewsHandlerInterface::broken public function Determines if the handler is considered 'broken', meaning it's a placeholder used when a handler can't be found.
ViewsHandlerInterface::ensureMyTable public function Ensure the main table for this handler is in the query. This is used a lot.
ViewsHandlerInterface::getEntityType public function Determines the entity type used by this handler.
ViewsHandlerInterface::getField public function Shortcut to get a handler's raw field value.
ViewsHandlerInterface::getJoin public function Get the join object that should be used for this handler.
ViewsHandlerInterface::getTableJoin public static function Fetches a handler to join one table to a primary table from the data cache.
ViewsHandlerInterface::postExecute public function Run after the view is executed, before the result is cached.
ViewsHandlerInterface::preQuery public function Run before the view is built.
ViewsHandlerInterface::sanitizeValue public function Sanitize the value for output.
ViewsHandlerInterface::setRelationship public function Called just prior to query(), this lets a handler set up any relationship it needs.
ViewsHandlerInterface::showExposeForm public function Shortcut to display the exposed options form.
ViewsPluginInterface::destroy public function Clears a plugin.
ViewsPluginInterface::filterByDefinedOptions public function Filter out stored options depending on the defined options.
ViewsPluginInterface::getAvailableGlobalTokens public function Returns an array of available token replacements.
ViewsPluginInterface::getProvider public function Returns the plugin provider.
ViewsPluginInterface::globalTokenForm public function Adds elements for available core tokens to a form.
ViewsPluginInterface::globalTokenReplace public function Returns a string with any core tokens replaced.
ViewsPluginInterface::pluginTitle public function Return the human readable name of the display.
ViewsPluginInterface::preRenderAddFieldsetMarkup public static function Moves form elements into fieldsets for presentation purposes.
ViewsPluginInterface::preRenderFlattenData public static function Flattens the structure of form elements.
ViewsPluginInterface::submitOptionsForm public function Handle any special handling on the validate form.
ViewsPluginInterface::summaryTitle public function Returns the summary of the settings in the display.
ViewsPluginInterface::themeFunctions public function Provide a full list of possible theme templates used by this style.
ViewsPluginInterface::unpackOptions public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
ViewsPluginInterface::usesOptions public function Returns the usesOptions property.
ViewsPluginInterface::validate public function Validate that the plugin is correct and can be saved.
ViewsPluginInterface::validateOptionsForm public function Validate the options form.