contact.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @file
  4. * Enables the use of personal and site-wide contact forms.
  5. */
  6. use Drupal\Core\Form\FormStateInterface;
  7. use Drupal\Core\Routing\RouteMatchInterface;
  8. use Drupal\user\Entity\User;
  9. /**
  10. * Implements hook_help().
  11. */
  12. function contact_help($route_name, RouteMatchInterface $route_match) {
  13. switch ($route_name) {
  14. case 'help.page.contact':
  15. $menu_page = \Drupal::moduleHandler()->moduleExists('menu_ui') ? \Drupal::url('entity.menu.collection') : '#';
  16. $block_page = \Drupal::moduleHandler()->moduleExists('block') ? \Drupal::url('block.admin_display') : '#';
  17. $contact_page = \Drupal::url('entity.contact_form.collection');
  18. $output = '';
  19. $output .= '<h3>' . t('About') . '</h3>';
  20. $output .= '<p>' . t('The Contact module allows visitors to contact registered users on your site, using the personal contact form, and also allows you to set up site-wide contact forms. For more information, see the <a href=":contact">online documentation for the Contact module</a>.', [':contact' => 'https://www.drupal.org/documentation/modules/contact']) . '</p>';
  21. $output .= '<h3>' . t('Uses') . '</h3>';
  22. $output .= '<dl>';
  23. $output .= '<dt>' . t('Using the personal contact form') . '</dt>';
  24. $output .= '<dd>' . t("Site visitors can email registered users on your site by using the personal contact form, without knowing or learning the email address of the recipient. When a site visitor is viewing a user profile, the viewer will see a <em>Contact</em> tab or link, which leads to the personal contact form. The personal contact link is not shown when you are viewing your own profile, and users must have both <em>View user information</em> (to see user profiles) and <em>Use users' personal contact forms</em> permission to see the link. The user whose profile is being viewed must also have their personal contact form enabled (this is a user account setting); viewers with <em>Administer users</em> permission can bypass this setting.") . '</dd>';
  25. $output .= '<dt>' . t('Configuring contact forms') . '</dt>';
  26. $output .= '<dd>' . t('On the <a href=":contact_admin">Contact forms page</a>, you can configure the fields and display of the personal contact form, and you can set up one or more site-wide contact forms. Each site-wide contact form has a machine name, a label, and one or more defined recipients; when a site visitor submits the form, the field values are sent to those recipients.', [':contact_admin' => $contact_page]) . '</dd>';
  27. $output .= '<dt>' . t('Linking to contact forms') . '</dt>';
  28. $output .= '<dd>' . t('One site-wide contact form can be designated as the default contact form. If you choose to designate a default form, the <em>Contact</em> menu link in the <em>Footer</em> menu will link to it. You can modify this link from the <a href=":menu-settings">Menus page</a> if you have the Menu UI module installed. You can also create links to other contact forms; the URL for each form you have set up has format <em>contact/machine_name_of_form</em>.', [':menu-settings' => $menu_page]) . '</p>';
  29. $output .= '<dt>' . t('Adding content to contact forms') . '</dt>';
  30. $output .= '<dd>' . t('From the <a href=":contact_admin">Contact forms page</a>, you can configure the fields to be shown on contact forms, including their labels and help text. If you would like other content (such as text or images) to appear on a contact form, use a block. You can create and edit blocks on the <a href=":blocks">Block layout page</a>, if the Block module is installed.', [':blocks' => $block_page, ':contact_admin' => $contact_page]) . '</dd>';
  31. $output .= '</dl>';
  32. return $output;
  33. }
  34. }
  35. /**
  36. * Implements hook_entity_type_alter().
  37. */
  38. function contact_entity_type_alter(array &$entity_types) {
  39. /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  40. $entity_types['user']->setLinkTemplate('contact-form', '/user/{user}/contact');
  41. }
  42. /**
  43. * Implements hook_entity_extra_field_info().
  44. */
  45. function contact_entity_extra_field_info() {
  46. $fields = [];
  47. foreach (array_keys(\Drupal::service('entity_type.bundle.info')->getBundleInfo('contact_message')) as $bundle) {
  48. $fields['contact_message'][$bundle]['form']['name'] = [
  49. 'label' => t('Sender name'),
  50. 'description' => t('Text'),
  51. 'weight' => -50,
  52. ];
  53. $fields['contact_message'][$bundle]['form']['mail'] = [
  54. 'label' => t('Sender email'),
  55. 'description' => t('Email'),
  56. 'weight' => -40,
  57. ];
  58. if ($bundle == 'personal') {
  59. $fields['contact_message'][$bundle]['form']['recipient'] = [
  60. 'label' => t('Recipient username'),
  61. 'description' => t('User'),
  62. 'weight' => -30,
  63. ];
  64. }
  65. $fields['contact_message'][$bundle]['form']['preview'] = [
  66. 'label' => t('Preview sender message'),
  67. 'description' => t('Preview'),
  68. 'weight' => 40,
  69. ];
  70. $fields['contact_message'][$bundle]['form']['copy'] = [
  71. 'label' => t('Send copy to sender'),
  72. 'description' => t('Option'),
  73. 'weight' => 50,
  74. ];
  75. }
  76. $fields['user']['user']['form']['contact'] = [
  77. 'label' => t('Contact settings'),
  78. 'description' => t('Contact module form element.'),
  79. 'weight' => 5,
  80. ];
  81. return $fields;
  82. }
  83. /**
  84. * Implements hook_menu_local_tasks_alter().
  85. *
  86. * Hides the 'Contact' tab on the user profile if the user does not have an
  87. * email address configured.
  88. */
  89. function contact_menu_local_tasks_alter(&$data, $route_name) {
  90. if ($route_name == 'entity.user.canonical') {
  91. foreach ($data['tabs'][0] as $href => $tab_data) {
  92. if ($href == 'entity.user.contact_form') {
  93. $link_params = $tab_data['#link']['url']->getRouteParameters();
  94. $account = User::load($link_params['user']);
  95. if (!$account->getEmail()) {
  96. unset($data['tabs'][0]['entity.user.contact_form']);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * Implements hook_mail().
  104. */
  105. function contact_mail($key, &$message, $params) {
  106. $contact_message = $params['contact_message'];
  107. /** @var $sender \Drupal\user\UserInterface */
  108. $sender = $params['sender'];
  109. $language = \Drupal::languageManager()->getLanguage($message['langcode']);
  110. $variables = [
  111. '@site-name' => \Drupal::config('system.site')->get('name'),
  112. '@subject' => $contact_message->getSubject(),
  113. '@form' => !empty($params['contact_form']) ? $params['contact_form']->label() : NULL,
  114. '@form-url' => \Drupal::url('<current>', [], ['absolute' => TRUE, 'language' => $language]),
  115. '@sender-name' => $sender->getDisplayName(),
  116. ];
  117. if ($sender->isAuthenticated()) {
  118. $variables['@sender-url'] = $sender->url('canonical', ['absolute' => TRUE, 'language' => $language]);
  119. }
  120. else {
  121. $variables['@sender-url'] = $params['sender']->getEmail();
  122. }
  123. $options = ['langcode' => $language->getId()];
  124. switch ($key) {
  125. case 'page_mail':
  126. case 'page_copy':
  127. $message['subject'] .= t('[@form] @subject', $variables, $options);
  128. $message['body'][] = t("@sender-name (@sender-url) sent a message using the contact form at @form-url.", $variables, $options);
  129. $build = entity_view($contact_message, 'mail');
  130. $message['body'][] = \Drupal::service('renderer')->renderPlain($build);
  131. break;
  132. case 'page_autoreply':
  133. $message['subject'] .= t('[@form] @subject', $variables, $options);
  134. $message['body'][] = $params['contact_form']->getReply();
  135. break;
  136. case 'user_mail':
  137. case 'user_copy':
  138. $variables += [
  139. '@recipient-name' => $params['recipient']->getDisplayName(),
  140. '@recipient-edit-url' => $params['recipient']->url('edit-form', ['absolute' => TRUE, 'language' => $language]),
  141. ];
  142. $message['subject'] .= t('[@site-name] @subject', $variables, $options);
  143. $message['body'][] = t('Hello @recipient-name,', $variables, $options);
  144. $message['body'][] = t("@sender-name (@sender-url) has sent you a message via your contact form at @site-name.", $variables, $options);
  145. $message['body'][] = t("If you don't want to receive such emails, you can change your settings at @recipient-edit-url.", $variables, $options);
  146. $build = entity_view($contact_message, 'mail');
  147. $message['body'][] = \Drupal::service('renderer')->renderPlain($build);
  148. break;
  149. }
  150. }
  151. /**
  152. * Implements hook_form_FORM_ID_alter().
  153. *
  154. * Add the enable personal contact form to an individual user's account page.
  155. *
  156. * @see \Drupal\user\ProfileForm::form()
  157. */
  158. function contact_form_user_form_alter(&$form, FormStateInterface $form_state) {
  159. $form['contact'] = [
  160. '#type' => 'details',
  161. '#title' => t('Contact settings'),
  162. '#open' => TRUE,
  163. '#weight' => 5,
  164. ];
  165. $account = $form_state->getFormObject()->getEntity();
  166. if (!\Drupal::currentUser()->isAnonymous() && $account->id()) {
  167. $account_data = \Drupal::service('user.data')->get('contact', $account->id(), 'enabled');
  168. }
  169. $form['contact']['contact'] = [
  170. '#type' => 'checkbox',
  171. '#title' => t('Personal contact form'),
  172. '#default_value' => isset($account_data) ? $account_data : \Drupal::config('contact.settings')->get('user_default_enabled'),
  173. '#description' => t('Allow other users to contact you via a personal contact form which keeps your email address hidden. Note that some privileged users such as site administrators are still able to contact you even if you choose to disable this feature.'),
  174. ];
  175. $form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit';
  176. }
  177. /**
  178. * Submit callback for the user profile form to save the contact page setting.
  179. */
  180. function contact_user_profile_form_submit($form, FormStateInterface $form_state) {
  181. $account = $form_state->getFormObject()->getEntity();
  182. if ($account->id() && $form_state->hasValue('contact')) {
  183. \Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state->getValue('contact'));
  184. }
  185. }
  186. /**
  187. * Implements hook_form_FORM_ID_alter().
  188. *
  189. * Add the default personal contact setting on the user settings page.
  190. *
  191. * @see \Drupal\user\AccountSettingsForm
  192. */
  193. function contact_form_user_admin_settings_alter(&$form, FormStateInterface $form_state) {
  194. $form['contact'] = [
  195. '#type' => 'details',
  196. '#title' => t('Contact settings'),
  197. '#open' => TRUE,
  198. '#weight' => 0,
  199. ];
  200. $form['contact']['contact_default_status'] = [
  201. '#type' => 'checkbox',
  202. '#title' => t('Enable the personal contact form by default for new users'),
  203. '#description' => t('Changing this setting will not affect existing users.'),
  204. '#default_value' => \Drupal::configFactory()->getEditable('contact.settings')->get('user_default_enabled'),
  205. ];
  206. // Add submit handler to save contact configuration.
  207. $form['#submit'][] = 'contact_form_user_admin_settings_submit';
  208. }
  209. /**
  210. * Form submission handler for user_admin_settings().
  211. *
  212. * @see contact_form_user_admin_settings_alter()
  213. */
  214. function contact_form_user_admin_settings_submit($form, FormStateInterface $form_state) {
  215. \Drupal::configFactory()->getEditable('contact.settings')
  216. ->set('user_default_enabled', $form_state->getValue('contact_default_status'))
  217. ->save();
  218. }