Message.php 4.78 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains Drupal\contact\Entity\Message.
6 7
 */

8
namespace Drupal\contact\Entity;
9

10
use Drupal\Core\Entity\ContentEntityBase;
11
use Drupal\contact\MessageInterface;
12
use Drupal\Core\Entity\EntityTypeInterface;
13
use Drupal\Core\Field\BaseFieldDefinition;
14 15 16 17

/**
 * Defines the contact message entity.
 *
18
 * @ContentEntityType(
19 20
 *   id = "contact_message",
 *   label = @Translation("Contact message"),
21
 *   controllers = {
22
 *     "storage" = "Drupal\Core\Entity\ContentEntityNullStorage",
23
 *     "view_builder" = "Drupal\contact\MessageViewBuilder",
24
 *     "form" = {
25
 *       "default" = "Drupal\contact\MessageForm"
26
 *     }
27 28
 *   },
 *   entity_keys = {
29
 *     "bundle" = "contact_form",
30
 *     "uuid" = "uuid"
31
 *   },
32
 *   bundle_entity_type = "contact_form",
33
 *   field_ui_base_route = "entity.contact_form.edit_form",
34 35 36
 *   fieldable = TRUE,
 * )
 */
37
class Message extends ContentEntityBase implements MessageInterface {
38 39

  /**
40
   * {@inheritdoc}
41
   */
42 43 44
  public function isPersonal() {
    return $this->bundle() == 'personal';
  }
45 46

  /**
47
   * {@inheritdoc}
48
   */
49 50
  public function getContactForm() {
    return $this->get('contact_form')->entity;
51
  }
52 53

  /**
54
   * {@inheritdoc}
55
   */
56 57 58
  public function getSenderName() {
    return $this->get('name')->value;
  }
59 60

  /**
61
   * {@inheritdoc}
62
   */
63 64 65
  public function setSenderName($sender_name) {
    $this->set('name', $sender_name);
  }
66 67

  /**
68
   * {@inheritdoc}
69
   */
70 71 72
  public function getSenderMail() {
    return $this->get('mail')->value;
  }
73 74

  /**
75
   * {@inheritdoc}
76
   */
77 78 79
  public function setSenderMail($sender_mail) {
    $this->set('mail', $sender_mail);
  }
80 81

  /**
82
   * {@inheritdoc}
83
   */
84 85
  public function getSubject() {
    return $this->get('subject')->value;
86 87 88
  }

  /**
89
   * {@inheritdoc}
90
   */
91 92
  public function setSubject($subject) {
    $this->set('subject', $subject);
93 94 95
  }

  /**
96
   * {@inheritdoc}
97
   */
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  public function getMessage() {
    return $this->get('message')->value;
  }

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

  /**
   * {@inheritdoc}
   */
  public function copySender() {
    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;
    }
130 131
  }

132 133 134
  /**
   * {@inheritdoc}
   */
135
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
136 137 138 139
    $fields['contact_form'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Form ID'))
      ->setDescription(t('The ID of the associated form.'))
      ->setSetting('target_type', 'contact_form')
140 141
      ->setRequired(TRUE);

142
    $fields['uuid'] = BaseFieldDefinition::create('uuid')
143 144 145 146
      ->setLabel(t('UUID'))
      ->setDescription(t('The message UUID.'))
      ->setReadOnly(TRUE);

147
    $fields['langcode'] = BaseFieldDefinition::create('language')
148 149 150
      ->setLabel(t('Language code'))
      ->setDescription(t('The comment language code.'));

151
    $fields['name'] = BaseFieldDefinition::create('string')
152 153 154
      ->setLabel(t("The sender's name"))
      ->setDescription(t('The name of the person that is sending the contact message.'));

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

159
    // The subject of the contact message.
160
    $fields['subject'] = BaseFieldDefinition::create('string')
161 162 163 164 165 166 167 168 169 170
      ->setLabel(t('Subject'))
      ->setRequired(TRUE)
      ->setSetting('max_length', 100)
      ->setDisplayOptions('form', array(
        'type' => 'string',
        'weight' => -10,
      ))
      ->setDisplayConfigurable('form', TRUE);

    // The text of the contact message.
171
    $fields['message'] = BaseFieldDefinition::create('string_long')
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
      ->setLabel(t('Message'))
      ->setRequired(TRUE)
      ->setDisplayOptions('form', array(
        'type' => 'string_textarea',
        'weight' => 0,
        'settings' => array(
          'rows' => 12,
        ),
      ))
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayOptions('view', array(
        'type' => 'string',
        'weight' => 0,
        'label' => 'above',
      ))
      ->setDisplayConfigurable('view', TRUE);
188

189
    $fields['copy'] = BaseFieldDefinition::create('boolean')
190 191 192
      ->setLabel(t('Copy'))
      ->setDescription(t('Whether to send a copy of the message to the sender.'));

193
    $fields['recipient'] = BaseFieldDefinition::create('entity_reference')
194 195
      ->setLabel(t('Recipient ID'))
      ->setDescription(t('The ID of the recipient user for personal contact messages.'))
196
      ->setSetting('target_type', 'user');
197

198 199 200
    return $fields;
  }

201
}