You are here

SendEmailAction.php in CRM Core 8.3

File

modules/crm_core_contact/src/Plugin/Action/SendEmailAction.php
View source
<?php

namespace Drupal\crm_core_contact\Plugin\Action;

use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Utility\Token;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Sends e-mail to contacts.
 *
 * @Action(
 *   id = "send_email_action",
 *   label = @Translation("Send an e-mail to contacts"),
 *   type = "crm_core_contact"
 * )
 */
class SendEmailAction extends ConfigurableActionBase implements ContainerFactoryPluginInterface {

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

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

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

  /**
   * Constructs a EmailAction 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\Mail\MailManagerInterface $mail_manager
   *   The mail manager.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token, MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->token = $token;
    $this->mailManager = $mail_manager;
    $this->languageManager = $language_manager;
  }

  /**
   * {@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('plugin.manager.mail'), $container
      ->get('language_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    return $return_as_object ? AccessResult::allowed() : AccessResult::allowed()
      ->isAllowed();
  }

  /**
   * {@inheritdoc}
   */
  public function executeMultiple(array $objects) {
    $subject = isset($this->configuration['subject']) ? $this->configuration['subject'] : '';
    $message = isset($this->configuration['message']) ? $this->configuration['message'] : '';
    foreach ($objects as $contact) {

      // Token replacement preparations.
      $data = [
        'crm_core_contact' => $contact,
      ];
      $options = [
        // Remove tokens that could not be found.
        'clear' => TRUE,
      ];
      $subject = $this->token
        ->replace($subject, $data, $options);
      $message = $this->token
        ->replace($message, $data, $options);
      $email = $contact
        ->getPrimaryEmail()->value;
      $params = [
        'subject' => $subject,
        'message' => $message,
      ];
      $langcode = $this->languageManager
        ->getCurrentLanguage()
        ->getId();
      $this->mailManager
        ->mail('crm_core_contact', 'send_email', $email, $langcode, $params);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function execute($object = NULL) {
    $this
      ->executeMultiple([
      $object,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = [];
    $form['subject'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Subject'),
      '#description' => $this
        ->t('The subject of the message.'),
      '#default_value' => $form_state
        ->getValue('subject', ''),
    ];
    $form['message'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Message'),
      '#description' => $this
        ->t('The message that should be sent.'),
      '#default_value' => $form_state
        ->getValue('message', ''),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['subject'] = $form_state
      ->getValue('subject');
    $this->configuration['message'] = $form_state
      ->getValue('message');
  }

}

Classes

Namesort descending Description
SendEmailAction Sends e-mail to contacts.