contact.module 10.8 KB
Newer Older
Dries's avatar
 
Dries committed
1 2 3 4
<?php

/**
 * @file
5
 * Enables the use of personal and site-wide contact forms.
Dries's avatar
 
Dries committed
6 7
 */

8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\Core\Routing\RouteMatchInterface;
10
use Drupal\user\Entity\User;
11

Dries's avatar
 
Dries committed
12
/**
13
 * Implements hook_help().
Dries's avatar
 
Dries committed
14
 */
15
function contact_help($route_name, RouteMatchInterface $route_match) {
16 17
  switch ($route_name) {
    case 'help.page.contact':
18 19
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
20
      $output .= '<p>' . t('The Contact module allows visitors to contact site administrators and other users. Users specify a subject, write their message, and can have a copy of their message sent to their own email address. For more information, see the online handbook entry for <a href="@contact">Contact module</a>.', array('@contact' => 'http://drupal.org/documentation/modules/contact')) . '</p>';
21 22 23
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('User contact forms') . '</dt>';
24
      $output .= '<dd>' . t('Site users can be contacted with a user contact form that keeps their email address private. Users may enable or disable their personal contact forms by editing their <em>My account</em> page. If enabled, a <em>Contact</em> tab leads to a personal contact form displayed on their user profile. Site administrators are still able to use the contact form, even if has been disabled. The <em>Contact</em> tab is not shown when you view your own profile.') . '</dd>';
25
      $output .= '<dt>' . t('Site-wide contact forms') . '</dt>';
26
      $output .= '<dd>' . t('The <a href="@contact">Contact page</a> provides a simple form for users with the <em>Use the site-wide contact form</em> permission to send comments, feedback, or other requests. You can create forms for directing the contact messages to a set of defined recipients. Common forms for a business site, for example, might include "Website feedback" (messages are forwarded to website administrators) and "Product information" (messages are forwarded to members of the sales department). Email addresses defined within a form are not displayed publicly.', array('@contact' => \Drupal::url('contact.site_page'))) . '</p>';
27
      $output .= '<dt>' . t('Navigation') . '</dt>';
28
      $output .= '<dd>' . t('When the site-wide contact form is enabled, a link in the <em>Footer</em> menu is created, which you can modify on the <a href="@menu">Menus administration page</a>.', array('@menu' => \Drupal::url('entity.menu.collection'))) . '</dd>';
29
      $output .= '<dt>' . t('Customization') . '</dt>';
30
      $output .= '<dd>' . t('If you would like additional text to appear on the site-wide or personal contact page, use a block. You can create and edit blocks on the <a href="@blocks">Blocks administration page</a>.', array('@blocks' => \Drupal::url('block.admin_display'))) . '</dd>';
31
      $output .= '</dl>';
32
      return $output;
33

34
    case 'entity.contact_form.collection':
35
      $output = '<p>' . t('Add one or more forms on this page to set up your site-wide <a href="@form">contact form</a>.', array('@form' => \Drupal::url('contact.site_page'))) . '</p>';
36
      $output .= '<p>' . t('A <em>Contact</em> menu item is added to the <em>Footer</em> menu, which you can modify on the <a href="@menu-settings">Menus administration page</a>.', array('@menu-settings' => \Drupal::url('entity.menu.collection'))) . '</p>';
37
      $output .= '<p>' . t('If you would like additional text to appear on the site-wide contact page, use a block. You can create and edit blocks on the <a href="@blocks">Blocks administration page</a>.', array('@blocks' => \Drupal::url('block.admin_display'))) . '</p>';
38
      return $output;
Dries's avatar
 
Dries committed
39 40 41
  }
}

42
/**
43
 * Implements hook_entity_type_alter().
44
 */
45 46
function contact_entity_type_alter(array &$entity_types) {
  /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
47
  $entity_types['user']->setLinkTemplate('contact-form', '/user/{user}/contact');
48 49
}

50
/**
51
 * Implements hook_entity_extra_field_info().
52
 */
