abstract class RecipientHandlerBase in Simplenews 3.x
Same name and namespace in other branches
- 8.2 src/Plugin/simplenews/RecipientHandler/RecipientHandlerBase.php \Drupal\simplenews\Plugin\simplenews\RecipientHandler\RecipientHandlerBase
- 8 src/Plugin/simplenews/RecipientHandler/RecipientHandlerBase.php \Drupal\simplenews\Plugin\simplenews\RecipientHandler\RecipientHandlerBase
Base class for all Recipient Handler classes.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\simplenews\Plugin\simplenews\RecipientHandler\RecipientHandlerBase implements RecipientHandlerInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of RecipientHandlerBase
1 file declares its use of RecipientHandlerBase
- RecipientHandlerSiteMail.php in modules/
simplenews_demo/ src/ Plugin/ simplenews/ RecipientHandler/ RecipientHandlerSiteMail.php
File
- src/
Plugin/ simplenews/ RecipientHandler/ RecipientHandlerBase.php, line 12
Namespace
Drupal\simplenews\Plugin\simplenews\RecipientHandlerView source
abstract class RecipientHandlerBase extends PluginBase implements RecipientHandlerInterface {
/**
* The newsletter issue.
*
* @var \Drupal\Core\Entity\ContentEntityInterface
*/
protected $issue;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* The newsletter IDs.
*
* @var array
*/
protected $newsletterIds;
/**
* RecipientHandlerBase constructor.
*
* @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.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->issue = $configuration['_issue'];
$this->connection = $configuration['_connection'];
$this->newsletterIds = $configuration['_newsletter_ids'];
}
/**
* {@inheritdoc}
*/
public function count() {
$cache =& drupal_static(__METHOD__, []);
$cid = $this->pluginId . ':' . implode(':', $this->newsletterIds);
if (isset($cache[$cid])) {
return $cache[$cid];
}
$count = $this
->doCount();
if ($this
->cacheCount()) {
$cache[$cid] = $count;
}
return $count;
}
/**
* {@inheritdoc}
*/
public function settingsForm() {
return [];
}
/**
* Counts the number of recipients.
*
* Internal count function allowing the caller to perform caching.
*
* @return int
* Number of recipients.
*/
protected abstract function doCount();
/**
* Checks if the recipient count can be cached.
*
* Caching is allowed if the count depends only on the newsletter IDs, and
* does not vary with a specific issue or handler settings.
*
* @return bool
* TRUE if the count can be cached.
*/
protected function cacheCount() {
return FALSE;
}
/**
* Returns the newsletter ID.
*
* @return int
* Newsletter ID.
*
* @throws \Exception
* The configuration doesn't specify a single newsletter ID.
*/
protected function getNewsletterId() {
if (count($this->newsletterIds) != 1) {
throw new \Exception("Recipient handler requires a single newsletter ID.");
}
return $this->newsletterIds[0];
}
/**
* Adds an array of entries to the spool.
*
* The caller specifies the values for a field to define the recipient. The
* other fields are automatically defaulted based on the issue and
* newsletter.
*
* @param string $field
* Field to set: 'snid', 'data' (automatically serialised) or 'uid'
* (automatically stored in 'data' array with key 'uid').
* @param array $values
* Values to set for field.
*/
protected function addArrayToSpool($field, array $values) {
if (empty($values)) {
return;
}
$template = [
'entity_type' => $this->issue
->getEntityTypeId(),
'entity_id' => $this->issue
->id(),
'status' => SpoolStorageInterface::STATUS_PENDING,
'timestamp' => REQUEST_TIME,
'newsletter_id' => $this
->getNewsletterId(),
];
if ($field == 'uid') {
$field = 'data';
$values = array_map(function ($v) {
return [
'uid' => $v,
];
}, $values);
}
$insert = $this->connection
->insert('simplenews_mail_spool')
->fields(array_merge(array_keys($template), [
$field,
]));
foreach ($values as $value) {
$row = $template;
$row[$field] = $field == 'data' ? serialize($value) : $value;
$insert
->values($row);
}
$insert
->execute();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
RecipientHandlerBase:: |
protected | property | The database connection. | |
RecipientHandlerBase:: |
protected | property | The newsletter issue. | |
RecipientHandlerBase:: |
protected | property | The newsletter IDs. | |
RecipientHandlerBase:: |
protected | function | Adds an array of entries to the spool. | |
RecipientHandlerBase:: |
protected | function | Checks if the recipient count can be cached. | 2 |
RecipientHandlerBase:: |
public | function | ||
RecipientHandlerBase:: |
abstract protected | function | Counts the number of recipients. | 3 |
RecipientHandlerBase:: |
protected | function | Returns the newsletter ID. | |
RecipientHandlerBase:: |
public | function |
Returns the elements to add to the settings form for handler settings. Overrides RecipientHandlerInterface:: |
1 |
RecipientHandlerBase:: |
public | function |
RecipientHandlerBase constructor. Overrides PluginBase:: |
|
RecipientHandlerInterface:: |
public | function | Adds a newsletter issue to the mail spool. | 3 |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |