diff --git a/core/modules/contact/contact.module b/core/modules/contact/contact.module index 431efe4..7df0286 100644 --- a/core/modules/contact/contact.module +++ b/core/modules/contact/contact.module @@ -162,6 +162,7 @@ function contact_menu_local_tasks_alter(&$data, $router_item, $root_path) { } // Expand the dynamic argument into a tab for each type. $entities = entity_load_multiple('contact_category'); + uasort($entities, 'Drupal\Core\Config\Entity\ConfigEntityBase::sort'); $default = config('contact.settings')->get('default_category'); foreach ($entities as $entity) { if ($entity->id() === $default) { @@ -349,6 +350,12 @@ function contact_field_extra_fields() { 'description' => t('Option: Send copy to sender'), 'weight' => 50, ); + + $fields['contact_message'][$bundle]['display']['message'] = array( + 'label' => t('Message'), + 'description' => t('The main contact message'), + 'weight' => 0, + ); } return $fields; } @@ -396,7 +403,7 @@ function contact_mail($key, &$message, $params) { '!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)), '!sender-name' => user_format_name($sender), ); - if ($sender->uid) { + if (!empty($sender->uid)) { $sender_uri = $sender->uri(); $variables['!sender-url'] = url($sender_uri['path'], array('absolute' => TRUE, 'language' => $language) + $sender_uri['options']); } @@ -411,7 +418,7 @@ function contact_mail($key, &$message, $params) { case 'page_copy': $message['subject'] .= t('[!category] !subject', $variables, $options); $message['body'][] = t("!sender-name (!sender-url) sent a message using the contact form at !form-url.", $variables, $options); - $message['body'][] = $contact_message->message; + $message['body'][] = entity_view($contact_message, 'mail', $language->langcode); break; case 'page_autoreply': diff --git a/core/modules/contact/contact.pages.inc b/core/modules/contact/contact.pages.inc index b38d14e..1e53b76 100644 --- a/core/modules/contact/contact.pages.inc +++ b/core/modules/contact/contact.pages.inc @@ -45,7 +45,6 @@ function contact_site_page(Category $category = NULL) { } } } - // @todo Add 'uid' or 'sender' property and set property here? $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 5bb37b9..e536675 100644 --- a/core/modules/contact/lib/Drupal/contact/MessageFormController.php +++ b/core/modules/contact/lib/Drupal/contact/MessageFormController.php @@ -29,11 +29,7 @@ public function form(array $form, array &$form_state, EntityInterface $message) '#theme_wrappers' => array('container__preview'), '#attributes' => array('class' => array('preview')), ); - $form['preview']['message'] = entity_view($message, 'preview'); - - $form['preview']['mail'] = entity_view($message, 'mail'); - $form['preview']['mail']['#prefix'] = '
';
-      $form['preview']['mail']['#suffix'] = '
'; + $form['preview']['message'] = entity_view($message, 'full'); } $form['name'] = array( @@ -58,14 +54,15 @@ public function form(array $form, array &$form_state, EntityInterface $message) $form['name']['#value'] = $user->name; $form['name']['#required'] = FALSE; $form['name']['#markup'] = check_plain(user_format_name($user)); + $form['mail']['#type'] = 'item'; $form['mail']['#value'] = $user->mail; $form['mail']['#required'] = FALSE; $form['mail']['#markup'] = check_plain($user->mail); } - // If there is a recipient, then this is a user contact form and there is no - // category. + // 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) { $form['recipient'] = array( '#type' => 'item', @@ -77,42 +74,12 @@ public function form(array $form, array &$form_state, EntityInterface $message) ), ); } - // The site-wide contact form always passes a category and defaults to the - // configured default category. - elseif (!empty($message->category)) { + else { $form['category'] = array( '#type' => 'value', '#value' => $message->category, ); } - // There may be other callers, which request this form without a category. - // In this case, the Message entity automatically sets 'fieldable' to FALSE. - // @todo Consider to remove this fallback. - else { - // Prepare array for select options. - $categories = entity_load_multiple('contact_category'); - uasort($categories, 'Drupal\Core\Config\Entity\ConfigEntityBase::sort'); - $options = array(); - foreach ($categories as $category) { - $options[$category->id()] = $category->label(); - } - // If there is more than one category available and no default category has - // been selected, prepend a default placeholder value. - $config = config('contact.settings'); - $default_category = $config->get('default_category'); - if (!$default_category) { - $default_category = !empty($categories) ? key($categories) : NULL; - } - $form['category'] = array( - '#type' => 'select', - '#title' => t('Category'), - '#default_value' => $default_category, - '#options' => $options, - '#empty_value' => 0, - '#required' => TRUE, - '#access' => count($categories) > 1, - ); - } $form['subject'] = array( '#type' => 'textfield', @@ -157,6 +124,9 @@ public function actions(array $form, array &$form_state) { return $elements; } + /** + * Form submission handler for the 'preview' action. + */ public function preview(array $form, array &$form_state) { $message = $this->getEntity($form_state); $message->preview = TRUE; @@ -168,13 +138,14 @@ public function preview(array $form, array &$form_state) { */ public function save(array $form, array &$form_state) { global $user; + $language_interface = language(LANGUAGE_TYPE_INTERFACE); $message = $this->getEntity($form_state); $sender = clone user_load($user->uid); if (!$user->uid) { - // At this point, $message->sender contains drupal_anonymous_user(), so we - // need to take over the submitted form values. + // 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; // Save the anonymous user information to a cookie for reuse. @@ -188,12 +159,11 @@ public function save(array $form, array &$form_state) { $params['contact_message'] = $message; $params['sender'] = $sender; - // @todo Category should be available in $form_state to hook_form_alter() already. if ($message->category) { + // Send to the category recipient(s), using the site's default language. $category = entity_load('contact_category', $message->category); $params['contact_category'] = $category; - // Send to configured recipient(s), using the site's default language. $to = implode(', ', $category->recipients); $recipient_langcode = language_default()->langcode; } @@ -203,7 +173,7 @@ public function save(array $form, array &$form_state) { $recipient_langcode = user_preferred_langcode($message->recipient); } else { - throw new \RuntimeException('Unable to determine message recipient.'); + throw new \RuntimeException(t('Unable to determine message recipient.')); } // Send e-mail to the recipient(s). diff --git a/core/modules/contact/lib/Drupal/contact/MessageRenderController.php b/core/modules/contact/lib/Drupal/contact/MessageRenderController.php index a2163b9..3044a85 100644 --- a/core/modules/contact/lib/Drupal/contact/MessageRenderController.php +++ b/core/modules/contact/lib/Drupal/contact/MessageRenderController.php @@ -15,12 +15,37 @@ */ class MessageRenderController extends EntityRenderController { + /** + * Overrides Drupal\Core\Entity\EntityRenderController::buildContent(). + */ + public function buildContent(array $entities = array(), $view_mode = 'full', $langcode = NULL) { + parent::buildContent($entities, $view_mode, $langcode); + + foreach ($entities as $entity) { + // Add the message extra field, if enabled. + $bundle = $entity->bundle(); + $entity_view_mode = $entity->content['#view_mode']; + $fields = field_extra_fields_get_display($this->entityType, $bundle, $entity_view_mode); + if (!empty($entity->message) && !empty($fields['message']['visible'])) { + $entity->content['message'] = array( + '#type' => 'item', + '#title' => t('Message'), + '#markup' => check_plain($entity->message), + '#weight' => $fields['message']['weight'], + ); + } + } + } + + /** + * Overrides Drupal\Core\Entity\EntityRenderController::view(). + */ public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) { $build = parent::view($entity, $view_mode, $langcode); if ($view_mode == 'mail') { - // Convert field labels into headings, as long as drupal_html_to_text() - // fails to convert DIVs correctly. + // Convert field labels into headings. + // @todo Improve drupal_html_to_text() to convert DIVs correctly. foreach (element_children($build) as $key) { if (isset($build[$key]['#label_display']) && $build[$key]['#label_display'] == 'above') { $build[$key] += array('#prefix' => '');