contact.module 12.3 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
 */

/**
9
 * Implements hook_help().
Dries's avatar
 
Dries committed
10
 */
11 12
function contact_help($path, $arg) {
  switch ($path) {
13
    case 'admin/help#contact':
14 15
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
16
      $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>';
17 18 19 20 21
      $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>';
22
      $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>';
23
      $output .= '<dt>' . t('Navigation') . '</dt>';
24
      $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>';
25 26
      $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>';
27
      $output .= '</dl>';
28
      return $output;
29
    case 'admin/structure/contact':
30
      $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>';
31
      $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>';
32
      $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>';
33
      return $output;
Dries's avatar
 
Dries committed
34 35 36
  }
}

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

Dries's avatar
 
Dries committed
54
/**
55
 * Implements hook_menu().
Dries's avatar
 
Dries committed
56
 */
57
function contact_menu() {
58
  $items['admin/structure/contact'] = array(
59
    'title' => 'Contact form categories',
60
    'description' => 'Create a system contact form and set up categories for the form to use.',
61
    'route_name' => 'contact.category_list',
62
  );
63
  $items['admin/structure/contact/manage/%contact_category'] = array(
64
    'title' => 'Edit contact category',
65
    'route_name' => 'contact.category_edit',
66
  );
67

68
  $items['contact'] = array(
69
    'title' => 'Contact',
70
    'route_name' => 'contact.site_page',
71
    'menu_name' => 'footer',
72 73
    'type' => MENU_SUGGESTED_ITEM,
  );
74 75
  $items['contact/%contact_category'] = array(
    'title' => 'Contact category form',
76
    'route_name' => 'contact.site_page_category',
77 78
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
  );
Dries's avatar
 
Dries committed
79 80 81
  return $items;
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/**
 * Implements hook_menu_link_defaults().
 */
function contact_menu_link_defaults() {
  $links['contact.admin.categories'] = array(
    'link_title' => 'Contact form categories',
    'parent' => 'system.admin.structure',
    'description' => 'Create a system contact form and set up categories for the form to use.',
    'route_name' => 'contact.category_list',
  );

  $links['contact.site_page'] = array(
    'link_title' => 'Contact',
    'route_name' => 'contact.site_page',
    'menu_name' => 'footer',
    'type' => MENU_SUGGESTED_ITEM,
  );
  return $links;
}

102
/**
103
 * Implements hook_entity_bundle_info().
104
 */
105 106
function contact_entity_bundle_info() {
  $bundles = array();
107
  foreach (config_get_storage_names_with_prefix('contact.category.') as $config_name) {
108
    $config = \Drupal::config($config_name);
109
    $bundles['contact_message'][$config->get('id')]['label'] = $config->get('label');
110
  }
111
  return $bundles;
112 113 114 115 116 117 118
}

/**
 * Implements hook_field_extra_fields().
 */
function contact_field_extra_fields() {
  $fields = array();
119
  foreach (array_keys(entity_get_bundles('contact_message')) as $bundle) {
120 121 122 123 124 125 126 127 128 129
    $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,
    );
130 131 132 133 134 135 136
    if ($bundle == 'personal') {
      $fields['contact_message'][$bundle]['form']['recipient'] = array(
        'label' => t('Recipient user name'),
        'description' => t('User'),
        'weight' => -30,
      );
    }
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    $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,
    );
  }
159 160 161 162 163 164

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

168
/**
169
 * Loads a contact category.
170
 *
171 172
 * @param $id
 *   The ID of the contact category to load.
173
 *
174
 * @return \Drupal\contact\Entity\Category|null
175
 *   A Category object or NULL if the requested $id does not exist.
176
 */
177 178 179 180
function contact_category_load($id) {
  return entity_load('contact_category', $id);
}

181
/**
182
 * Implements hook_mail().
183 184
 */
