Skip to content
Message.php 4.7 KiB
Newer Older
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\contact\MessageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
 *   id = "contact_message",
 *   label = @Translation("Contact message"),
 *   bundle_label = @Translation("Contact form"),
 *     "access" = "Drupal\contact\ContactMessageAccessControlHandler",
 *     "storage" = "Drupal\Core\Entity\ContentEntityNullStorage",
 *     "view_builder" = "Drupal\contact\MessageViewBuilder",
 *   admin_permission = "administer contact forms",
 *     "uuid" = "uuid",
 *     "langcode" = "langcode"
 *   field_ui_base_route = "entity.contact_form.edit_form",
class Message extends ContentEntityBase implements MessageInterface {
  public function isPersonal() {
    return $this->bundle() == 'personal';
  }
  public function getContactForm() {
    return $this->get('contact_form')->entity;
  public function getSenderName() {
    return $this->get('name')->value;
  }
  public function setSenderName($sender_name) {
    $this->set('name', $sender_name);
  }
  public function getSenderMail() {
    return $this->get('mail')->value;
  }
  public function setSenderMail($sender_mail) {
    $this->set('mail', $sender_mail);
  }
  public function getSubject() {
    return $this->get('subject')->value;
  public function setSubject($subject) {
    $this->set('subject', $subject);
  public function getMessage() {
    return $this->get('message')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setMessage($message) {
    $this->set('message', $message);
  }

  /**
   * {@inheritdoc}
   */
  public function copySender() {
  }

  /**
   * {@inheritdoc}
   */
  public function setCopySender($inform) {
    $this->set('copy', (bool) $inform);
  }

  /**
   * {@inheritdoc}
   */
  public function getPersonalRecipient() {
    if ($this->isPersonal()) {
      return $this->get('recipient')->entity;
    }
  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.'));
    $fields['name'] = BaseFieldDefinition::create('string')
      ->setLabel(t("The sender's name"))
      ->setDescription(t('The name of the person that is sending the contact message.'));

    $fields['mail'] = BaseFieldDefinition::create('email')
      ->setLabel(t("The sender's email"))
      ->setDescription(t('The email of the person that is sending the contact message.'));

    $fields['subject'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Subject'))
      ->setRequired(TRUE)
      ->setSetting('max_length', 100)
      ->setDisplayConfigurable('form', TRUE);

    // The text of the contact message.
    $fields['message'] = BaseFieldDefinition::create('string_long')
        'type' => 'string',
        'weight' => 0,
        'label' => 'above',
    $fields['copy'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Copy'))
      ->setDescription(t('Whether to send a copy of the message to the sender.'));

    $fields['recipient'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Recipient ID'))
      ->setDescription(t('The ID of the recipient user for personal contact messages.'))