class EmailField in Workbench Email 2.x
Same name and namespace in other branches
- 8 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\workbench_email\Plugin\RecipientTypeBase implements RecipientTypeInterface uses PluginWithFormsTrait
- class \Drupal\workbench_email\Plugin\RecipientType\EmailField implements ContainerFactoryPluginInterface
- class \Drupal\workbench_email\Plugin\RecipientTypeBase implements RecipientTypeInterface uses PluginWithFormsTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of EmailField
File
- src/
Plugin/ RecipientType/ EmailField.php, line 31
Namespace
Drupal\workbench_email\Plugin\RecipientTypeView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
EmailField:: |
protected | property | The entity field manager. | |
EmailField:: |
protected | property | The entity type manager. | |
EmailField:: |
protected | property | Moderation info service. | |
EmailField:: |
public | function |
Generates a recipient types's settings form. Overrides RecipientTypeBase:: |
1 |
EmailField:: |
public | function |
Calculates dependencies for the configured plugin. Overrides RecipientTypeBase:: |
|
EmailField:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
EmailField:: |
protected | function | 1 | |
EmailField:: |
protected | function | Gets value of roles. | |
EmailField:: |
protected | function | Determines if an entity type has been marked as moderatable. | |
EmailField:: |
public | function |
Informs the plugin that a dependency of the recipient type will be deleted. Overrides RecipientTypeBase:: |
|
EmailField:: |
public | function |
Returns email address(s) matching this recipient type's configuration. Overrides RecipientTypeBase:: |
|
EmailField:: |
protected | function | Sets roles. | |
EmailField:: |
public | function |
Form submission handler. Overrides RecipientTypeBase:: |
|
EmailField:: |
public | function |
Constructs a new EmailField object. Overrides RecipientTypeBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginWithFormsTrait:: |
public | function | Implements \Drupal\Core\Plugin\PluginWithFormsInterface::getFormClass(). | |
PluginWithFormsTrait:: |
public | function | Implements \Drupal\Core\Plugin\PluginWithFormsInterface::hasFormClass(). | |
RecipientTypeBase:: |
public | property | The name of the provider that owns this recipient type. | |
RecipientTypeBase:: |
public | property | An associative array containing the configured settings of this recipient type. | |
RecipientTypeBase:: |
public | property | A Boolean indicating whether this recipient type is enabled. | |
RecipientTypeBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
|
RecipientTypeBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
RecipientTypeBase:: |
public | function |
Returns the administrative description for this recipient type plugin. Overrides RecipientTypeInterface:: |
|
RecipientTypeBase:: |
public | function |
Returns the administrative label for this recipient type plugin. Overrides RecipientTypeInterface:: |
|
RecipientTypeBase:: |
public | function |
Checks status. Overrides RecipientTypeInterface:: |
|
RecipientTypeBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
RecipientTypeBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |