TemplateCollection.php in Courier 2.x
File
src/Entity/TemplateCollection.php
View source
<?php
namespace Drupal\courier\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\courier\TemplateCollectionInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\courier\CourierContextInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\courier\ChannelInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\courier\TokenTrait;
class TemplateCollection extends ContentEntityBase implements TemplateCollectionInterface {
use TokenTrait;
public function getContext() {
return $this
->get('context')->entity;
}
public function setContext(CourierContextInterface $entity) {
$this
->set('context', [
'entity' => $entity,
]);
return $this;
}
public function getOwner() {
return $this
->get('owner')->entity;
}
public function setOwner(EntityInterface $entity) {
$this
->set('owner', [
'entity' => $entity,
]);
return $this;
}
public function getTemplate($channel_type_id) {
foreach ($this
->getTemplates() as $template) {
if ($template
->getEntityTypeId() == $channel_type_id) {
return $template;
}
}
return NULL;
}
public function getTemplates() {
return $this->templates
->referencedEntities();
}
public function setTemplate(ChannelInterface $template) {
$this
->removeTemplate($template
->getEntityTypeId());
$this->templates[] = $template;
return $this;
}
public function removeTemplate($channel_type_id) {
foreach ($this
->getTemplates() as $key => $template) {
if ($channel_type_id == $template
->getEntityTypeId()) {
$this->templates
->removeItem($key);
}
}
return $this;
}
public function validateTokenValues() {
if ($this
->getContext()) {
$required_tokens = $this
->getContext()
->getTokens();
$existing_tokens = array_keys($this
->getTokenValues());
$key = array_search('identity', $required_tokens);
unset($required_tokens[$key]);
$missing_tokens = array_diff_key($required_tokens, $existing_tokens);
if ($missing_tokens) {
throw new \Exception(sprintf('Missing tokens required by courier context: %s', implode(', ', $missing_tokens)));
}
}
}
public static function getTemplateCollectionForTemplate(ChannelInterface $template) {
$ids = \Drupal::entityTypeManager()
->getStorage('courier_template_collection')
->getQuery()
->condition('templates.target_type', $template
->getEntityTypeId(), '=')
->condition('templates.target_id', $template
->id(), '=')
->execute();
return $ids ? static::load(reset($ids)) : NULL;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Template collection ID'))
->setDescription(t('The template collection ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['context'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Context for templates.'))
->setSetting('target_type', 'courier_context')
->setCardinality(1)
->setReadOnly(TRUE);
$fields['owner'] = BaseFieldDefinition::create('dynamic_entity_reference')
->setLabel(t('Owner'))
->setDescription(t('The owner entity.'))
->setCardinality(1)
->setReadOnly(TRUE);
$fields['templates'] = BaseFieldDefinition::create('dynamic_entity_reference')
->setLabel(t('Templates'))
->setDescription(t('Templates for this this collection.'))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setReadOnly(TRUE);
return $fields;
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
foreach ($entities as $template_collection) {
foreach ($template_collection
->getTemplates() as $template) {
$template
->delete();
}
}
parent::preDelete($storage, $entities);
}
}