53
function contact_entity_extra_field_info() {
54
  $fields = array();
55
  foreach (array_keys(entity_get_bundles('contact_message')) as $bundle) {
56 57 58 59 60 61
    $fields['contact_message'][$bundle]['form']['name'] = array(
      'label' => t('Sender name'),
      'description' => t('Text'),
      'weight' => -50,
    );
    $fields['contact_message'][$bundle]['form']['mail'] = array(
62 63
      'label' => t('Sender email'),
      'description' => t('Email'),
64 65
      'weight' => -40,
    );
66 67
    if ($bundle == 'personal') {
      $fields['contact_message'][$bundle]['form']['recipient'] = array(
68
        'label' => t('Recipient username'),
69 70 71 72
        'description' => t('User'),
        'weight' => -30,
      );
    }
73 74 75 76 77 78
    $fields['contact_message'][$bundle]['form']['copy'] = array(
      'label' => t('Send copy to sender'),
      'description' => t('Option'),
      'weight' => 50,
    );
  }
79 80 81 82 83 84

  $fields['user']['user']['form']['contact'] = array(
    'label' => t('Contact settings'),
    'description' => t('Contact module form element.'),
    'weight' => 5,
  );
85 86 87
  return $fields;
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/**
 * Implements hook_menu_local_tasks_alter().
 *
 * Hides the 'Contact' tab on the user profile if the user does not have an
 * email address configured.
 */
function contact_menu_local_tasks_alter(&$data, $route_name) {
  if ($route_name == 'entity.user.canonical') {
    foreach ($data['tabs'][0] as $href => $tab_data) {
      if ($href == 'entity.user.contact_form') {
        $link_params = $tab_data['#link']['url']->getRouteParameters();
        $account = User::load($link_params['user']);
        if (!$account->getEmail()) {
          unset($data['tabs'][0]['entity.user.contact_form']);
        }
      }
    }
  }
}

108
/**
109
 * Implements hook_mail().
110 111
 */
function contact_mail($key, &$message, $params) {
112
  $contact_message = $params['contact_message'];
113
  /** @var $sender \Drupal\user\UserInterface */
114
  $sender = $params['sender'];
115
  $language = \Drupal::languageManager()->getLanguage($message['langcode']);
116

117
  $variables = array(
118
    '!site-name' => \Drupal::config('system.site')->get('name'),
119
    '!subject' => $contact_message->getSubject(),
120
    '!form' => !empty($params['contact_form']) ? $params['contact_form']->label() : NULL,
121
    '!form-url' => \Drupal::url('<current>', [], ['absolute' => TRUE, 'language' => $language]),
122
    '!sender-name' => user_format_name($sender),
123
  );
124
  if ($sender->isAuthenticated()) {
125
    $variables['!sender-url'] = $sender->url('canonical', array('absolute' => TRUE, 'language' => $language));
126 127
  }
  else {
128
    $variables['!sender-url'] = $params['sender']->getEmail();
129 130
  }

131
  $options = array('langcode' => $language->getId());
132

133 134 135
  switch ($key) {
    case 'page_mail':
    case 'page_copy':
136
      $message['subject'] .= t('[!form] !subject', $variables, $options);
137
      $message['body'][] = t("!sender-name (!sender-url) sent a message using the contact form at !form-url.", $variables, $options);
138
      $build = entity_view($contact_message, 'mail', $language->getId());
139
      $message['body'][] = \Drupal::service('renderer')->renderPlain($build);
140
      break;
141

142
    case 'page_autoreply':
143
      $message['subject'] .= t('[!form] !subject', $variables, $options);
144
      $message['body'][] = $params['contact_form']->getReply();
145
      break;
146

147 148
    case 'user_mail':
    case 'user_copy':
149
      $variables += array(
150
        '!recipient-name' => user_format_name($params['recipient']),
151
        '!recipient-edit-url' => $params['recipient']->url('edit-form', array('absolute' => TRUE, 'language' => $language)),
152
      );
153 154 155
      $message['subject'] .= t('[!site-name] !subject', $variables, $options);
      $message['body'][] = t('Hello !recipient-name,', $variables, $options);
      $message['body'][] = t("!sender-name (!sender-url) has sent you a message via your contact form at !site-name.", $variables, $options);
156
      $message['body'][] = t("If you don't want to receive such emails, you can change your settings at !recipient-edit-url.", $variables, $options);
157
      $build = entity_view($contact_message, 'mail', $language->getId());
158
      $message['body'][] = \Drupal::service('renderer')->renderPlain($build);
159 160
      break;
  }
161
}
162

163
/**
164
 * Implements hook_form_FORM_ID_alter().
165 166
 *
 * Add the enable personal contact form to an individual user's account page.
167
 *
168
 * @see \Drupal\user\ProfileForm::form()
169
 */
170
function contact_form_user_form_alter(&$form, FormStateInterface $form_state) {
171
  $form['contact'] = array(
172
    '#type' => 'details',
173
    '#title' => t('Contact settings'),
174
    '#open' => TRUE,
175 176
    '#weight' => 5,
  );
177
  $account = $form_state->getFormObject()->getEntity();
178 179 180
  if (!\Drupal::currentUser()->isAnonymous() && $account->id()) {
    $account_data = \Drupal::service('user.data')->get('contact', $account->id(), 'enabled');
  }
181 182 183
  $form['contact']['contact'] = array(
    '#type' => 'checkbox',
    '#title' => t('Personal contact form'),
184
    '#default_value' => isset($account_data) ? $account_data : \Drupal::config('contact.settings')->get('user_default_enabled'),
185
    '#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.'),
186
  );
187
  $form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit';
188 189
}

190
/**
191
 * Submit callback for the user profile form to save the contact page setting.
192
 */
193
function contact_user_profile_form_submit($form, FormStateInterface $form_state) {
194
  $account = $form_state->getFormObject()->getEntity();
195 196
  if ($account->id() && $form_state->hasValue('contact')) {
    \Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state->getValue('contact'));
197
  }
198 199
}

200
/**
201
 * Implements hook_form_FORM_ID_alter().
202 203
 *
 * Add the default personal contact setting on the user settings page.
204
 *
205
 * @see \Drupal\user\AccountSettingsForm
206
 */
207
function contact_form_user_admin_settings_alter(&$form, FormStateInterface $form_state) {
208
  $form['contact'] = array(
209
    '#type' => 'details',
210
    '#title' => t('Contact settings'),
211
    '#open' => TRUE,
212 213 214 215
    '#weight' => 0,
  );
  $form['contact']['contact_default_status'] = array(
    '#type' => 'checkbox',
216
    '#title' => t('Enable the personal contact form by default for new users'),
217
    '#description' => t('Changing this setting will not affect existing users.'),
218
    '#default_value' => \Drupal::configFactory()->getEditable('contact.settings')->get('user_default_enabled'),
219
  );
220 221 222 223 224 225 226 227 228
  // Add submit handler to save contact configuration.
  $form['#submit'][] = 'contact_form_user_admin_settings_submit';
}

/**
 * Form submission handler for user_admin_settings().
 *
 * @see contact_form_user_admin_settings_alter()
 */
229
function contact_form_user_admin_settings_submit($form, FormStateInterface $form_state) {
230
  \Drupal::configFactory()->getEditable('contact.settings')
231
    ->set('user_default_enabled', $form_state->getValue('contact_default_status'))
232
    ->save();
233
}