You are here

class SendEmailAction in CRM Core 8

Same name and namespace in other branches
  1. 8.3 modules/crm_core_contact/src/Plugin/Action/SendEmailAction.php \Drupal\crm_core_contact\Plugin\Action\SendEmailAction
  2. 8.2 modules/crm_core_contact/src/Plugin/Action/SendEmailAction.php \Drupal\crm_core_contact\Plugin\Action\SendEmailAction

Sends e-mail to contacts.

Plugin annotation


@Action(
  id = "send_email_action",
  label = @Translation("Send an e-mail to contacts"),
  type = "crm_core_contact"
)

Hierarchy

Expanded class hierarchy of SendEmailAction

File

modules/crm_core_contact/src/Plugin/Action/SendEmailAction.php, line 24

Namespace

Drupal\crm_core_contact\Plugin\Action
View source
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');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurableActionBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 1
ConfigurableActionBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 8
ConfigurableActionBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ConfigurableActionBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ConfigurableActionBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 2
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.
SendEmailAction::$languageManager protected property The language manager.
SendEmailAction::$mailManager protected property The mail manager.
SendEmailAction::$token protected property The token service.
SendEmailAction::access public function Checks object access. Overrides ActionInterface::access
SendEmailAction::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
SendEmailAction::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
SendEmailAction::execute public function Executes the plugin. Overrides ExecutableInterface::execute
SendEmailAction::executeMultiple public function Executes the plugin for an array of objects. Overrides ActionBase::executeMultiple
SendEmailAction::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
SendEmailAction::__construct public function Constructs a EmailAction object. Overrides ConfigurableActionBase::__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.