View source
<?php
namespace Drupal\easy_email\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\RevisionableContentEntityBase;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
class EasyEmail extends RevisionableContentEntityBase implements EasyEmailInterface {
use EntityChangedTrait;
protected $evaluatedAttachments;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$easy_email_type = $storage_controller
->getEmailTypeStorage()
->load($values['type']);
$values += [
'creator_uid' => \Drupal::currentUser()
->id(),
'key' => $easy_email_type
->getKey(),
'from_name' => $easy_email_type
->getFromName(),
'from_address' => $easy_email_type
->getFromAddress(),
'reply_to' => $easy_email_type
->getReplyToAddress(),
'subject' => $easy_email_type
->getSubject(),
'body_html' => $easy_email_type
->getBodyHtml(),
'body_plain' => $easy_email_type
->getBodyPlain(),
'inbox_preview' => $easy_email_type
->getInboxPreview(),
'recipient_address' => $easy_email_type
->getRecipient(),
'cc_address' => $easy_email_type
->getCc(),
'bcc_address' => $easy_email_type
->getBcc(),
'attachment_path' => $easy_email_type
->getAttachment(),
];
}
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel);
if ($rel === 'revision_revert' && $this instanceof RevisionableInterface) {
$uri_route_parameters[$this
->getEntityTypeId() . '_revision'] = $this
->getRevisionId();
}
elseif ($rel === 'revision_delete' && $this instanceof RevisionableInterface) {
$uri_route_parameters[$this
->getEntityTypeId() . '_revision'] = $this
->getRevisionId();
}
return $uri_route_parameters;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
foreach (array_keys($this
->getTranslationLanguages()) as $langcode) {
$translation = $this
->getTranslation($langcode);
if (!$translation
->getCreator()) {
$translation
->setCreatorId(0);
}
}
if (!$this
->getRevisionUser()) {
$this
->setRevisionUserId($this
->getCreatorId());
}
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getCreator() {
return $this
->get('creator_uid')->entity;
}
public function getCreatorId() {
return $this
->get('creator_uid')->target_id;
}
public function setCreatorId($uid) {
$this
->set('creator_uid', $uid);
return $this;
}
public function setCreator(UserInterface $account) {
$this
->set('creator_uid', $account
->id());
return $this;
}
public function getKey() {
return $this
->get('key')->value;
}
public function setKey($key) {
$this
->set('key', $key);
return $this;
}
public function getRecipients() {
if ($this
->hasField('recipient_uid')) {
return $this
->get('recipient_uid')
->referencedEntities();
}
return NULL;
}
public function setRecipients($accounts) {
if ($this
->hasField('recipient_uid')) {
$this
->set('recipient_uid', $accounts);
}
return $this;
}
public function getRecipientIds() {
if ($this
->hasField('recipient_uid')) {
return $this
->getEntityReferenceIds($this
->get('recipient_uid'));
}
return [];
}
public function setRecipientIds($uids) {
if ($this
->hasField('recipient_uid')) {
$this
->set('recipient_uid', $uids);
}
return $this;
}
public function addRecipient($uid) {
if ($this
->hasField('recipient_uid')) {
return $this
->addEntityReferenceById($uid, 'recipient_uid');
}
return $this;
}
public function removeRecipient($uid) {
if ($this
->hasField('recipient_uid')) {
return $this
->removeEntityReferenceById($uid, 'recipient_uid');
}
return $this;
}
public function getRecipientAddresses() {
return $this
->getListTextValues($this
->get('recipient_address'));
}
public function setRecipientAddresses($addresses) {
$this
->set('recipient_address', $addresses);
return $this;
}
public function addRecipientAddress($address) {
return $this
->addTextValueToList($address, 'recipient_address');
}
public function removeRecipientAddress($address) {
return $this
->removeTextValueFromList($address, 'recipient_address');
}
public function getCC() {
if ($this
->hasField('cc_uid')) {
return $this
->get('cc_uid')
->referencedEntities();
}
return NULL;
}
public function setCC($accounts) {
if ($this
->hasField('cc_uid')) {
$this
->set('cc_uid', $accounts);
}
return $this;
}
public function getCCIds() {
if ($this
->hasField('cc_uid')) {
return $this
->getEntityReferenceIds($this
->get('cc_uid'));
}
return [];
}
public function setCCIds($uids) {
if ($this
->hasField('cc_uid')) {
$this
->set('cc_uid', $uids);
}
return $this;
}
public function addCC($uid) {
if ($this
->hasField('cc_uid')) {
return $this
->addEntityReferenceById($uid, 'cc_uid');
}
return $this;
}
public function removeCC($uid) {
if ($this
->hasField('cc_uid')) {
return $this
->removeEntityReferenceById($uid, 'cc_uid');
}
return $this;
}
public function getCCAddresses() {
if ($this
->hasField('cc_address')) {
return $this
->getListTextValues($this
->get('cc_address'));
}
return NULL;
}
public function setCCAddresses($addresses) {
if ($this
->hasField('cc_address')) {
$this
->set('cc_address', $addresses);
}
return $this;
}
public function addCCAddress($address) {
if ($this
->hasField('cc_address')) {
return $this
->addTextValueToList($address, 'cc_address');
}
return $this;
}
public function removeCCAddress($address) {
if ($this
->hasField('cc_address')) {
return $this
->removeTextValueFromList($address, 'cc_address');
}
return $this;
}
public function getBCC() {
if ($this
->hasField('bcc_uid')) {
return $this
->get('bcc_uid')
->referencedEntities();
}
return NULL;
}
public function setBCC($accounts) {
if ($this
->hasField('bcc_uid')) {
$this
->set('bcc_uid', $accounts);
}
return $this;
}
public function getBCCIds() {
if ($this
->hasField('bcc_uid')) {
return $this
->getEntityReferenceIds($this
->get('bcc_uid'));
}
return [];
}
public function setBCCIds($uids) {
if ($this
->hasField('bcc_uid')) {
$this
->set('bcc_uid', $uids);
}
return $this;
}
public function addBCC($uid) {
if ($this
->hasField('bcc_uid')) {
return $this
->addEntityReferenceById($uid, 'bcc_uid');
}
return $this;
}
public function removeBCC($uid) {
if ($this
->hasField('bcc_uid')) {
return $this
->removeEntityReferenceById($uid, 'bcc_uid');
}
return $this;
}
public function getBCCAddresses() {
if ($this
->hasField('bcc_address')) {
return $this
->getListTextValues($this
->get('bcc_address'));
}
return NULL;
}
public function setBCCAddresses($addresses) {
if ($this
->hasField('bcc_address')) {
$this
->set('bcc_address', $addresses);
}
return $this;
}
public function addBCCAddress($address) {
if ($this
->hasField('bcc_address')) {
return $this
->addTextValueToList($address, 'bcc_address');
}
return $this;
}
public function removeBCCAddress($address) {
if ($this
->hasField('bcc_address')) {
return $this
->removeTextValueFromList($address, 'bcc_address');
}
return $this;
}
public function getFromName() {
if ($this
->hasField('from_name')) {
return $this
->get('from_name')->value;
}
return NULL;
}
public function setFromName($from_name) {
if ($this
->hasField('from_name')) {
$this
->set('from_name', $from_name);
}
return $this;
}
public function getFromAddress() {
if ($this
->hasField('from_address')) {
return $this
->get('from_address')->value;
}
return NULL;
}
public function setFromAddress($from_email) {
if ($this
->hasField('from_address')) {
$this
->set('from_address', $from_email);
}
return $this;
}
public function getReplyToAddress() {
if ($this
->hasField('reply_to')) {
return $this
->get('reply_to')->value;
}
return NULL;
}
public function setReplyToAddress($reply_to_email) {
if ($this
->hasField('reply_to')) {
$this
->set('reply_to', $reply_to_email);
}
return $this;
}
public function getSubject() {
return $this
->get('subject')->value;
}
public function setSubject($subject) {
$this
->set('subject', $subject);
return $this;
}
public function getInboxPreview() {
if ($this
->hasField('inbox_preview')) {
return $this
->get('inbox_preview')->value;
}
return NULL;
}
public function setInboxPreview($text) {
if ($this
->hasField('inbox_preview')) {
$this
->set('inbox_preview', $text);
}
return $this;
}
public function getHtmlBody() {
if ($this
->hasField('body_html')) {
return [
'value' => $this
->get('body_html')->value,
'format' => $this
->get('body_html')->format,
];
}
return NULL;
}
public function setHtmlBody($text, $format) {
if ($this
->hasField('body_html')) {
$this
->set('body_html', [
'value' => $text,
'format' => $format,
]);
}
return $this;
}
public function getPlainBody() {
if ($this
->hasField('body_plain')) {
return $this
->get('body_plain')->value;
}
return NULL;
}
public function setPlainBody($text) {
if ($this
->hasField('body_plain')) {
$this
->set('body_plain', $text);
}
return $this;
}
public function getAttachments() {
if ($this
->hasField('attachment')) {
return $this
->get('attachment')
->referencedEntities();
}
return [];
}
public function setAttachments($files) {
if ($this
->hasField('attachment')) {
$this
->set('attachment', $files);
}
return $this;
}
public function getAttachmentIds() {
if ($this
->hasField('attachment')) {
return $this
->getEntityReferenceIds($this
->get('attachment'));
}
return [];
}
public function setAttachmentIds($fids) {
if ($this
->hasField('attachment')) {
$this
->set('attachment', $fids);
}
return $this;
}
public function addAttachment($fid) {
if ($this
->hasField('attachment')) {
return $this
->addEntityReferenceById($fid, 'attachment');
}
return $this;
}
public function removeAttachment($fid) {
if ($this
->hasField('attachment')) {
return $this
->removeEntityReferenceById($fid, 'attachment');
}
return $this;
}
public function getAttachmentPaths() {
if ($this
->hasField('attachment_path')) {
return $this
->getListTextValues($this
->get('attachment_path'));
}
return NULL;
}
public function setAttachmentPaths($paths) {
if ($this
->hasField('attachment_path')) {
$this
->set('attachment_path', $paths);
}
return $this;
}
public function addAttachmentPath($path) {
if ($this
->hasField('attachment_path')) {
return $this
->addTextValueToList($path, 'attachment_path');
}
return NULL;
}
public function removeAttachmentPath($path) {
if ($this
->hasField('attachment_path')) {
return $this
->removeTextValueFromList($path, 'attachment_path');
}
return $this;
}
public function getEvaluatedAttachments() {
if (is_null($this->evaluatedAttachments)) {
$this->evaluatedAttachments = [];
}
return $this->evaluatedAttachments;
}
public function setEvaluatedAttachments($attachments) {
$this->evaluatedAttachments = $attachments;
return $this;
}
public function addEvaluatedAttachment($attachment) {
$existing_attachments = $this
->getEvaluatedAttachments();
foreach ($existing_attachments as $existing_attachment) {
if ($existing_attachment->uri === $attachment->uri) {
return $this;
}
}
$this->evaluatedAttachments[] = $attachment;
return $this;
}
public function removeEvaluatedAttachment($attachment) {
$existing_attachments = $this
->getEvaluatedAttachments();
foreach ($existing_attachments as $i => $existing_attachment) {
if ($existing_attachment->uri === $attachment->uri) {
unset($existing_attachments[$i]);
}
}
$this->evaluatedAttachments = array_values($existing_attachments);
return $this;
}
public function isSent() {
return (bool) $this
->get('sent')->value;
}
public function getSentTime() {
return $this
->get('sent')->value;
}
public function setSentTime($timestamp) {
$this
->set('sent', $timestamp);
return $this;
}
protected function getEntityReferenceIds($field_item_list) {
$ids = [];
if (!$field_item_list
->isEmpty()) {
foreach ($field_item_list as $delta => $item) {
if ($item->target_id !== NULL) {
$ids[$delta] = $item->target_id;
}
}
}
return $ids;
}
protected function addEntityReferenceById($id, $field_name) {
$ids = $this
->getEntityReferenceIds($this
->get($field_name));
if (!is_null($ids) && !in_array($id, $ids)) {
$ids[] = $id;
$this
->set($field_name, $ids);
}
return $this;
}
protected function removeEntityReferenceById($id, $field_name) {
$ids = $this
->getEntityReferenceIds($this
->get($field_name));
if (!is_null($ids)) {
foreach ($ids as $delta => $value) {
if ($id === $value) {
unset($ids[$delta]);
}
}
$this
->set($field_name, $ids);
}
return $this;
}
protected function getListTextValues($field_item_list) {
if (!$field_item_list
->isEmpty()) {
$values = [];
foreach ($field_item_list as $delta => $item) {
if ($item->value !== NULL) {
$values[$delta] = $item->value;
}
}
return $values;
}
return NULL;
}
protected function addTextValueToList($text, $field_name) {
$strings = $this
->getListTextValues($this
->get($field_name));
if (!is_null($strings) && !in_array($text, $strings)) {
$strings[] = $text;
$this
->set($field_name, $strings);
}
return $this;
}
protected function removeTextValueFromList($text, $field_name) {
$strings = $this
->getListTextValues($this
->get($field_name));
if (!is_null($strings)) {
foreach ($strings as $delta => $value) {
if ($text === $value) {
unset($strings[$delta]);
}
}
$this
->set($field_name, $strings);
}
return $this;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['creator_uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Creator'))
->setDescription(t('The user ID of creator of the Email entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'autocomplete_type' => 'tags',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['recipient_address'] = BaseFieldDefinition::create('string')
->setLabel(t('Recipient Addresses'))
->setDescription(t('The recipient email addresses of the Email entity.'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setRevisionable(TRUE)
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['subject'] = BaseFieldDefinition::create('string')
->setLabel(t('Subject'))
->setDescription(t('The Subject of the Email entity.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
$fields['sent'] = BaseFieldDefinition::create('timestamp')
->setLabel(t('Sent'))
->setDescription(t('The time that the entity was sent.'));
$fields['revision_translation_affected'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Revision translation affected'))
->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
->setReadOnly(TRUE)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
return $fields;
}
}