View source
<?php
namespace Drupal\social_user\Plugin\Action;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\role_delegation\DelegatableRolesInterface;
use Drupal\user\Plugin\Action\ChangeUserRoleBase;
use Drupal\user\RoleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SocialRemoveRoleUser extends ChangeUserRoleBase implements ContainerFactoryPluginInterface {
protected $currentUser;
protected $delegatableRoles;
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;
}
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'));
}
public function execute($account = NULL) {
$rid = $this->configuration['rid'];
if ($account !== FALSE && $account
->hasRole($rid)) {
$account->original = clone $account;
$account
->removeRole($rid);
$account
->save();
}
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$roles = $this->delegatableRoles
->getAssignableRoles($this->currentUser);
unset($roles[RoleInterface::AUTHENTICATED_ID]);
$form['rid'] = [
'#type' => 'radios',
'#title' => t('Role'),
'#options' => $roles,
'#default_value' => $this->configuration['rid'],
'#required' => TRUE,
];
return $form;
}
}