contact.module 10.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
use Drupal\Core\Form\FormStateInterface;
9 10
use Drupal\Core\Routing\RouteMatchInterface;

Dries's avatar
 
Dries committed
11
/**
12
 * Implements hook_help().
Dries's avatar
 
Dries committed
13
 */
14
function contact_help($route_name, RouteMatchInterface $route_match) {
15 16
  switch ($route_name) {
    case 'help.page.contact':
17 18
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
19
      $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>';
20 21 22
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('User contact forms') . '</dt>';
23
      $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>';
24
      $output .= '<dt>' . t('Site-wide contact forms') . '</dt>';
25
      $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' => url('contact'))) . '</p>';
26
      $output .= '<dt>' . t('Navigation') . '</dt>';
27
      $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>';
28 29
      $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>';
30
      $output .= '</dl>';
31
      return $output;
32

33 34
    case 'contact.form_list':
      $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' => url('contact'))) . '</p>';
35
      $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>';
36
      $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>';
37
      return $output;
Dries's avatar
 
Dries committed
38 39 40
  }
}

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

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

66
/**
67
 * Implements hook_entity_extra_field_info().
68
 */
69
function contact_entity_extra_field_info() {
70
  $fields = array();
71
  foreach (array_keys(entity_get_bundles('contact_message')) as $bundle) {
72 73 74 75 76 77
    $fields['contact_message'][$bundle]['form']['name'] = array(
      'label' => t('Sender name'),
      'description' => t('Text'),
      'weight' => -50,
    );
    $fields['contact_message'][$bundle]['form']['mail'] = array(
78 79
      'label' => t('Sender email'),
      'description' => t('Email'),
80 81
      'weight' => -40,
    );
82 83 84 85 86 87 88
    if ($bundle == 'personal') {
      $fields['contact_message'][$bundle]['form']['recipient'] = array(
        'label' => t('Recipient user name'),
        'description' => t('User'),
        'weight' => -30,
      );
    }
89 90 91 92 93 94
    $fields['contact_message'][$bundle]['form']['copy'] = array(
      'label' => t('Send copy to sender'),
      'description' => t('Option'),
      'weight' => 50,
    );
  }
95 96 97 98 99 100

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

104
/**
105
 * Implements hook_mail().
106 107
 */
function contact_mail($key, &$message, $params) {
108
  $contact_message = $params['contact_message'];
109
  /** @var $sender \Drupal\user\UserInterface */
110
  $sender = $params['sender'];
111
  $language = language_load($message['langcode']);
112

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

127
  $options = array('langcode' => $language->id);
128

129 130 131
  switch ($key) {
    case 'page_mail':
    case 'page_copy':
132
      $message['subject'] .= t('[!form] !subject', $variables, $options);
133
      $message['body'][] = t("!sender-name (!sender-url) sent a message using the contact form at !form-url.", $variables, $options);
134
      $build = entity_view($contact_message, 'mail', $language->id);
135
      $message['body'][] = drupal_render($build);
136
      break;
137

138
    case 'page_autoreply':
139 140
      $message['subject'] .= t('[!form] !subject', $variables, $options);
      $message['body'][] = $params['contact_form']->reply;
141
      break;
142

143 144
    case 'user_mail':
    case 'user_copy':
145
      $variables += array(
146
        '!recipient-name' => user_format_name($params['recipient']),
147
        '!recipient-edit-url' => url('user/' . $params['recipient']->id() . '/edit', array('absolute' => TRUE, 'language' => $language)),
148
      );
149 150 151
      $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);
152
      $message['body'][] = t("If you don't want to receive such emails, you can change your settings at !recipient-edit-url.", $variables, $options);
153
      $build = entity_view($contact_message, 'mail', $language->id);
154
      $message['body'][] = drupal_render($build);
155 156
      break;
  }
157
}
158

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

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

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