Newer
Older
Dries Buytaert
committed
<?php
namespace Drupal\contact\Entity;
Dries Buytaert
committed
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\contact\MessageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
Alex Pott
committed
use Drupal\Core\Field\BaseFieldDefinition;
Dries Buytaert
committed
/**
* Defines the contact message entity.
*
Alex Pott
committed
* @ContentEntityType(
Dries Buytaert
committed
* id = "contact_message",
* label = @Translation("Contact message"),
* bundle_label = @Translation("Contact form"),
Alex Pott
committed
* handlers = {
Alex Pott
committed
* "access" = "Drupal\contact\ContactMessageAccessControlHandler",
catch
committed
* "storage" = "Drupal\Core\Entity\ContentEntityNullStorage",
Alex Pott
committed
* "view_builder" = "Drupal\contact\MessageViewBuilder",
Alex Pott
committed
* "default" = "Drupal\contact\MessageForm"
Dries Buytaert
committed
* },
Alex Pott
committed
* admin_permission = "administer contact forms",
Dries Buytaert
committed
* entity_keys = {
Alex Pott
committed
* "bundle" = "contact_form",
* "uuid" = "uuid",
* "langcode" = "langcode"
Dries Buytaert
committed
* },
Alex Pott
committed
* bundle_entity_type = "contact_form",
Alex Pott
committed
* field_ui_base_route = "entity.contact_form.edit_form",
Dries Buytaert
committed
* )
*/
class Message extends ContentEntityBase implements MessageInterface {
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function isPersonal() {
return $this->bundle() == 'personal';
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
Alex Pott
committed
public function getContactForm() {
return $this->get('contact_form')->entity;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getSenderName() {
return $this->get('name')->value;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function setSenderName($sender_name) {
$this->set('name', $sender_name);
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getSenderMail() {
return $this->get('mail')->value;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function setSenderMail($sender_mail) {
$this->set('mail', $sender_mail);
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getSubject() {
return $this->get('subject')->value;
Dries Buytaert
committed
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function setSubject($subject) {
$this->set('subject', $subject);
Dries Buytaert
committed
}
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getMessage() {
return $this->get('message')->value;
}
/**
* {@inheritdoc}
*/
public function setMessage($message) {
$this->set('message', $message);
}
/**
* {@inheritdoc}
*/
public function copySender() {
Jess
committed
return (bool) $this->get('copy')->value;
}
/**
* {@inheritdoc}
*/
public function setCopySender($inform) {
$this->set('copy', (bool) $inform);
}
/**
* {@inheritdoc}
*/
public function getPersonalRecipient() {
if ($this->isPersonal()) {
return $this->get('recipient')->entity;
}
Dries Buytaert
committed
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
$fields = parent::baseFieldDefinitions($entity_type);
$fields['contact_form']->setLabel(t('Form ID'))
->setDescription(t('The ID of the associated form.'));
$fields['uuid']->setDescription(t('The message UUID.'));
$fields['langcode']->setDescription(t('The message language code.'));
Alex Pott
committed
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t("The sender's name"))
->setDescription(t('The name of the person that is sending the contact message.'));
Alex Pott
committed
$fields['mail'] = BaseFieldDefinition::create('email')
->setLabel(t("The sender's email"))
->setDescription(t('The email of the person that is sending the contact message.'));
Alex Pott
committed
// The subject of the contact message.
Alex Pott
committed
$fields['subject'] = BaseFieldDefinition::create('string')
Alex Pott
committed
->setLabel(t('Subject'))
->setRequired(TRUE)
->setSetting('max_length', 100)
->setDisplayOptions('form', [
'type' => 'string_textfield',
Alex Pott
committed
'weight' => -10,
])
Alex Pott
committed
->setDisplayConfigurable('form', TRUE);
// The text of the contact message.
Alex Pott
committed
$fields['message'] = BaseFieldDefinition::create('string_long')
Alex Pott
committed
->setLabel(t('Message'))
->setRequired(TRUE)
->setDisplayOptions('form', [
Alex Pott
committed
'type' => 'string_textarea',
'weight' => 0,
'settings' => [
Alex Pott
committed
'rows' => 12,
],
])
Alex Pott
committed
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
Alex Pott
committed
'type' => 'string',
'weight' => 0,
'label' => 'above',
])
Alex Pott
committed
->setDisplayConfigurable('view', TRUE);
Alex Pott
committed
$fields['copy'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Copy'))
->setDescription(t('Whether to send a copy of the message to the sender.'));
Alex Pott
committed
$fields['recipient'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Recipient ID'))
->setDescription(t('The ID of the recipient user for personal contact messages.'))
Alex Pott
committed
->setSetting('target_type', 'user');
Alex Pott
committed
return $fields;
}