View source
<?php
namespace Drupal\mass_contact\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\user\UserInterface;
class MassContactMessage extends ContentEntityBase implements MassContactMessageInterface {
public function getOwner() {
return $this
->get('uid')->entity;
}
public function setOwner(UserInterface $account) {
$this
->set('uid', $account
->id());
return $this;
}
public function getOwnerId() {
return $this
->getEntityKey('uid');
}
public function setOwnerId($uid) {
$this
->set('uid', $uid);
return $this;
}
public function getBody() {
return $this
->get('body')->value;
}
public function getCategories() {
$categories = [];
foreach ($this
->get('categories') as $data) {
$categories[$data->target_id] = $data->entity;
}
return $categories;
}
public function getFormat() {
return $this
->get('body')->format;
}
public function getSentTime() {
return $this
->get('sent')->value;
}
public function getSubject() {
return $this
->get('subject')->value;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['categories'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Categories'))
->setSetting('target_type', 'mass_contact_category')
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setRequired(TRUE)
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'entity_reference_label',
'weight' => -6,
]);
$fields['subject'] = BaseFieldDefinition::create('string')
->setLabel(t('Subject'))
->setRequired(TRUE)
->setTranslatable(FALSE)
->setRevisionable(FALSE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -15,
]);
$fields['body'] = BaseFieldDefinition::create('text_long')
->setLabel('body')
->setRequired(TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'text_default',
'weight' => 5,
]);
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Sent by'))
->setDescription(t('The that sent the mass contact message.'))
->setSetting('target_type', 'user')
->setDefaultValueCallback('Drupal\\mass_contact\\Entity\\MassContactMessage::getCurrentUserId')
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'author',
'weight' => -7,
]);
$fields['sent'] = BaseFieldDefinition::create('created')
->setLabel(t('Sent on'))
->setDescription(t('The time that the message was sent.'))
->setDefaultValueCallback('Drupal\\mass_contact\\Entity\\MassContactMessage::getDefaultSentTime')
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'timestamp',
'weight' => -8,
]);
return $fields;
}
public static function getCurrentUserId() {
return [
\Drupal::currentUser()
->id(),
];
}
public static function getDefaultSentTime() {
return [
\Drupal::requestStack()
->getCurrentRequest()->server
->get('REQUEST_TIME'),
];
}
}