Message.php 2.68 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains Drupal\contact\Plugin\Core\Entity\Message.
 */

namespace Drupal\contact\Plugin\Core\Entity;

10
use Drupal\Core\Entity\Annotation\EntityType;
11 12 13 14 15 16
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\Entity;

/**
 * Defines the contact message entity.
 *
17
 * @EntityType(
18 19 20
 *   id = "contact_message",
 *   label = @Translation("Contact message"),
 *   module = "contact",
21 22 23 24 25 26
 *   controllers = {
 *     "storage" = "Drupal\Core\Entity\DatabaseStorageController",
 *     "render" = "Drupal\contact\MessageRenderController",
 *     "form" = {
 *       "default" = "Drupal\contact\MessageFormController"
 *     }
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 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
 *   },
 *   entity_keys = {
 *     "bundle" = "category"
 *   },
 *   fieldable = TRUE,
 *   bundle_keys = {
 *     "bundle" = "id"
 *   }
 * )
 */
class Message extends Entity {

  /**
   * The contact category ID of this message.
   *
   * @var string
   */
  public $category;

  /**
   * The sender's name.
   *
   * @var string
   */
  public $name;

  /**
   * The sender's e-mail address.
   *
   * @var string
   */
  public $mail;

  /**
   * The user account object of the message recipient.
   *
   * Only applies to the user contact form. For a site contact form category,
   * multiple recipients can be configured. The existence of a $recipient
   * triggers user contact form specific processing in the contact message form
   * controller.
   *
   * @see Drupal\contact\MessageFormController::form()
   * @see Drupal\contact\MessageFormController::save()
   *
   * @todo Convert user contact form into a locked contact category, and replace
   *   Category::$recipients with the user account's e-mail address upon
   *   Entity::create().
   *
   * @var Drupal\user\Plugin\Core\Entity\User
   */
  public $recipient;

  /**
   * The message subject.
   *
   * @var string
   */
  public $subject;

  /**
   * The message text.
   *
   * @var string
   */
  public $message;

  /**
   * Whether to send a copy of the message to the sender.
   *
   * @var bool
   */
  public $copy;

  /**
   * Overrides Drupal\Core\Entity\Entity::id().
   */
  public function id() {
    return NULL;
  }

  /**
   * Overrides Drupal\Core\Entity\Entity::bundle().
   */
  public function bundle() {
    return $this->category;
  }

  /**
   * Overrides Drupal\Core\Entity\Entity::entityInfo().
   */
  public function entityInfo() {
    // The user contact form is not a category/bundle currently, so it is not
    // fieldable. Prevent EntityFormController from calling into Field Attach
    // functions, since those will throw errors without a bundle name.
    $info = entity_get_info($this->entityType);
    if (isset($this->recipient)) {
      $info['fieldable'] = FALSE;
    }
    return $info;
  }

}