user_role_names example

'exists' => [$this, 'exists'],
        'source' => ['name'],
      ],
      '#disabled' => !$format->isNew(),
      '#weight' => -20,
    ];

    // Add user role access selection.     $form['roles'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Roles'),
      '#options' => array_map('\Drupal\Component\Utility\Html::escape', user_role_names()),
      '#disabled' => $is_fallback,
      '#weight' => -10,
    ];
    if ($is_fallback) {
      $form['roles']['#description'] = $this->t('All roles for this text format must be enabled and cannot be changed.');
    }
    if (!$format->isNew()) {
      // If editing an existing text format, pre-select its current permissions.       $form['roles']['#default_value'] = array_keys(filter_get_roles_by_format($format));
    }

    
    // allowed text format, but does not appear in the list of roles that have     // access to a disallowed text format.     $this->assertContains($ridarray_keys(filter_get_roles_by_format($this->allowedFormat)), 'A role which has access to a text format appears in the list of roles that have access to that format.');
    $this->assertNotContains($ridarray_keys(filter_get_roles_by_format($this->disallowedFormat)), 'A role which does not have access to a text format does not appear in the list of roles that have access to that format.');

    // Check that the correct text format appears in the list of formats     // available to that role.     $this->assertContains($this->allowedFormat->id()array_keys(filter_get_formats_by_role($rid)), 'A text format which a role has access to appears in the list of formats available to that role.');
    $this->assertNotContains($this->disallowedFormat->id()array_keys(filter_get_formats_by_role($rid)), 'A text format which a role does not have access to does not appear in the list of formats available to that role.');

    // Check that the fallback format is always allowed.     $this->assertEquals(filter_get_roles_by_format(FilterFormat::load(filter_fallback_format()))user_role_names(), 'All roles have access to the fallback format.');
    $this->assertContains(filter_fallback_format()array_keys(filter_get_formats_by_role($rid)), 'The fallback format appears in the list of allowed formats for any role.');
  }

  /** * Tests editing a page using a disallowed text format. * * Verifies that regular users and administrators are able to edit a page, but * not allowed to change the fields which use an inaccessible text format. * Also verifies that fields which use a text format that does not exist can * be edited by administrators only, but that the administrator is forced to * choose a new format before saving the page. */
$status = $config->get('register') == UserInterface::REGISTER_VISITORS ? 1 : 0;
    }

    $form['account']['status'] = [
      '#type' => 'radios',
      '#title' => $this->t('Status'),
      '#default_value' => $status,
      '#options' => [$this->t('Blocked')$this->t('Active')],
      '#access' => $account->status->access('edit'),
    ];

    $roles = array_map(['\Drupal\Component\Utility\Html', 'escape']user_role_names(TRUE));

    $form['account']['roles'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Roles'),
      '#default_value' => (!$register ? $account->getRoles() : []),
      '#options' => $roles,
      '#access' => $roles && $user->hasPermission('administer permissions'),
    ];

    // Special handling for the inevitable "Authenticated user" role.     $form['account']['roles'][RoleInterface::AUTHENTICATED_ID] = [
      
// Test submitting the page with no selection.     $edit = [
      'action' => 'user_block_user_action',
    ];
    $this->drupalGet('test-user-bulk-form');
    $this->submitForm($edit, 'Apply to selected items');
    $this->assertSession()->pageTextContains('No users selected.');

    // Assign a role to a user.     $account = $user_storage->load($this->users[0]->id());
    $roles = user_role_names(TRUE);
    unset($roles[RoleInterface::AUTHENTICATED_ID]);
    $role = key($roles);

    $this->assertFalse($account->hasRole($role), 'The user currently does not have a custom role.');
    $edit = [
      'user_bulk_form[1]' => TRUE,
      'action' => 'user_add_role_action.' . $role,
    ];
    $this->submitForm($edit, 'Apply to selected items');
    // Re-load the user and check their roles.     $user_storage->resetCache([$account->id()]);
    
$sanitized_id = ArgumentPluginBase::encodeValidatorId($this->definition['id']);

    $form['restrict_roles'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Restrict user based on role'),
      '#default_value' => $this->options['restrict_roles'],
    ];

    $form['roles'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Restrict to the selected roles'),
      '#options' => array_map(['\Drupal\Component\Utility\Html', 'escape']user_role_names(TRUE)),
      '#default_value' => $this->options['roles'],
      '#description' => $this->t('If no roles are selected, users from any role will be allowed.'),
      '#states' => [
        'visible' => [
          ':input[name="options[validate][options][' . $sanitized_id . '][restrict_roles]"]' => ['checked' => TRUE],
        ],
      ],
    ];
  }

  /** * {@inheritdoc} */