function contact_mail($key, &$message, $params) {
185 186
  $contact_message = $params['contact_message'];
  $sender = $params['sender'];
187
  $language = language_load($message['langcode']);
188

189
  $variables = array(
190
    '!site-name' => \Drupal::config('system.site')->get('name'),
191 192
    '!subject' => $contact_message->getSubject(),
    '!category' => !empty($params['contact_category']) ? $params['contact_category']->label() : NULL,
193
    '!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)),
194
    '!sender-name' => user_format_name($sender),
195
  );
196
  if ($sender->isAuthenticated()) {
197 198 199 200
    $sender_uri = $sender->uri();
    $variables['!sender-url'] = url($sender_uri['path'], array('absolute' => TRUE, 'language' => $language) + $sender_uri['options']);
  }
  else {
201
    $variables['!sender-url'] = $params['sender']->getEmail();
202 203
  }

204
  $options = array('langcode' => $language->id);
205

206 207 208
  switch ($key) {
    case 'page_mail':
    case 'page_copy':
209 210
      $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);
211
      $build = entity_view($contact_message, 'mail', $language->id);
212
      $message['body'][] = drupal_render($build);
213
      break;
214

215
    case 'page_autoreply':
216 217
      $message['subject'] .= t('[!category] !subject', $variables, $options);
      $message['body'][] = $params['contact_category']->reply;
218
      break;
219

220 221
    case 'user_mail':
    case 'user_copy':
222
      $variables += array(
223
        '!recipient-name' => user_format_name($params['recipient']),
224
        '!recipient-edit-url' => url('user/' . $params['recipient']->id() . '/edit', array('absolute' => TRUE, 'language' => $language)),
225
      );
226 227 228 229
      $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);
230
      $build = entity_view($contact_message, 'mail', $language->id);
231
      $message['body'][] = drupal_render($build);
232 233
      break;
  }
234
}
235

236
/**
237
 * Implements hook_form_FORM_ID_alter().
238 239
 *
 * Add the enable personal contact form to an individual user's account page.
240
 *
241
 * @see \Drupal\user\ProfileFormController::form()
242
 */
243
function contact_form_user_form_alter(&$form, &$form_state) {
244
  $form['contact'] = array(
245
    '#type' => 'details',
246 247 248
    '#title' => t('Contact settings'),
    '#weight' => 5,
  );
249
  $account = $form_state['controller']->getEntity();
250
  $account_data = !user_is_anonymous() ? \Drupal::service('user.data')->get('contact', $account->id(), 'enabled') : NULL;
251 252 253
  $form['contact']['contact'] = array(
    '#type' => 'checkbox',
    '#title' => t('Personal contact form'),
254
    '#default_value' => isset($account_data) ? $account_data : \Drupal::config('contact.settings')->get('user_default_enabled'),
255
    '#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.'),
256
  );
257
  $form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit';
258 259
}

260
/**
261
 * Submit callback for the user profile form to save the contact page setting.
262
 */
263 264 265
function contact_user_profile_form_submit($form, &$form_state) {
  $account = $form_state['controller']->getEntity();
  if ($account->id() && isset($form_state['values']['contact'])) {
266
    \Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state['values']['contact']);
267
  }
268 269
}

270
/**
271
 * Implements hook_form_FORM_ID_alter().
272 273
 *
 * Add the default personal contact setting on the user settings page.
274 275
 *
 * @see user_admin_settings()
276
 */
277
function contact_form_user_admin_settings_alter(&$form, &$form_state) {
278
  $form['contact'] = array(
279
    '#type' => 'details',
280
    '#title' => t('Contact settings'),
281 282 283 284 285
    '#weight' => 0,
  );
  $form['contact']['contact_default_status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable the personal contact form by default for new users.'),
286
    '#description' => t('Changing this setting will not affect existing users.'),
287
    '#default_value' => \Drupal::config('contact.settings')->get('user_default_enabled'),
288
  );
289 290 291 292 293 294 295 296 297 298
  // 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) {
299
  \Drupal::config('contact.settings')
300 301
    ->set('user_default_enabled', $form_state['values']['contact_default_status'])
    ->save();
302
}