diff --git a/core/modules/contact/contact.module b/core/modules/contact/contact.module index 3ef1ce6..d1c1526 100644 --- a/core/modules/contact/contact.module +++ b/core/modules/contact/contact.module @@ -272,8 +272,8 @@ function contact_mail($key, &$message, $params) { $variables = array( '!site-name' => config('system.site')->get('name'), - '!subject' => $contact_message->subject, - '!category' => isset($params['contact_category']) ? $params['contact_category']->label() : NULL, + '!subject' => $contact_message->subject->value, + '!category' => !empty($params['contact_category']) ? $params['contact_category']->label() : NULL, '!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)), '!sender-name' => user_format_name($sender), ); @@ -312,7 +312,7 @@ function contact_mail($key, &$message, $params) { $message['body'][] = t("!sender-name (!sender-url) has sent you a message via your contact form at !site-name.", $variables, $options); $message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !recipient-edit-url.", $variables, $options); $message['body'][] = t('Message:', array(), $options); - $message['body'][] = $contact_message->message; + $message['body'][] = $contact_message->message->value; break; } } diff --git a/core/modules/contact/contact.pages.inc b/core/modules/contact/contact.pages.inc index 423cd72..52834fd 100644 --- a/core/modules/contact/contact.pages.inc +++ b/core/modules/contact/contact.pages.inc @@ -45,6 +45,9 @@ function contact_site_page(Category $category = NULL) { } } } + else if ($category->id() == 'personal') { + throw new NotFoundHttpException(); + } $message = entity_create('contact_message', array( 'category' => $category->id(), )); diff --git a/core/modules/contact/lib/Drupal/contact/MessageFormController.php b/core/modules/contact/lib/Drupal/contact/MessageFormController.php index 6f73d96..2a09412 100644 --- a/core/modules/contact/lib/Drupal/contact/MessageFormController.php +++ b/core/modules/contact/lib/Drupal/contact/MessageFormController.php @@ -7,13 +7,13 @@ namespace Drupal\contact; -use Drupal\Core\Entity\EntityFormController; +use Drupal\Core\Entity\EntityFormControllerNG; use Drupal\user\Plugin\Core\Entity\User; /** * Form controller for contact message forms. */ -class MessageFormController extends EntityFormController { +class MessageFormController extends EntityFormControllerNG { /** * Overrides Drupal\Core\Entity\EntityFormController::form(). @@ -61,23 +61,22 @@ public function form(array $form, array &$form_state) { $form['mail']['#markup'] = check_plain($user->mail); } - // The user contact form only has a recipient, not a category. - // @todo Convert user contact form into a locked contact category. - if ($message->recipient instanceof User) { + // Display the recipient for a personal message. + if ($message->category->target_id == 'personal') { $form['recipient'] = array( '#type' => 'item', '#title' => t('To'), - '#value' => $message->recipient, + '#value' => $message->recipient->entity, 'name' => array( '#theme' => 'username', - '#account' => $message->recipient, + '#account' => $message->recipient->entity, ), ); } else { $form['category'] = array( '#type' => 'value', - '#value' => $message->category, + '#value' => $message->category->target_id, ); } @@ -146,31 +145,31 @@ public function save(array $form, array &$form_state) { if (!$user->uid) { // At this point, $sender contains drupal_anonymous_user(), so we need to // take over the submitted form values. - $sender->name = $message->name; - $sender->mail = $message->mail; + $sender->name = $message->name->value; + $sender->mail = $message->mail->value; // Save the anonymous user information to a cookie for reuse. - user_cookie_save(array('name' => $message->name, 'mail' => $message->mail)); + user_cookie_save(array('name' => $message->name->value, 'mail' => $message->mail->value)); // For the e-mail message, clarify that the sender name is not verified; it // could potentially clash with a username on this site. - $sender->name = t('!name (not verified)', array('!name' => $message->name)); + $sender->name = t('!name (not verified)', array('!name' => $message->name->value)); } // Build e-mail parameters. $params['contact_message'] = $message; $params['sender'] = $sender; - if ($message->category) { + if ($message->category->target_id != 'personal') { // Send to the category recipient(s), using the site's default language. - $category = entity_load('contact_category', $message->category); + $category = $message->category->entity; $params['contact_category'] = $category; $to = implode(', ', $category->recipients); $recipient_langcode = language_default()->langcode; } - elseif ($message->recipient instanceof User) { + elseif ($message->recipient->target_id) { // Send to the user in the user's preferred language. - $to = $message->recipient->mail; - $recipient_langcode = user_preferred_langcode($message->recipient); + $to = $message->recipient->entity->mail; + $recipient_langcode = user_preferred_langcode($message->recipient->entity); } else { throw new \RuntimeException(t('Unable to determine message recipient.')); @@ -180,19 +179,19 @@ public function save(array $form, array &$form_state) { drupal_mail('contact', 'page_mail', $to, $recipient_langcode, $params, $sender->mail); // If requested, send a copy to the user, using the current language. - if ($message->copy) { + if ($message->copy->value) { drupal_mail('contact', 'page_copy', $sender->mail, $language_interface->langcode, $params, $sender->mail); } // If configured, send an auto-reply, using the current language. - if ($message->category && $category->reply) { + if ($message->category->target_id && $category->reply) { // User contact forms do not support an auto-reply message, so this // message always originates from the site. drupal_mail('contact', 'page_autoreply', $sender->mail, $language_interface->langcode, $params); } \Drupal::service('flood')->register('contact', config('contact.settings')->get('flood.interval')); - if ($message->category) { + if ($message->category->target_id) { watchdog('contact', '%sender-name (@sender-from) sent an e-mail regarding %category.', array( '%sender-name' => $sender->name, '@sender-from' => $sender->mail, @@ -211,8 +210,8 @@ public function save(array $form, array &$form_state) { // To avoid false error messages caused by flood control, redirect away from // the contact form; either to the contacted user account or the front page. - if ($message->recipient instanceof User && user_access('access user profiles')) { - $uri = $message->recipient->uri(); + if ($message->recipient->target_id && user_access('access user profiles')) { + $uri = $message->recipient->entity->uri(); $form_state['redirect'] = array($uri['path'], $uri['options']); } else { diff --git a/core/modules/contact/lib/Drupal/contact/MessageRenderController.php b/core/modules/contact/lib/Drupal/contact/MessageRenderController.php index b3a8a31..1effe40 100644 --- a/core/modules/contact/lib/Drupal/contact/MessageRenderController.php +++ b/core/modules/contact/lib/Drupal/contact/MessageRenderController.php @@ -24,11 +24,11 @@ public function buildContent(array $entities, array $displays, $view_mode, $lang foreach ($entities as $entity) { // Add the message extra field, if enabled. $display = $displays[$entity->bundle()]; - if (!empty($entity->message) && $display->getComponent('message')) { + if ($entity->message->value && $display->getComponent('message')) { $entity->content['message'] = array( '#type' => 'item', '#title' => t('Message'), - '#markup' => check_plain($entity->message), + '#markup' => check_plain($entity->message->value), ); } } diff --git a/core/modules/contact/lib/Drupal/contact/MessageStorageController.php b/core/modules/contact/lib/Drupal/contact/MessageStorageController.php new file mode 100644 index 0000000..927fa2d --- /dev/null +++ b/core/modules/contact/lib/Drupal/contact/MessageStorageController.php @@ -0,0 +1,61 @@ + t('Category'), + 'description' => t('The ID of the associated category.'), + 'type' => 'entity_reference_field', + 'settings' => array('target_type' => 'contact_category'), + ); + $fields['name'] = array( + 'label' => t("The sender's name"), + 'description' => t('The name of the person that is sending the contact message.'), + 'type' => 'string_field', + ); + $fields['mail'] = array( + 'label' => t("The sender's e-mail"), + 'description' => t('The e-mail of the person that is sending the contact message.'), + 'type' => 'string_field', + ); + $fields['subject'] = array( + 'label' => t("The message subject"), + 'description' => t('The subject of the contact message.'), + 'type' => 'string_field', + ); + $fields['message'] = array( + 'label' => t("The message text"), + 'description' => t('The text of the contact message.'), + 'type' => 'string_field', + ); + $fields['copy'] = array( + 'label' => t("Copy"), + 'description' => t('Whether to send a copy of the message to the sender.'), + 'type' => 'string_field', + ); + $fields['recipient'] = array( + 'label' => t('Recipient ID'), + 'description' => t('The ID of the recipient user.'), + 'type' => 'entity_reference_field', + 'settings' => array('target_type' => 'user'), + 'translatable' => TRUE, + ); + return $fields; + } +} diff --git a/core/modules/contact/lib/Drupal/contact/Plugin/Core/Entity/Message.php b/core/modules/contact/lib/Drupal/contact/Plugin/Core/Entity/Message.php index 3bec9e9..bd5584f 100644 --- a/core/modules/contact/lib/Drupal/contact/Plugin/Core/Entity/Message.php +++ b/core/modules/contact/lib/Drupal/contact/Plugin/Core/Entity/Message.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\Annotation\EntityType; use Drupal\Core\Annotation\Translation; use Drupal\Core\Entity\Entity; +use Drupal\Core\Entity\EntityNG; use Drupal\contact\MessageInterface; /** @@ -20,7 +21,7 @@ * label = @Translation("Contact message"), * module = "contact", * controllers = { - * "storage" = "Drupal\Core\Entity\DatabaseStorageController", + * "storage" = "Drupal\contact\MessageStorageController", * "render" = "Drupal\contact\MessageRenderController", * "form" = { * "default" = "Drupal\contact\MessageFormController" @@ -35,7 +36,7 @@ * } * ) */ -class Message extends Entity implements MessageInterface { +class Message extends EntityNG implements MessageInterface { /** * The contact category ID of this message. @@ -104,11 +105,15 @@ public function id() { return NULL; } - /** - * Overrides Drupal\Core\Entity\Entity::bundle(). - */ - public function bundle() { - return $this->category; + protected function init() { + parent::init(); + unset($this->name); + unset($this->message); + unset($this->mail); + unset($this->category); + unset($this->copy); + unset($this->recipient); + unset($this->subject); } /**