You are here

public function SpoolStorage::getRecipientHandler in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 src/Spool/SpoolStorage.php \Drupal\simplenews\Spool\SpoolStorage::getRecipientHandler()

Builds a recipient handler class for a given newsletter issue.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $issue: The newsletter issue to be sent.

array $edited_values: (optional) Modified values, if called from an edit form.

bool $return_options: (optional, defaults to FALSE) If set, also return the set of valid options for choice of recipient handler.

Return value

\Drupal\simplenews\RecipientHandler\RecipientHandlerInterface|array A constructed recipient handler plugin. If $return_options is set then the return is an array of two items: the recipient handler plugin and the result of RecipientHandlerManager::getOptions().

Throws

\Exception If the handler class does not exist.

Overrides SpoolStorageInterface::getRecipientHandler

1 call to SpoolStorage::getRecipientHandler()
SpoolStorage::addIssue in src/Spool/SpoolStorage.php
Adds a newsletter issue to the mail spool.

File

src/Spool/SpoolStorage.php, line 306

Class

SpoolStorage
Default database spool storage.

Namespace

Drupal\simplenews\Spool

Code

public function getRecipientHandler(ContentEntityInterface $issue, array $edited_values = NULL, $return_options = FALSE) {
  $field = $issue
    ->get('simplenews_issue');
  $newsletter_ids = $field
    ->isEmpty() ? [] : array_map(function ($i) {
    return $i['target_id'];
  }, $field
    ->getValue());
  $newsletter_id = $edited_values['target_id'] ?? $newsletter_ids[0] ?? NULL;
  $handler = $edited_values['handler'] ?? $field->handler ?: 'simplenews_all';

  // Ensure the requested handler is a valid option.
  $options = $this->recipientHandlerManager
    ->getOptions($newsletter_id);
  if (!isset($options[$handler])) {
    reset($options);
    $handler = key($options);
  }
  $handler_settings = $edited_values['handler_settings'] ?? $field->handler_settings;
  $handler_settings['_issue'] = $issue;
  $handler_settings['_connection'] = $this->connection;
  $handler_settings['_newsletter_ids'] = $newsletter_ids;
  $recipient_handler = $this->recipientHandlerManager
    ->createInstance($handler, $handler_settings);
  return $return_options ? [
    $recipient_handler,
    $options,
  ] : $recipient_handler;
}