You are here

class CourierTemplateCollection in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Action/CourierTemplateCollection.php \Drupal\rng\Plugin\Action\CourierTemplateCollection
  2. 3.x src/Plugin/Action/CourierTemplateCollection.php \Drupal\rng\Plugin\Action\CourierTemplateCollection

Creates a template collection and provides a user interface to its templates.

Plugin annotation


@Action(
  id = "rng_courier_message",
  label = @Translation("Send message"),
  type = "registration"
)

Hierarchy

Expanded class hierarchy of CourierTemplateCollection

1 file declares its use of CourierTemplateCollection
RngEntityModel.php in src/RngEntityModel.php

File

src/Plugin/Action/CourierTemplateCollection.php, line 25

Namespace

Drupal\rng\Plugin\Action
View source
class CourierTemplateCollection extends ConfigurableActionBase implements ContainerFactoryPluginInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * The RNG event manager.
   *
   * @var \Drupal\rng\EventManagerInterface
   */
  protected $eventManager;

  /**
   * The courier manager.
   *
   * @var \Drupal\courier\Service\CourierManagerInterface
   */
  protected $courierManager;

  /**
   * Constructs a RegistrantBasicEmail 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\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\rng\EventManagerInterface $event_manager
   *   The RNG event manager.
   * @param \Drupal\courier\Service\CourierManagerInterface $courier_manager
   *   The courier manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, EventManagerInterface $event_manager, CourierManagerInterface $courier_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityManager = $entity_manager;
    $this->eventManager = $event_manager;
    $this->courierManager = $courier_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity.manager'), $container
      ->get('rng.event_manager'), $container
      ->get('courier.manager'));
  }

  /**
   * {@inheritdoc}
   *
   * @return array
   *   - template_collection: integer: ID of a courier_template_collection
   *     entity. Automatically filled after first submission.
   */
  public function defaultConfiguration() {
    return array(
      'template_collection' => NULL,
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    if ($template_collection = $this
      ->getTemplateCollection()) {
      $form['template_collection']['#markup'] = $this
        ->t('Template collection #@id', [
        '@id' => $template_collection
          ->id(),
      ]);
    }
    else {
      drupal_set_message('No template collection entity found.', 'warning');
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $configuration = $this
      ->getConfiguration();

    // Create new.
    if (!isset($configuration['template_collection'])) {
      $template_collection = TemplateCollection::create();
      if ($template_collection
        ->save()) {
        $this->courierManager
          ->addTemplates($template_collection);
        $template_collection
          ->save();
      }
      $this->configuration['template_collection'] = $template_collection
        ->id();
    }
  }

  /**
   * Sends the message.
   *
   * @param array $context
   *   An associative array defining context.
   *   - \Drupal\rng\RegistrationInterface[] registrations: An array of
   *     registrations to send the message.
   */
  public function execute($context = NULL) {
    if (!isset($context['registrations'])) {
      return;
    }
    if (!is_array($context['registrations'])) {
      return;
    }
    if ($collection_original = $this
      ->getTemplateCollection()) {
      foreach ($context['registrations'] as $registration) {
        $options = [];

        /** @var \Drupal\rng\RegistrationInterface $registration */
        if (($event = $registration
          ->getEvent()) instanceof EntityInterface) {
          $event_meta = $this->eventManager
            ->getMeta($event);
          $options['channels']['courier_email']['reply_to'] = $event_meta
            ->getReplyTo();
          $collection_original
            ->setTokenValue($event
            ->getEntityTypeId(), $event);
        }
        $collection = clone $collection_original;
        $collection
          ->setTokenValue('registration', $registration);
        foreach ($registration
          ->getRegistrants() as $registrant) {
          $identity = $registrant
            ->getIdentity();
          $this->courierManager
            ->sendMessage($collection, $identity, $options);
        }
      }
    }
  }

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

  /**
   * Get the courier_template_collection object associated with this action.
   *
   * @return \Drupal\courier\TemplateCollectionInterface|NULL
   *   A courier_template_collection object. NULL if it has not been created.
   */
  public function getTemplateCollection() {
    if (isset($this->configuration['template_collection'])) {
      return $this->entityManager
        ->getStorage('courier_template_collection')
        ->load($this->configuration['template_collection']);
    }
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActionBase::executeMultiple public function Executes the plugin for an array of objects. Overrides ActionInterface::executeMultiple 3
ConfigurableActionBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 1
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
CourierTemplateCollection::$courierManager protected property The courier manager.
CourierTemplateCollection::$entityManager protected property The entity type manager.
CourierTemplateCollection::$eventManager protected property The RNG event manager.
CourierTemplateCollection::access public function Checks object access. Overrides ActionInterface::access
CourierTemplateCollection::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
CourierTemplateCollection::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
CourierTemplateCollection::defaultConfiguration public function Overrides ConfigurableActionBase::defaultConfiguration
CourierTemplateCollection::execute public function Sends the message. Overrides ExecutableInterface::execute
CourierTemplateCollection::getTemplateCollection public function Get the courier_template_collection object associated with this action.
CourierTemplateCollection::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
CourierTemplateCollection::__construct public function Constructs a RegistrantBasicEmail object. Overrides ConfigurableActionBase::__construct
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.
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.