contact.module 10.7 KB
Newer Older
Dries's avatar
Dries committed
1
<?php
2
use Symfony\Component\HttpFoundation\Request;
Dries's avatar
Dries committed
3 4 5

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

/**
10
 * Implements hook_help().
Dries's avatar
Dries committed
11
 */
12 13 14
function contact_help($route_name, Request $request) {
  switch ($route_name) {
    case 'help.page.contact':
15 16
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
17
      $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 e-mail 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>';
18 19 20 21 22
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('User contact forms') . '</dt>';
      $output .= '<dd>' . t('Site users can be contacted with a user contact form that keeps their e-mail 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>';
      $output .= '<dt>' . t('Site-wide contact forms') . '</dt>';
23
      $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 categories for directing the contact form messages to a set of defined recipients. Common categories 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). E-mail addresses defined within a category are not displayed publicly.', array('@contact' => url('contact'))) . '</p>';
24
      $output .= '<dt>' . t('Navigation') . '</dt>';
25
      $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' => url('admin/structure/menu'))) . '</dd>';
26 27
      $output .= '<dt>' . t('Customization') . '</dt>';
      $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' => url('admin/structure/block'))) . '</dd>';
28
      $output .= '</dl>';
29
      return $output;
30 31

    case 'contact.category_list':
32
      $output = '<p>' . t('Add one or more categories on this page to set up your site-wide <a href="@form">contact form</a>.', array('@form' => url('contact'))) . '</p>';
33
      $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' => url('admin/structure/menu'))) . '</p>';
34
      $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' => url('admin/structure/block'))) . '</p>';
35
      return $output;
Dries's avatar
Dries committed
36 37 38
  }
}

39
/**
40
 * Implements hook_permission().
41
 */
42
function contact_permission() {
43
  return array(
44
    'administer contact forms' => array(
45
      'title' => t('Administer contact forms and contact form settings'),
46 47
    ),
    'access site-wide contact form' => array(
48
      'title' => t('Use the site-wide contact form'),
49
    ),
50
    'access user contact forms' => array(
51
      'title' => t("Use users' personal contact forms"),
52
    ),
53
  );
54
}
55

56
/**
57
 * Implements hook_entity_type_alter().
58
 */
59 60 61
function contact_entity_type_alter(array &$entity_types) {
  /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  $entity_types['user']->setLinkTemplate('contact-form', 'contact.personal_page');
62 63
}

64
/**
65
 * Implements hook_entity_extra_field_info().
66
 */
67
function contact_entity_extra_field_info() {
68
  $fields = array();
69
  foreach (array_keys(entity_get_bundles('contact_message')) as $bundle) {
70 71 72 73 74 75 76 77 78 79
    $fields['contact_message'][$bundle]['form']['name'] = array(
      'label' => t('Sender name'),
      'description' => t('Text'),
      'weight' => -50,
    );
    $fields['contact_message'][$bundle]['form']['mail'] = array(
      'label' => t('Sender e-mail'),
      'description' => t('E-mail'),
      'weight' => -40,
    );
80 81 82 83 84 85 86
    if ($bundle == 'personal') {
      $fields['contact_message'][$bundle]['form']['recipient'] = array(
        'label' => t('Recipient user name'),
        'description' => t('User'),
        'weight' => -30,
      );
    }
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    $fields['contact_message'][$bundle]['form']['subject'] = array(
      'label' => t('Subject'),
      'description' => t('Text'),
      'weight' => -10,
    );
    $fields['contact_message'][$bundle]['form']['message'] = array(
      'label' => t('Message'),
      'description' => t('Long text'),
      'weight' => 0,
    );
    $fields['contact_message'][$bundle]['form']['copy'] = array(
      'label' => t('Send copy to sender'),
      'description' => t('Option'),
      'weight' => 50,
    );

    $fields['contact_message'][$bundle]['display']['message'] = array(
      'label' => t('Message'),
      'description' => t('The main contact message'),
      'weight' => 0,
    );
  }
109 110 111 112 113 114

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

118
/**
119
 * Implements hook_mail().
120 121
 */
function contact_mail($key, &$message, $params) {
122
  $contact_message = $params['contact_message'];
123
  /** @var $sender \Drupal\user\UserInterface */
124
  $sender = $params['sender'];
125
  $language = language_load($message['langcode']);
126

127
  $variables = array(
128
    '!site-name' => \Drupal::config('system.site')->get('name'),
129 130
    '!subject' => $contact_message->getSubject(),
    '!category' => !empty($params['contact_category']) ? $params['contact_category']->label() : NULL,
131
    '!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)),
132
    '!sender-name' => user_format_name($sender),
133
  );
134
  if ($sender->isAuthenticated()) {
135
    $variables['!sender-url'] = $sender->url('canonical', array('absolute' => TRUE, 'language' => $language));
136 137
  }
  else {
138
    $variables['!sender-url'] = $params['sender']->getEmail();
139 140
  }

141
  $options = array('langcode' => $language->id);
142

143 144 145
  switch ($key) {
    case 'page_mail':
    case 'page_copy':
146 147
      $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);
148
      $build = entity_view($contact_message, 'mail', $language->id);
149
      $message['body'][] = drupal_render($build);
150
      break;
151

152
    case 'page_autoreply':
153 154
      $message['subject'] .= t('[!category] !subject', $variables, $options);
      $message['body'][] = $params['contact_category']->reply;
155
      break;
156

157 158
    case 'user_mail':
    case 'user_copy':
159
      $variables += array(
160
        '!recipient-name' => user_format_name($params['recipient']),
161
        '!recipient-edit-url' => url('user/' . $params['recipient']->id() . '/edit', array('absolute' => TRUE, 'language' => $language)),
162
      );
163 164 165 166
      $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);
      $message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !recipient-edit-url.", $variables, $options);
167
      $build = entity_view($contact_message, 'mail', $language->id);
168
      $message['body'][] = drupal_render($build);
169 170
      break;
  }
