You are here

class SocialSendEmail in Open Social 8.6

Same name in this branch
  1. 8.6 modules/social_features/social_group/src/Plugin/Action/SocialSendEmail.php \Drupal\social_group\Plugin\Action\SocialSendEmail
  2. 8.6 modules/social_features/social_user/src/Plugin/Action/SocialSendEmail.php \Drupal\social_user\Plugin\Action\SocialSendEmail
Same name and namespace in other branches
  1. 8.9 modules/social_features/social_user/src/Plugin/Action/SocialSendEmail.php \Drupal\social_user\Plugin\Action\SocialSendEmail
  2. 8.5 modules/social_features/social_user/src/Plugin/Action/SocialSendEmail.php \Drupal\social_user\Plugin\Action\SocialSendEmail
  3. 8.7 modules/social_features/social_user/src/Plugin/Action/SocialSendEmail.php \Drupal\social_user\Plugin\Action\SocialSendEmail
  4. 8.8 modules/social_features/social_user/src/Plugin/Action/SocialSendEmail.php \Drupal\social_user\Plugin\Action\SocialSendEmail
  5. 10.3.x modules/social_features/social_user/src/Plugin/Action/SocialSendEmail.php \Drupal\social_user\Plugin\Action\SocialSendEmail
  6. 10.0.x modules/social_features/social_user/src/Plugin/Action/SocialSendEmail.php \Drupal\social_user\Plugin\Action\SocialSendEmail
  7. 10.1.x modules/social_features/social_user/src/Plugin/Action/SocialSendEmail.php \Drupal\social_user\Plugin\Action\SocialSendEmail
  8. 10.2.x modules/social_features/social_user/src/Plugin/Action/SocialSendEmail.php \Drupal\social_user\Plugin\Action\SocialSendEmail

An example action covering most of the possible options.

Plugin annotation


@Action(
  id = "social_user_send_email",
  label = @Translation("Send email"),
  type = "user",
  confirm = TRUE,
)

Hierarchy

  • class \Drupal\social_user\Plugin\Action\SocialSendEmail extends \Drupal\views_bulk_operations\Action\ViewsBulkOperationsActionBase implements \Drupal\views_bulk_operations\Action\ViewsBulkOperationsPreconfigurationInterface, ContainerFactoryPluginInterface

Expanded class hierarchy of SocialSendEmail

2 files declare their use of SocialSendEmail
SocialEventManagersSendEmail.php in modules/social_features/social_event/modules/social_event_managers/src/Plugin/Action/SocialEventManagersSendEmail.php
SocialSendEmail.php in modules/social_features/social_group/src/Plugin/Action/SocialSendEmail.php

File

modules/social_features/social_user/src/Plugin/Action/SocialSendEmail.php, line 29

Namespace

Drupal\social_user\Plugin\Action
View source
class SocialSendEmail extends ViewsBulkOperationsActionBase implements ContainerFactoryPluginInterface, ViewsBulkOperationsPreconfigurationInterface {

  /**
   * The token service.
   *
   * @var \Drupal\Core\Utility\Token
   */
  protected $token;

