You are here

class InviteByEmail in Invite 8

Class for Invite by Email.

Plugin annotation


@Plugin(
  id="invite_by_email",
  label = @Translation("Invite By Email")
)

Hierarchy

Expanded class hierarchy of InviteByEmail

1 file declares its use of InviteByEmail
InviteResendForm.php in src/Form/InviteResendForm.php

File

modules/invite_by_email/src/Plugin/Invite/InviteByEmail.php, line 21

Namespace

Drupal\invite_by_email\Plugin\Invite
View source
class InviteByEmail extends PluginBase implements InvitePluginInterface, ContainerFactoryPluginInterface {
  use StringTranslationTrait;

  /**
   * The Messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Getter for the messenger service.
   *
   * @return \Drupal\Core\Messenger\MessengerInterface
   */
  public function getMessenger() {
    return $this->messenger;
  }

  /**
   * Constructs invite_by_email plugin.
   *
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MessengerInterface $messenger) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('messenger'));
  }

  /**
   * {@inheritdoc}
   */
  public function send($invite) {

    /*
     * @var $token \Drupal\token\Token
     * @var $mail \Drupal\Core\Mail\MailManager
     */
    $bubbleable_metadata = new BubbleableMetadata();
    $token = \Drupal::service('token');
    $mail = \Drupal::service('plugin.manager.mail');
    $mail_key = $invite
      ->get('type')->value;

    // Prepare message.
    $message = $mail
      ->mail('invite_by_email', $mail_key, $invite
      ->get('field_invite_email_address')->value, $invite->activeLangcode, [], $invite
      ->getOwner()
      ->getEmail(), FALSE);

    // If HTML email.
    if (unserialize(\Drupal::config('invite.invite_type.' . $invite
      ->get('type')->value)
      ->get('data'))['html_email']) {
      $message['headers']['Content-Type'] = 'text/html; charset=UTF-8;';
    }
    $message['subject'] = $token
      ->replace($invite
      ->get('field_invite_email_subject')->value, [
      'invite' => $invite,
    ], [], $bubbleable_metadata);
    $body = [
      '#theme' => 'invite_by_email',
      '#body' => $token
        ->replace($invite
        ->get('field_invite_email_body')->value, [
        'invite' => $invite,
      ], [], $bubbleable_metadata),
    ];
    $message['body'] = \Drupal::service('renderer')
      ->render($body)
      ->__toString();

    // Send.
    $system = $mail
      ->getInstance([
      'module' => 'invite_by_email',
      'key' => $mail_key,
    ]);
    $result = $system
      ->mail($message);
    if ($result) {
      $this
        ->getMessenger()
        ->addStatus($this
        ->t('Invitation has been sent.'));
      $mail_user = $message['to'];
      \Drupal::logger('invite')
        ->notice('Invitation has been sent for: @mail_user.', [
        '@mail_user' => $mail_user,
      ]);
    }
    else {
      $this
        ->getMessenger()
        ->addStatus($this
        ->t('Failed to send a message.'), 'error');
      \Drupal::logger('invite')
        ->error('Failed to send a message.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InviteByEmail::$messenger protected property The Messenger service.
InviteByEmail::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
InviteByEmail::getMessenger public function Getter for the messenger service.
InviteByEmail::send public function Plugin send method. Overrides InvitePluginInterface::send
InviteByEmail::__construct public function Constructs invite_by_email plugin. Overrides PluginBase::__construct
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.
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.