171
}
172

173
/**
174
 * Implements hook_form_FORM_ID_alter().
175 176
 *
 * Add the enable personal contact form to an individual user's account page.
177
 *
178
 * @see \Drupal\user\ProfileForm::form()
179
 */
180
function contact_form_user_form_alter(&$form, &$form_state) {
181
  $form['contact'] = array(
182
    '#type' => 'details',
183
    '#title' => t('Contact settings'),
184
    '#open' => TRUE,
185 186
    '#weight' => 5,
  );
187
  $account = $form_state['controller']->getEntity();
188 189 190
  if (!\Drupal::currentUser()->isAnonymous() && $account->id()) {
    $account_data = \Drupal::service('user.data')->get('contact', $account->id(), 'enabled');
  }
191 192 193
  $form['contact']['contact'] = array(
    '#type' => 'checkbox',
    '#title' => t('Personal contact form'),
194
    '#default_value' => isset($account_data) ? $account_data : \Drupal::config('contact.settings')->get('user_default_enabled'),
195
    '#description' => t('Allow other users to contact you via a personal contact form which keeps your e-mail 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.'),
196
  );
197
  $form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit';
198 199
}

200
/**
201
 * Submit callback for the user profile form to save the contact page setting.
202
 */
203 204 205
function contact_user_profile_form_submit($form, &$form_state) {
  $account = $form_state['controller']->getEntity();
  if ($account->id() && isset($form_state['values']['contact'])) {
206
    \Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state['values']['contact']);
207
  }
208 209
}

210
/**
211
 * Implements hook_form_FORM_ID_alter().
212 213
 *
 * Add the default personal contact setting on the user settings page.
214 215
 *
 * @see user_admin_settings()
216
 */
217
function contact_form_user_admin_settings_alter(&$form, &$form_state) {
218
  $form['contact'] = array(
219
    '#type' => 'details',
220
    '#title' => t('Contact settings'),
221
    '#open' => TRUE,
222 223 224 225 226
    '#weight' => 0,
  );
  $form['contact']['contact_default_status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable the personal contact form by default for new users.'),
227
    '#description' => t('Changing this setting will not affect existing users.'),
228
    '#default_value' => \Drupal::config('contact.settings')->get('user_default_enabled'),
229
  );
230 231 232 233 234 235 236 237 238 239
  // 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()
 */
function contact_form_user_admin_settings_submit($form, &$form_state) {
240
  \Drupal::config('contact.settings')
241 242
    ->set('user_default_enabled', $form_state['values']['contact_default_status'])
    ->save();
243
}