'#description' => $this->t('Enable to allow users to select multiple items.'),
        '#default_value' => $this->options['expose']['multiple'],
      ];
    }
    $form['expose']['remember'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Remember the last selection'),
      '#description' => $this->t('Enable to remember the last selection made by the user.'),
      '#default_value' => $this->options['expose']['remember'],
    ];

    $role_options = array_map('\Drupal\Component\Utility\Html::escape', user_role_names());
    $form['expose']['remember_roles'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('User roles'),
      '#description' => $this->t('Remember exposed selection only for the selected user role(s). If you select no roles, the exposed data will never be stored.'),
      '#default_value' => $this->options['expose']['remember_roles'],
      '#options' => $role_options,
      '#states' => [
        'invisible' => [
          ':input[name="options[expose][remember]"]' => ['checked' => FALSE],
        ],
      ],
    ];

  public function defaultConfiguration() {
    return [
      'rid' => '',
    ];
  }

  /** * {@inheritdoc} */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $roles = user_role_names(TRUE);
    unset($roles[RoleInterface::AUTHENTICATED_ID]);
    $form['rid'] = [
      '#type' => 'radios',
      '#title' => $this->t('Role'),
      '#options' => $roles,
      '#default_value' => $this->configuration['rid'],
      '#required' => TRUE,
    ];
    return $form;
  }

  
$form['filter']['settings'] = [
      '#type' => 'container',
      '#attributes' => ['class' => ['entity_reference-settings']],
      '#process' => [['\Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem', 'formProcessMergeParent']],
    ];

    if ($configuration['filter']['type'] == 'role') {
      $form['filter']['settings']['role'] = [
        '#type' => 'checkboxes',
        '#title' => $this->t('Restrict to the selected roles'),
        '#required' => TRUE,
        '#options' => array_diff_key(user_role_names(TRUE)[RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID]),
        '#default_value' => $configuration['filter']['role'],
      ];
    }

    $form += parent::buildConfigurationForm($form$form_state);

    return $form;
  }

  /** * {@inheritdoc} */
/** * {@inheritdoc} * * Replace the configured permission with a filter by all roles that have this * permission. */
  public function query() {
    // @todo user_role_names() should maybe support multiple permissions.     $rids = [];
    // Get all role IDs that have the configured permissions.     foreach ($this->value as $permission) {
      $roles = user_role_names(FALSE, $permission);
      // user_role_names() returns an array with the role IDs as keys, so take       // the array keys and merge them with previously found role IDs.       $rids = array_merge($ridsarray_keys($roles));
    }
    // Remove any duplicate role IDs.     $rids = array_unique($rids);
    $this->value = $rids;

    // $this->value contains the role IDs that have the configured permission.     parent::query();
  }

}
/** * {@inheritdoc} */
  public function buildRow(EntityInterface $entity) {
    $row['username']['data'] = [
      '#theme' => 'username',
      '#account' => $entity,
    ];
    $row['status'] = $entity->isActive() ? $this->t('active') : $this->t('blocked');

    $roles = user_role_names(TRUE);
    unset($roles[RoleInterface::AUTHENTICATED_ID]);
    $users_roles = [];
    foreach ($entity->getRoles() as $role) {
      if (isset($roles[$role])) {
        $users_roles[] = $roles[$role];
      }
    }
    asort($users_roles);
    $row['roles']['data'] = [
      '#theme' => 'item_list',
      '#items' => $users_roles,
    ];

  public function buildForm(array $form, FormStateInterface $form_state) {
    // Administrative role option.     $form['admin_role'] = [
      '#type' => 'details',
      '#title' => $this->t('Administrator role'),
      '#open' => TRUE,
    ];
    // Do not allow users to set the anonymous or authenticated user roles as     // the administrator role.     $roles = user_role_names(TRUE);
    unset($roles[RoleInterface::AUTHENTICATED_ID]);
    $admin_roles = $this->roleStorage->getQuery()
      ->condition('is_admin', TRUE)
      ->execute();
    $default_value = reset($admin_roles);
    $form['admin_role']['user_admin_role'] = [
      '#type' => 'select',
      '#title' => $this->t('Administrator role'),
      '#empty_value' => '',
      '#default_value' => $default_value,
      '#options' => $roles,
      

class UserRole extends ConditionPluginBase {

  /** * {@inheritdoc} */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['roles'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('When the user has the following roles'),
      '#default_value' => $this->configuration['roles'],
      '#options' => array_map('\Drupal\Component\Utility\Html::escape', user_role_names()),
      '#description' => $this->t('If you select no roles, the condition will evaluate to TRUE for all users.'),
    ];
    return parent::buildConfigurationForm($form$form_state);
  }

  /** * {@inheritdoc} */
  public function defaultConfiguration() {
    return [
      'roles' => [],
    ]

  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')
    );
  }

  public function getValueOptions() {
    $this->valueOptions = user_role_names(TRUE);
    unset($this->valueOptions[RoleInterface::AUTHENTICATED_ID]);
    return $this->valueOptions;

  }

  /** * Override empty and not empty operator labels to be clearer for user roles. */
  public function operators() {
    $operators = parent::operators();
    $operators['empty']['title'] = $this->t("Only has the 'authenticated user' 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 {
      $rids = user_role_names();
      $rid = reset($this->options['role']);
      return $rids[$rid];
    }
  }

  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['role'] = ['default' => []];

    return $options;
  }

  
Home | Imprint | This part of the site doesn't use cookies.