You are here

class EmailField in Workbench Email 8

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

Provides a recipient type of an email field.

Plugin annotation


@RecipientType(
  id = "email",
  title = @Translation("Email field"),
  description = @Translation("Send to email addresses in email field."),
  settings = {
    "fields" = {},
  },
)

Hierarchy

Expanded class hierarchy of EmailField

File

src/Plugin/RecipientType/EmailField.php, line 31

Namespace

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

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

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * Moderation info service.
   *
   * @var \Drupal\workbench_moderation\ModerationInformationInterface|\Drupal\content_moderation\ModerationInformationInterface
   */
  protected $moderationInfo;

  /**
   * Constructs a new EmailField 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.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   * @param \Drupal\workbench_moderation\ModerationInformationInterface|\Drupal\content_moderation\ModerationInformationInterface $moderation_info
   *   Moderation info service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, $moderation_info) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
    $this->entityFieldManager = $entity_field_manager;
    $this->moderationInfo = $moderation_info;
  }

  /**
   * {@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'), $container
      ->get('entity_field.manager'), $container
      ->has('workbench_moderation.moderation_information') ? $container
      ->get('workbench_moderation.moderation_information') : $container
      ->get('content_moderation.moderation_information'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

    // Add the fields.
    $fields = $this->entityFieldManager
      ->getFieldMapByFieldType('email');
    $field_options = [];
    foreach ($fields as $entity_type_id => $entity_type_fields) {
      $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
      if (!$this
        ->isModeratableEntityType($entity_type)) {

        // These fields are irrelevant, the entity type isn't moderated.
        continue;
      }
      $base = $this->entityFieldManager
        ->getBaseFieldDefinitions($entity_type_id);
      foreach ($entity_type_fields as $field_name => $field_detail) {
        if (in_array($field_name, array_keys($base), TRUE)) {
          continue;
        }
        $sample_bundle = reset($field_detail['bundles']);
        $sample_field = $this->entityTypeManager
          ->getStorage('field_config')
          ->load($entity_type_id . '.' . $sample_bundle . '.' . $field_name);
        if ($sample_field) {
          $field_options[$entity_type_id . ':' . $field_name] = $sample_field
            ->label() . ' (' . $entity_type
            ->getLabel() . ')';
        }
      }
    }
    return [
      'fields' => [
        '#type' => 'checkboxes',
        '#title' => $this
          ->t('Email Fields'),
        '#description' => $this
          ->t('Send to mail address found in the selected fields'),
        '#options' => $field_options,
        '#default_value' => $this
          ->getFields(),
      ],
    ];
  }

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

  /**
   * {@inheritdoc}
   */
  public function prepareRecipients(ContentEntityInterface $entity, TemplateInterface $template) {
    $recipients = [];
    $fields = array_filter($this
      ->getFields(), function ($field_name) use ($entity) {
      list($entity_type, $field_name) = explode(':', $field_name, 2);
      return $entity_type === $entity
        ->getEntityTypeId() && $entity
        ->hasField($field_name) && !$entity->{$field_name}
        ->isEmpty();
    });
    foreach ($fields as $field) {
      list(, $field_name) = explode(':', $field, 2);

      /** @var \Drupal\Core\Field\FieldItemInterface $field_item */
      foreach ($entity->{$field_name} as $field_item) {
        $recipients[] = $this
          ->getEmailFromFieldItem($field_item);
      }
    }
    return $recipients;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEmailFromFieldItem(FieldItemInterface $field_item) {
    return $field_item
      ->get('value')
      ->getValue();
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $dependencies = [];
    $field_storage = $this->entityTypeManager
      ->getStorage('field_storage_config');
    foreach ($this
      ->getFields() as $identifier) {
      list($entity_type_id, $field_name) = explode(':', $identifier, 2);
      if ($field = $field_storage
        ->load("{$entity_type_id}.{$field_name}")) {
        $dependencies[$field
          ->getConfigDependencyKey()][] = $field
          ->getConfigDependencyName();
      }
    }
    return NestedArray::mergeDeep($dependencies, parent::calculateDependencies());
  }

  /**
   * {@inheritdoc}
   */
  public function onDependencyRemoval(array $dependencies) {
    $removed_fields = array_reduce($dependencies['config'], function (array $carry, $item) {
      if (!$item instanceof FieldStorageConfigInterface) {
        return $carry;
      }
      $carry[] = sprintf('%s:%s', $item
        ->getTargetEntityTypeId(), $item
        ->getName());
      return $carry;
    }, []);
    if ($removed_fields && array_intersect($removed_fields, $this
      ->getFields())) {
      $this
        ->setFields(array_diff($this
        ->getFields(), $removed_fields));
      return TRUE;
    }
    return FALSE;
  }

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

  /**
   * Sets roles.
   *
   * @param array $fields
   *   Field IDs in {entity_type}:{field_name} format.
   *
   * @return $this
   */
  protected function setFields(array $fields) {
    $configuration = $this
      ->getConfiguration();
    $configuration['settings']['fields'] = $fields;
    $this
      ->setConfiguration($configuration);
    return $this;
  }

  /**
   * Determines if an entity type has been marked as moderatable.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   An entity type object.
   *
   * @return bool
   *   TRUE if this entity type has been marked as moderatable, FALSE otherwise.
   */
  protected function isModeratableEntityType(EntityTypeInterface $entity_type) {
    if (method_exists($this->moderationInfo, 'isModeratableEntityType')) {
      return $this->moderationInfo
        ->isModeratableEntityType($entity_type);
    }
    else {
      return $this->moderationInfo
        ->canModerateEntitiesOfEntityType($entity_type);
    }
  }

}

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
EmailField::$entityFieldManager protected property The entity field manager.
EmailField::$entityTypeManager protected property The entity type manager.
EmailField::$moderationInfo protected property Moderation info service.
EmailField::buildConfigurationForm public function Generates a recipient types's settings form. Overrides RecipientTypeBase::buildConfigurationForm 1
EmailField::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides RecipientTypeBase::calculateDependencies
EmailField::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
EmailField::getEmailFromFieldItem protected function 1
EmailField::getFields protected function Gets value of roles.
EmailField::isModeratableEntityType protected function Determines if an entity type has been marked as moderatable.
EmailField::onDependencyRemoval public function Informs the plugin that a dependency of the recipient type will be deleted. Overrides RecipientTypeBase::onDependencyRemoval
EmailField::prepareRecipients public function Returns email address(s) matching this recipient type's configuration. Overrides RecipientTypeBase::prepareRecipients
EmailField::setFields protected function Sets roles.
EmailField::submitConfigurationForm public function Form submission handler. Overrides RecipientTypeBase::submitConfigurationForm
EmailField::__construct public function Constructs a new EmailField object. Overrides RecipientTypeBase::__construct
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
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.