You are here

function role_delegation_options_list_alter in Role Delegation 8

Implements hook_options_list_alter().

File

./role_delegation.module, line 145
Allows admins to grant roles the authority to assign selected roles to users.

Code

function role_delegation_options_list_alter(array &$options, array $context) {

  /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
  $field_definition = $context['fieldDefinition'];

  // By default, ALL the entities for a given type will be used for the options
  // on an enity reference field, but we only want the user to be able to choose
  // from the roles they can assign.
  if ($field_definition
    ->getTargetEntityTypeId() === 'user' && $field_definition
    ->getName() === 'role_change') {
    $current_user = \Drupal::currentUser();
    $options = \Drupal::service('delegatable_roles')
      ->getAssignableRoles($current_user);
  }
}