You are here

class Role in Workbench Email 8

Same name and namespace in other branches
  1. 2.x src/Plugin/RecipientType/Role.php \Drupal\workbench_email\Plugin\RecipientType\Role

Provides a recipient type of user role.

Plugin annotation


@RecipientType(
  id = "role",
  title = @Translation("Role"),
  description = @Translation("Send to all users with selected roles."),
  settings = {
    "roles" = {},
  },
)

Hierarchy

Expanded class hierarchy of Role

1 string reference to 'Role'
workbench_email.schema.yml in config/schema/workbench_email.schema.yml
config/schema/workbench_email.schema.yml

File

src/Plugin/RecipientType/Role.php, line 27

Namespace

Drupal\workbench_email\Plugin\RecipientType
View source
class Role extends RecipientTypeBase implements ContainerFactoryPluginInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new Role object.
   *
   * @param array $configuration
   *   Plugin configuration.
   * @param string $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@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'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $roles = array_filter($this->entityTypeManager
      ->getStorage('user_role')
      ->loadMultiple(), function (RoleInterface $role) {
      return !in_array($role
        ->id(), [
        RoleInterface::ANONYMOUS_ID,
        RoleInterface::AUTHENTICATED_ID,
      ], TRUE);
    });
    $role_options = array_map(function (RoleInterface $role) {
      return $role
        ->label();
    }, $roles);
    return [
      'roles' => [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Roles'),
        '#description' => $this
          ->t('Send to all users with selected roles'),
        '#options' => $role_options,
        '#default_value' => $this
          ->getRoles(),
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this
      ->setRoles(array_filter($form_state
      ->getValue('roles')));
    parent::submitConfigurationForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRecipients(ContentEntityInterface $entity, TemplateInterface $template) {
    $recipients = [];
    foreach ($this
      ->getRoles() as $role) {
      foreach ($this->entityTypeManager
        ->getStorage('user')
        ->loadByProperties([
        'roles' => $role,
        'status' => 1,
      ]) as $account) {
        $recipients[] = $account
          ->getEmail();
      }
    }
    return $recipients;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $dependencies = [];
    $role_storage = $this->entityTypeManager
      ->getStorage('user_role');
    foreach ($role_storage
      ->loadMultiple($this
      ->getRoles()) as $role) {
      $dependencies[$role
        ->getConfigDependencyKey()][] = $role
        ->getConfigDependencyName();
    }
    return NestedArray::mergeDeep($dependencies, parent::calculateDependencies());
  }

  /**
   * {@inheritdoc}
   */
  public function onDependencyRemoval(array $dependencies) {
    $removed_roles = array_reduce($dependencies['config'], function (array $carry, $item) {
      if (!$item instanceof RoleInterface) {
        return $carry;
      }
      $carry[] = $item
        ->id();
      return $carry;
    }, []);
    if ($removed_roles && array_intersect($removed_roles, $this
      ->getRoles())) {
      $this
        ->setRoles(array_diff($this
        ->getRoles(), $removed_roles));
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Gets value of roles.
   *
   * @return array
   *   Value of roles
   */
  protected function getRoles() {
    return $this
      ->getConfiguration()['settings']['roles'];
  }

  /**
   * Sets roles.
   *
   * @param array $roles
   *   Role IDs.
   *
   * @return $this
   */
  protected function setRoles(array $roles) {
    $configuration = $this
      ->getConfiguration();
    $configuration['settings']['roles'] = $roles;
    $this
      ->setConfiguration($configuration);
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
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 3
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.
PluginWithFormsTrait::getFormClass public function
PluginWithFormsTrait::hasFormClass public function
RecipientTypeBase::$provider public property The name of the provider that owns this recipient type.
RecipientTypeBase::$settings public property An associative array containing the configured settings of this recipient type.
RecipientTypeBase::$status public property A Boolean indicating whether this recipient type is enabled.
RecipientTypeBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration
RecipientTypeBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
RecipientTypeBase::getDescription public function Returns the administrative description for this recipient type plugin. Overrides RecipientTypeInterface::getDescription
RecipientTypeBase::getLabel public function Returns the administrative label for this recipient type plugin. Overrides RecipientTypeInterface::getLabel
RecipientTypeBase::isEnabled public function Checks status. Overrides RecipientTypeInterface::isEnabled
RecipientTypeBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
RecipientTypeBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
Role::$entityTypeManager protected property The entity type manager.
Role::buildConfigurationForm public function Generates a recipient types's settings form. Overrides RecipientTypeBase::buildConfigurationForm 1
Role::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides RecipientTypeBase::calculateDependencies
Role::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Role::getRoles protected function Gets value of roles.
Role::onDependencyRemoval public function Informs the plugin that a dependency of the recipient type will be deleted. Overrides RecipientTypeBase::onDependencyRemoval
Role::prepareRecipients public function Returns email address(s) matching this recipient type's configuration. Overrides RecipientTypeBase::prepareRecipients 1
Role::setRoles protected function Sets roles.
Role::submitConfigurationForm public function Form submission handler. Overrides RecipientTypeBase::submitConfigurationForm
Role::__construct public function Constructs a new Role object. Overrides RecipientTypeBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.