  /**
   * The user storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $storage;

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The mail manager.
   *
   * @var \Drupal\Core\Mail\MailManagerInterface
   */
  protected $mailManager;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The email validator.
   *
   * @var \Egulias\EmailValidator\EmailValidator
   */
  protected $emailValidator;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a SocialSendEmail object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Utility\Token $token
   *   The token service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
   *   The mail manager.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Egulias\EmailValidator\EmailValidator $email_validator
   *   The email validator.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token, EntityTypeManagerInterface $entity_type_manager, LoggerInterface $logger, MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager, EmailValidator $email_validator) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->token = $token;
    $this->storage = $entity_type_manager
      ->getStorage('user');
    $this->logger = $logger;
    $this->mailManager = $mail_manager;
    $this->languageManager = $language_manager;
    $this->emailValidator = $email_validator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('token'), $container
      ->get('entity_type.manager'), $container
      ->get('logger.factory')
      ->get('action'), $container
      ->get('plugin.manager.mail'), $container
      ->get('language_manager'), $container
      ->get('email.validator'));
  }

  /**
   * {@inheritdoc}
   */
  public function execute($entity = NULL) {

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    if (!$entity
      ->getEntityTypeId() === 'user') {
      $this->logger
        ->notice('Can not send e-mail for %entity', [
        '%entity' => $entity
          ->getEntityTypeId() . ':' . $entity
          ->id(),
      ]);
      return;
    }

    /** @var \Drupal\user\Entity\User $entity */
    if ($entity) {
      $langcode = $entity
        ->getPreferredLangcode();
    }
    else {
      $langcode = $this->languageManager
        ->getDefaultLanguage()
        ->getId();
    }
    $params = [
      'context' => $this->configuration,
    ];
    $email = $this
      ->getEmail($entity);
    $message = $this->mailManager
      ->mail('system', 'action_send_email', $email, $langcode, $params, $this->configuration['reply']);

    // Error logging is handled by \Drupal\Core\Mail\MailManager::mail().
    if ($message['result']) {
      $this->logger
        ->notice('Sent email to %recipient', [
        '%recipient' => $email,
      ]);
    }
    return $this
      ->t('Send email');
  }

  /**
   * Returns the email address of this account.
   *
   * @param \Drupal\user\UserInterface $account
   *   The user object.
   *
   * @return string|null
   *   The email address, or NULL if the account is anonymous or the user does
   *   not have an email address.
   */
  public function getEmail(UserInterface $account) {
    return $account
      ->getEmail();
  }

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

  /**
   * Configuration form builder.
   *
   * If this method has implementation, the action is
   * considered to be configurable.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   *
   * @return array
   *   The configuration form.
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['reply'] = [
      '#type' => 'email',
      '#title' => $this
        ->t('Reply-to'),
      '#description' => $this
        ->t('A Reply-To address is the email address that receives messages sent from those who select Reply in their email clients.'),
    ];
    $form['subject'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Subject'),
      '#required' => TRUE,
      '#default_value' => $form_state
        ->getValue('subject'),
      '#maxlength' => '254',
    ];
    $form['message'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Message'),
      '#required' => TRUE,
      '#default_value' => $form_state
        ->getValue('message'),
      '#cols' => '80',
      '#rows' => '20',
    ];
    $form['#title'] = $this
      ->t('Send an email to :selected_count members', [
      ':selected_count' => $this->context['selected_count'],
    ]);
    if (isset($form['list'])) {
      unset($form['list']);
    }
    $form['actions']['submit']['#value'] = $this
      ->t('Send email');
    $classes = [
      'button',
      'btn',
      'waves-effect',
      'waves-btn',
    ];
    $form['actions']['submit']['#attributes']['class'] = [
      'button--primary',
      'js-form-submit',
      'form-submit',
      'js-form-submit',
      'btn-raised',
      'btn-primary',
      'waves-light',
    ] + $classes;
    $form['actions']['cancel']['#attributes']['class'] = [
      'button--danger',
      'btn-flat',
    ] + $classes;
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {

    // @TODO Check for proper access here.
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SocialSendEmail::$configFactory protected property The configuration factory.
SocialSendEmail::$emailValidator protected property The email validator.
SocialSendEmail::$languageManager protected property The language manager.
SocialSendEmail::$logger protected property A logger instance.
SocialSendEmail::$mailManager protected property The mail manager.
SocialSendEmail::$storage protected property The user storage.
SocialSendEmail::$token protected property The token service.
SocialSendEmail::access public function 2
SocialSendEmail::buildConfigurationForm public function Configuration form builder. 2
SocialSendEmail::buildPreConfigurationForm public function 1
SocialSendEmail::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
SocialSendEmail::execute public function 2
SocialSendEmail::getEmail public function Returns the email address of this account. 1
SocialSendEmail::__construct public function Constructs a SocialSendEmail object. 1