annotate core/modules/contact/src/Entity/Message.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\contact\Entity;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\ContentEntityBase;
Chris@0 6 use Drupal\contact\MessageInterface;
Chris@0 7 use Drupal\Core\Entity\EntityTypeInterface;
Chris@0 8 use Drupal\Core\Field\BaseFieldDefinition;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Defines the contact message entity.
Chris@0 12 *
Chris@0 13 * @ContentEntityType(
Chris@0 14 * id = "contact_message",
Chris@0 15 * label = @Translation("Contact message"),
Chris@17 16 * label_collection = @Translation("Contact messages"),
Chris@17 17 * label_singular = @Translation("contact message"),
Chris@17 18 * label_plural = @Translation("contact messages"),
Chris@17 19 * label_count = @PluralTranslation(
Chris@17 20 * singular = "@count contact message",
Chris@17 21 * plural = "@count contact messages",
Chris@17 22 * ),
Chris@0 23 * bundle_label = @Translation("Contact form"),
Chris@0 24 * handlers = {
Chris@0 25 * "access" = "Drupal\contact\ContactMessageAccessControlHandler",
Chris@0 26 * "storage" = "Drupal\Core\Entity\ContentEntityNullStorage",
Chris@0 27 * "view_builder" = "Drupal\contact\MessageViewBuilder",
Chris@0 28 * "form" = {
Chris@0 29 * "default" = "Drupal\contact\MessageForm"
Chris@0 30 * }
Chris@0 31 * },
Chris@0 32 * admin_permission = "administer contact forms",
Chris@0 33 * entity_keys = {
Chris@0 34 * "bundle" = "contact_form",
Chris@0 35 * "uuid" = "uuid",
Chris@0 36 * "langcode" = "langcode"
Chris@0 37 * },
Chris@0 38 * bundle_entity_type = "contact_form",
Chris@0 39 * field_ui_base_route = "entity.contact_form.edit_form",
Chris@0 40 * )
Chris@0 41 */
Chris@0 42 class Message extends ContentEntityBase implements MessageInterface {
Chris@0 43
Chris@0 44 /**
Chris@0 45 * {@inheritdoc}
Chris@0 46 */
Chris@0 47 public function isPersonal() {
Chris@0 48 return $this->bundle() == 'personal';
Chris@0 49 }
Chris@0 50
Chris@0 51 /**
Chris@0 52 * {@inheritdoc}
Chris@0 53 */
Chris@0 54 public function getContactForm() {
Chris@0 55 return $this->get('contact_form')->entity;
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * {@inheritdoc}
Chris@0 60 */
Chris@0 61 public function getSenderName() {
Chris@0 62 return $this->get('name')->value;
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * {@inheritdoc}
Chris@0 67 */
Chris@0 68 public function setSenderName($sender_name) {
Chris@0 69 $this->set('name', $sender_name);
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * {@inheritdoc}
Chris@0 74 */
Chris@0 75 public function getSenderMail() {
Chris@0 76 return $this->get('mail')->value;
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * {@inheritdoc}
Chris@0 81 */
Chris@0 82 public function setSenderMail($sender_mail) {
Chris@0 83 $this->set('mail', $sender_mail);
Chris@0 84 }
Chris@0 85
Chris@0 86 /**
Chris@0 87 * {@inheritdoc}
Chris@0 88 */
Chris@0 89 public function getSubject() {
Chris@0 90 return $this->get('subject')->value;
Chris@0 91 }
Chris@0 92
Chris@0 93 /**
Chris@0 94 * {@inheritdoc}
Chris@0 95 */
Chris@0 96 public function setSubject($subject) {
Chris@0 97 $this->set('subject', $subject);
Chris@0 98 }
Chris@0 99
Chris@0 100 /**
Chris@0 101 * {@inheritdoc}
Chris@0 102 */
Chris@0 103 public function getMessage() {
Chris@0 104 return $this->get('message')->value;
Chris@0 105 }
Chris@0 106
Chris@0 107 /**
Chris@0 108 * {@inheritdoc}
Chris@0 109 */
Chris@0 110 public function setMessage($message) {
Chris@0 111 $this->set('message', $message);
Chris@0 112 }
Chris@0 113
Chris@0 114 /**
Chris@0 115 * {@inheritdoc}
Chris@0 116 */
Chris@0 117 public function copySender() {
Chris@0 118 return (bool) $this->get('copy')->value;
Chris@0 119 }
Chris@0 120
Chris@0 121 /**
Chris@0 122 * {@inheritdoc}
Chris@0 123 */
Chris@0 124 public function setCopySender($inform) {
Chris@0 125 $this->set('copy', (bool) $inform);
Chris@0 126 }
Chris@0 127
Chris@0 128 /**
Chris@0 129 * {@inheritdoc}
Chris@0 130 */
Chris@0 131 public function getPersonalRecipient() {
Chris@0 132 if ($this->isPersonal()) {
Chris@0 133 return $this->get('recipient')->entity;
Chris@0 134 }
Chris@0 135 }
Chris@0 136
Chris@0 137 /**
Chris@0 138 * {@inheritdoc}
Chris@0 139 */
Chris@0 140 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
Chris@0 141 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
Chris@0 142 $fields = parent::baseFieldDefinitions($entity_type);
Chris@0 143
Chris@0 144 $fields['contact_form']->setLabel(t('Form ID'))
Chris@0 145 ->setDescription(t('The ID of the associated form.'));
Chris@0 146
Chris@0 147 $fields['uuid']->setDescription(t('The message UUID.'));
Chris@0 148
Chris@0 149 $fields['langcode']->setDescription(t('The message language code.'));
Chris@0 150
Chris@0 151 $fields['name'] = BaseFieldDefinition::create('string')
Chris@0 152 ->setLabel(t("The sender's name"))
Chris@0 153 ->setDescription(t('The name of the person that is sending the contact message.'));
Chris@0 154
Chris@0 155 $fields['mail'] = BaseFieldDefinition::create('email')
Chris@0 156 ->setLabel(t("The sender's email"))
Chris@0 157 ->setDescription(t('The email of the person that is sending the contact message.'));
Chris@0 158
Chris@0 159 // The subject of the contact message.
Chris@0 160 $fields['subject'] = BaseFieldDefinition::create('string')
Chris@0 161 ->setLabel(t('Subject'))
Chris@0 162 ->setRequired(TRUE)
Chris@0 163 ->setSetting('max_length', 100)
Chris@0 164 ->setDisplayOptions('form', [
Chris@0 165 'type' => 'string_textfield',
Chris@0 166 'weight' => -10,
Chris@0 167 ])
Chris@0 168 ->setDisplayConfigurable('form', TRUE);
Chris@0 169
Chris@0 170 // The text of the contact message.
Chris@0 171 $fields['message'] = BaseFieldDefinition::create('string_long')
Chris@0 172 ->setLabel(t('Message'))
Chris@0 173 ->setRequired(TRUE)
Chris@0 174 ->setDisplayOptions('form', [
Chris@0 175 'type' => 'string_textarea',
Chris@0 176 'weight' => 0,
Chris@0 177 'settings' => [
Chris@0 178 'rows' => 12,
Chris@0 179 ],
Chris@0 180 ])
Chris@0 181 ->setDisplayConfigurable('form', TRUE)
Chris@0 182 ->setDisplayOptions('view', [
Chris@0 183 'type' => 'string',
Chris@0 184 'weight' => 0,
Chris@0 185 'label' => 'above',
Chris@0 186 ])
Chris@0 187 ->setDisplayConfigurable('view', TRUE);
Chris@0 188
Chris@0 189 $fields['copy'] = BaseFieldDefinition::create('boolean')
Chris@0 190 ->setLabel(t('Copy'))
Chris@0 191 ->setDescription(t('Whether to send a copy of the message to the sender.'));
Chris@0 192
Chris@0 193 $fields['recipient'] = BaseFieldDefinition::create('entity_reference')
Chris@0 194 ->setLabel(t('Recipient ID'))
Chris@0 195 ->setDescription(t('The ID of the recipient user for personal contact messages.'))
Chris@0 196 ->setSetting('target_type', 'user');
Chris@0 197
Chris@0 198 return $fields;
Chris@0 199 }
Chris@0 200
Chris@0 201 }