You are here

class SocialAddRoleUser in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_user/src/Plugin/Action/SocialAddRoleUser.php \Drupal\social_user\Plugin\Action\SocialAddRoleUser
  2. 8.7 modules/social_features/social_user/src/Plugin/Action/SocialAddRoleUser.php \Drupal\social_user\Plugin\Action\SocialAddRoleUser
  3. 8.8 modules/social_features/social_user/src/Plugin/Action/SocialAddRoleUser.php \Drupal\social_user\Plugin\Action\SocialAddRoleUser
  4. 10.3.x modules/social_features/social_user/src/Plugin/Action/SocialAddRoleUser.php \Drupal\social_user\Plugin\Action\SocialAddRoleUser
  5. 10.0.x modules/social_features/social_user/src/Plugin/Action/SocialAddRoleUser.php \Drupal\social_user\Plugin\Action\SocialAddRoleUser
  6. 10.2.x modules/social_features/social_user/src/Plugin/Action/SocialAddRoleUser.php \Drupal\social_user\Plugin\Action\SocialAddRoleUser

Adds a role to a user but restricted by role.

Plugin annotation


@Action(
  id = "social_user_add_role_action",
  label = @Translation("Add a role to the selected users"),
  type = "social_user"
)

Hierarchy

Expanded class hierarchy of SocialAddRoleUser

1 file declares its use of SocialAddRoleUser
social_user.module in modules/social_features/social_user/social_user.module
The social user module alterations.

File

modules/social_features/social_user/src/Plugin/Action/SocialAddRoleUser.php, line 23

Namespace

Drupal\social_user\Plugin\Action
View source
class SocialAddRoleUser extends ChangeUserRoleBase implements ContainerFactoryPluginInterface {

  /**
   * The account proxy interface.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The role delegation delegatable roles interface.
   *
   * @var \Drupal\role_delegation\DelegatableRolesInterface
   */
  protected $delegatableRoles;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeInterface $entity_type, AccountProxyInterface $currentUser, DelegatableRolesInterface $delegatableRoles) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type);
    $this->entityType = $entity_type;
    $this->currentUser = $currentUser
      ->getAccount();
    $this->delegatableRoles = $delegatableRoles;
  }

  /**
   * {@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')
      ->getDefinition('user_role'), $container
      ->get('current_user'), $container
      ->get('delegatable_roles'));
  }

  /**
   * {@inheritdoc}
   */
  public function execute($account = NULL) {
    $rid = $this->configuration['rid'];

    // Skip adding the role to the user if they already have it.
    if ($account !== FALSE && !$account
      ->hasRole($rid)) {

      // For efficiency manually save the original account before applying
      // any changes.
      $account->original = clone $account;
      $account
        ->addRole($rid);
      $account
        ->save();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $roles = $this->delegatableRoles
      ->getAssignableRoles($this->currentUser);

    // Remove the authenticated role.
    unset($roles[RoleInterface::AUTHENTICATED_ID]);
    $form['rid'] = [
      '#type' => 'radios',
      '#title' => t('Role'),
      '#options' => $roles,
      '#default_value' => $this->configuration['rid'],
      '#required' => TRUE,
    ];
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActionBase::executeMultiple public function Executes the plugin for an array of objects. Overrides ActionInterface::executeMultiple 3
ChangeUserRoleBase::$entityType protected property The user role entity type.
ChangeUserRoleBase::access public function Checks object access. Overrides ActionInterface::access
ChangeUserRoleBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides ConfigurableActionBase::calculateDependencies
ChangeUserRoleBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableActionBase::defaultConfiguration
ChangeUserRoleBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
ConfigurableActionBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ConfigurableActionBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ConfigurableActionBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 2
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
SocialAddRoleUser::$currentUser protected property The account proxy interface.
SocialAddRoleUser::$delegatableRoles protected property The role delegation delegatable roles interface.
SocialAddRoleUser::buildConfigurationForm public function Form constructor. Overrides ChangeUserRoleBase::buildConfigurationForm
SocialAddRoleUser::create public static function Creates an instance of the plugin. Overrides ChangeUserRoleBase::create
SocialAddRoleUser::execute public function Executes the plugin. Overrides ExecutableInterface::execute
SocialAddRoleUser::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides ChangeUserRoleBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.