contact.module 9.89 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_entity_type_alter().
43
 */
44 45
function contact_entity_type_alter(array &$entity_types) {
  /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
46
  $entity_types['user']->setLinkTemplate('contact-form', 'entity.user.contact_form');
47 48
}

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

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

87
/**
88
 * Implements hook_mail().
89 90
 */
function contact_mail($key, &$message, $params) {
91
  $contact_message = $params['contact_message'];
92
  /** @var $sender \Drupal\user\UserInterface */
93
  $sender = $params['sender'];
94
  $language = language_load($message['langcode']);
95

96
  $variables = array(
97
    '!site-name' => \Drupal::config('system.site')->get('name'),
98
    '!subject' => $contact_message->getSubject(),
99
    '!form' => !empty($params['contact_form']) ? $params['contact_form']->label() : NULL,
100
    '!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)),
101
    '!sender-name' => user_format_name($sender),
102
  );
103
  if ($sender->isAuthenticated()) {
104
    $variables['!sender-url'] = $sender->url('canonical', array('absolute' => TRUE, 'language' => $language));
105 106
  }
  else {
107
    $variables['!sender-url'] = $params['sender']->getEmail();
108 109
  }

110
  $options = array('langcode' => $language->id);
111

112 113 114
  switch ($key) {
    case 'page_mail':
    case 'page_copy':
115
      $message['subject'] .= t('[!form] !subject', $variables, $options);
116
      $message['body'][] = t("!sender-name (!sender-url) sent a message using the contact form at !form-url.", $variables, $options);
117
      $build = entity_view($contact_message, 'mail', $language->id);
118
      $message['body'][] = drupal_render($build);
119
      break;
120

121
    case 'page_autoreply':
122
      $message['subject'] .= t('[!form] !subject', $variables, $options);
123
      $message['body'][] = $params['contact_form']->getReply();
124
      break;
125

126 127
    case 'user_mail':
    case 'user_copy':
128
      $variables += array(
129
        '!recipient-name' => user_format_name($params['recipient']),
130
        '!recipient-edit-url' => url('user/' . $params['recipient']->id() . '/edit', array('absolute' => TRUE, 'language' => $language)),
131
      );
132 133 134
      $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);
135
      $message['body'][] = t("If you don't want to receive such emails, you can change your settings at !recipient-edit-url.", $variables, $options);
136
      $build = entity_view($contact_message, 'mail', $language->id);
137
      $message['body'][] = drupal_render($build);
138 139
      break;
  }
140
}
141

142
/**
143
 * Implements hook_form_FORM_ID_alter().
144 145
 *
 * Add the enable personal contact form to an individual user's account page.
146
 *
147
 * @see \Drupal\user\ProfileForm::form()
148
 */
149
function contact_form_user_form_alter(&$form, FormStateInterface $form_state) {
150
  $form['contact'] = array(
151
    '#type' => 'details',
152
    '#title' => t('Contact settings'),
153
    '#open' => TRUE,
154 155
    '#weight' => 5,
  );
156
  $account = $form_state->getFormObject()->getEntity();
157 158 159
  if (!\Drupal::currentUser()->isAnonymous() && $account->id()) {
    $account_data = \Drupal::service('user.data')->get('contact', $account->id(), 'enabled');
  }
160 161 162
  $form['contact']['contact'] = array(
    '#type' => 'checkbox',
    '#title' => t('Personal contact form'),
163
    '#default_value' => isset($account_data) ? $account_data : \Drupal::config('contact.settings')->get('user_default_enabled'),
164
    '#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.'),
165
  );
166
  $form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit';
167 168
}

169
/**
170
 * Submit callback for the user profile form to save the contact page setting.
171
 */
172
function contact_user_profile_form_submit($form, FormStateInterface $form_state) {
173
  $account = $form_state->getFormObject()->getEntity();
174 175
  if ($account->id() && $form_state->hasValue('contact')) {
    \Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state->getValue('contact'));
176
  }
177 178
}

179
/**
180
 * Implements hook_form_FORM_ID_alter().
181 182
 *
 * Add the default personal contact setting on the user settings page.
183 184
 *
 * @see user_admin_settings()
185
 */
186
function contact_form_user_admin_settings_alter(&$form, FormStateInterface $form_state) {
187
  $form['contact'] = array(
188
    '#type' => 'details',
189
    '#title' => t('Contact settings'),
190
    '#open' => TRUE,
191 192 193 194
    '#weight' => 0,
  );
  $form['contact']['contact_default_status'] = array(
    '#type' => 'checkbox',
195
    '#title' => t('Enable the personal contact form by default for new users'),
196
    '#description' => t('Changing this setting will not affect existing users.'),
197
    '#default_value' => \Drupal::config('contact.settings')->get('user_default_enabled'),
198
  );
199 200 201 202 203 204 205 206 207
  // 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()
 */
208
function contact_form_user_admin_settings_submit($form, FormStateInterface $form_state) {
209
  \Drupal::config('contact.settings')
210
    ->set('user_default_enabled', $form_state->getValue('contact_default_status'))
211
    ->save();
212
}