TestMessage.php in Courier 2.x
File
tests/modules/courier_test_message/src/Entity/TestMessage.php
View source
<?php
namespace Drupal\courier_test_message\Entity;
use Drupal\courier\ChannelBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
class TestMessage extends ChannelBase implements TestMessageInterface {
public function getUid() {
return $this
->get('uid')->value;
}
public function setUid($uid) {
$this
->set('uid', [
'value' => $uid,
]);
return $this;
}
public function getMessage() {
return $this
->get('message')->value;
}
public function setMessage($message) {
$this
->get('message')->value = $message;
return $this;
}
public function applyTokens() {
$tokens = $this
->getTokenValues();
$options = $this
->getTokenOptions();
$this
->setMessage(\Drupal::token()
->replace($this
->getMessage(), $tokens, $options));
return $this;
}
public function isEmpty() {
return empty($this
->getMessage());
}
public static function sendMessages(array $messages, array $options = []) {
$state = \Drupal::state()
->get('courier_test_message.messages', []);
foreach ($messages as $message) {
$state[] = $message;
}
\Drupal::state()
->set('courier_test_message.messages', $state);
}
public function sendMessage(array $options = []) {
$this
->sendMessages([
$this,
], $options);
}
public function importTemplate($content) {
$this
->setMessage($content['message']);
}
public function exportTemplate() {
return [
'message' => $this
->getMessage(),
];
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Email ID'))
->setDescription(t('The email ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User ID'))
->setDescription(t('The user ID.'))
->setSetting('target_type', 'user');
$fields['message'] = BaseFieldDefinition::create('string')
->setLabel(t('Message'))
->setDescription(t('The message contents.'))
->setDisplayOptions('form', [
'type' => 'text_textfield',
]);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The message language code.'));
return $fields;
}
}