2004-12-07 17:03:46 +00:00
<?php
/**
* @file
2006-01-14 10:33:22 +00:00
* Enables the use of personal and site-wide contact forms.
2004-12-07 17:03:46 +00:00
*/
2013-08-18 21:16:19 +00:00
use Drupal\contact\Entity\Category;
2013-07-24 19:40:03 +00:00
use Drupal\user\UserInterface;
2012-10-18 08:55:55 +00:00
2004-12-07 17:03:46 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_help().
2004-12-07 17:03:46 +00:00
*/
2007-06-30 19:46:58 +00:00
function contact_help($path, $arg) {
switch ($path) {
2005-11-01 10:17:34 +00:00
case 'admin/help#contact':
2009-11-29 19:54:40 +00:00
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
2012-03-21 05:51:30 +00:00
$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>';
2009-11-29 19:54:40 +00:00
$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>';
2010-01-14 05:51:59 +00:00
$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>';
2009-11-29 19:54:40 +00:00
$output .= '<dt>' . t('Navigation') . '</dt>';
2013-01-02 12:00:25 +00:00
$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>';
2010-01-14 05:51:59 +00:00
$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>';
2009-11-29 19:54:40 +00:00
$output .= '</dl>';
2005-11-01 10:17:34 +00:00
return $output;
2009-07-20 18:51:36 +00:00
case 'admin/structure/contact':
2010-01-14 05:51:59 +00:00
$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>';
2013-01-02 12:00:25 +00:00
$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>';
2010-01-14 05:51:59 +00:00
$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>';
2007-04-13 08:56:59 +00:00
return $output;
2004-12-07 17:03:46 +00:00
}
}
2006-05-12 18:12:21 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_permission().
2006-05-12 18:12:21 +00:00
*/
2009-07-05 18:00:11 +00:00
function contact_permission() {
2008-02-20 13:46:43 +00:00
return array(
2009-10-09 00:54:33 +00:00
'administer contact forms' => array(
2009-12-01 13:14:43 +00:00
'title' => t('Administer contact forms and contact form settings'),
2008-10-09 15:15:55 +00:00
),
'access site-wide contact form' => array(
2009-12-01 13:14:43 +00:00
'title' => t('Use the site-wide contact form'),
2008-10-09 15:15:55 +00:00
),
2009-10-11 18:34:10 +00:00
'access user contact forms' => array(
2009-12-01 13:14:43 +00:00
'title' => t("Use users' personal contact forms"),
2009-10-11 18:34:10 +00:00
),
2008-02-20 13:46:43 +00:00
);
2006-05-12 18:12:21 +00:00
}
2008-02-20 13:46:43 +00:00
2004-12-07 17:03:46 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_menu().
2004-12-07 17:03:46 +00:00
*/
2007-01-24 14:48:36 +00:00
function contact_menu() {
2009-07-20 18:51:36 +00:00
$items['admin/structure/contact'] = array(
2013-05-26 20:00:21 +00:00
'title' => 'Contact form categories',
2007-04-30 17:03:29 +00:00
'description' => 'Create a system contact form and set up categories for the form to use.',
2013-09-15 19:59:49 +00:00
'route_name' => 'contact.category_list',
2007-01-24 14:48:36 +00:00
);
2012-11-06 14:03:05 +00:00
$items['admin/structure/contact/manage/%contact_category'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Edit contact category',
2013-09-15 19:59:49 +00:00
'route_name' => 'contact.category_edit',
2012-11-06 14:03:05 +00:00
);
2012-12-14 16:16:41 +00:00
2007-01-24 14:48:36 +00:00
$items['contact'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Contact',
2013-09-18 18:30:30 +00:00
'route_name' => 'contact.site_page',
2012-11-20 11:36:51 +00:00
'menu_name' => 'footer',
2007-01-24 14:48:36 +00:00
'type' => MENU_SUGGESTED_ITEM,
);
2012-12-14 16:16:41 +00:00
$items['contact/%contact_category'] = array(
'title' => 'Contact category form',
'title callback' => 'entity_page_label',
'title arguments' => array(1),
2013-09-18 18:30:30 +00:00
'route_name' => 'contact.site_page_category',
2012-12-14 16:16:41 +00:00
'type' => MENU_VISIBLE_IN_BREADCRUMB,
);
2007-02-11 09:30:51 +00:00
$items['user/%user/contact'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Contact',
2013-09-18 18:30:30 +00:00
'route_name' => 'contact.personal_page',
2007-01-24 14:48:36 +00:00
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
2004-12-07 17:03:46 +00:00
return $items;
}
2007-07-16 06:37:49 +00:00
/**
2011-12-05 14:23:20 +00:00
* Access callback: Checks access for a user's personal contact form.
*
2009-10-10 19:54:15 +00:00
* @param $account
2011-12-05 14:23:20 +00:00
* The user object of the user whose contact form is being requested.
*
* @see contact_menu()
2007-07-16 06:37:49 +00:00
*/
2013-07-24 19:40:03 +00:00
function _contact_personal_tab_access(UserInterface $account) {
2013-10-30 17:31:44 +00:00
return \Drupal::service('access_manager')->checkNamedRoute('contact.personal_page', array('user' => $account->id()), \Drupal::currentUser());
2007-01-24 14:48:36 +00:00
}
2012-12-14 16:16:41 +00:00
/**
2013-01-23 17:46:47 +00:00
* Implements hook_entity_bundle_info().
2012-12-14 16:16:41 +00:00
*/
2013-01-23 17:46:47 +00:00
function contact_entity_bundle_info() {
$bundles = array();
2012-12-14 16:16:41 +00:00
foreach (config_get_storage_names_with_prefix('contact.category.') as $config_name) {
2013-09-16 03:58:06 +00:00
$config = \Drupal::config($config_name);
2013-05-05 00:24:24 +00:00
$bundles['contact_message'][$config->get('id')]['label'] = $config->get('label');
2012-12-14 16:16:41 +00:00
}
2013-01-23 17:46:47 +00:00
return $bundles;
2012-12-14 16:16:41 +00:00
}
/**
* Implements hook_field_extra_fields().
*/
function contact_field_extra_fields() {
$fields = array();
2013-01-23 17:46:47 +00:00
foreach (array_keys(entity_get_bundles('contact_message')) as $bundle) {
2012-12-14 16:16:41 +00:00
$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,
);
2013-06-18 23:42:10 +00:00
if ($bundle == 'personal') {
$fields['contact_message'][$bundle]['form']['recipient'] = array(
'label' => t('Recipient user name'),
'description' => t('User'),
'weight' => -30,
);
}
2012-12-14 16:16:41 +00:00
$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,
);
}
Issue #967566 by Letharion, mariusz.slonina, indytechcook, adnasa, joestewart, oriol_e9g, Fabianx, tim.plunkett, valthebald, tsvenson, rbayliss, sun, xjm | DocuAnt: Several important Core User settings need to implement hook_field_extra_fields().
2013-02-18 21:21:42 +00:00
$fields['user']['user']['form']['contact'] = array(
'label' => t('Contact settings'),
'description' => t('Contact module form element.'),
'weight' => 5,
);
2012-12-14 16:16:41 +00:00
return $fields;
}
2007-07-16 06:37:49 +00:00
/**
2011-12-05 14:23:20 +00:00
* Loads a contact category.
2009-10-09 02:34:07 +00:00
*
2012-10-18 08:55:55 +00:00
* @param $id
* The ID of the contact category to load.
2011-12-05 14:23:20 +00:00
*
2013-08-18 21:16:19 +00:00
* @return \Drupal\contact\Entity\Category|null
2013-07-01 00:09:20 +00:00
* A Category object or NULL if the requested $id does not exist.
2007-07-16 06:37:49 +00:00
*/
2012-10-18 08:55:55 +00:00
function contact_category_load($id) {
return entity_load('contact_category', $id);
}
2007-07-01 19:49:19 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_mail().
2007-07-01 19:49:19 +00:00
*/
function contact_mail($key, &$message, $params) {
2012-12-14 16:16:41 +00:00
$contact_message = $params['contact_message'];
$sender = $params['sender'];
2012-09-13 08:48:24 +00:00
$language = language_load($message['langcode']);
2012-12-14 16:16:41 +00:00
2009-10-09 02:34:07 +00:00
$variables = array(
2013-09-16 03:58:06 +00:00
'!site-name' => \Drupal::config('system.site')->get('name'),
2013-06-18 23:42:10 +00:00
'!subject' => $contact_message->getSubject(),
'!category' => !empty($params['contact_category']) ? $params['contact_category']->label() : NULL,
2012-04-29 15:16:27 +00:00
'!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)),
2012-12-14 16:16:41 +00:00
'!sender-name' => user_format_name($sender),
2009-10-09 02:34:07 +00:00
);
2013-07-11 17:29:02 +00:00
if ($sender->isAuthenticated()) {
2012-12-14 16:16:41 +00:00
$sender_uri = $sender->uri();
$variables['!sender-url'] = url($sender_uri['path'], array('absolute' => TRUE, 'language' => $language) + $sender_uri['options']);
}
else {
2013-07-24 19:40:03 +00:00
$variables['!sender-url'] = $params['sender']->getEmail();
2012-12-14 16:16:41 +00:00
}
2013-06-29 10:56:53 +00:00
$options = array('langcode' => $language->id);
2009-10-09 02:34:07 +00:00
2007-07-01 19:49:19 +00:00
switch ($key) {
case 'page_mail':
case 'page_copy':
2012-12-14 16:16:41 +00:00
$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);
2013-06-29 10:56:53 +00:00
$build = entity_view($contact_message, 'mail', $language->id);
2012-12-14 16:16:41 +00:00
$message['body'][] = drupal_render($build);
2007-07-01 19:49:19 +00:00
break;
2009-10-09 02:34:07 +00:00
2007-07-01 19:49:19 +00:00
case 'page_autoreply':
2012-12-14 16:16:41 +00:00
$message['subject'] .= t('[!category] !subject', $variables, $options);
$message['body'][] = $params['contact_category']->reply;
2007-07-01 19:49:19 +00:00
break;
2009-10-09 02:34:07 +00:00
2007-07-01 19:49:19 +00:00
case 'user_mail':
case 'user_copy':
2009-10-09 02:34:07 +00:00
$variables += array(
2012-01-16 02:18:26 +00:00
'!recipient-name' => user_format_name($params['recipient']),
2013-07-11 17:29:02 +00:00
'!recipient-edit-url' => url('user/' . $params['recipient']->id() . '/edit', array('absolute' => TRUE, 'language' => $language)),
2009-10-09 02:34:07 +00:00
);
2012-12-14 16:16:41 +00:00
$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);
2013-06-29 10:56:53 +00:00
$build = entity_view($contact_message, 'mail', $language->id);
2013-06-18 23:42:10 +00:00
$message['body'][] = drupal_render($build);
2007-07-01 19:49:19 +00:00
break;
}
2005-10-07 06:48:33 +00:00
}
2009-09-26 00:13:19 +00:00
2009-10-09 02:34:07 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_form_FORM_ID_alter().
2009-10-09 02:34:07 +00:00
*
* Add the enable personal contact form to an individual user's account page.
2011-12-05 14:23:20 +00:00
*
2013-06-27 21:24:39 +00:00
* @see \Drupal\user\ProfileFormController::form()
2009-10-09 02:34:07 +00:00
*/
2013-06-27 21:24:39 +00:00
function contact_form_user_form_alter(&$form, &$form_state) {
2011-11-03 07:34:18 +00:00
$form['contact'] = array(
2012-11-27 07:06:47 +00:00
'#type' => 'details',
2011-11-03 07:34:18 +00:00
'#title' => t('Contact settings'),
'#weight' => 5,
);
2013-04-29 23:46:14 +00:00
$account = $form_state['controller']->getEntity();
2013-09-16 03:58:06 +00:00
$account_data = !user_is_anonymous() ? \Drupal::service('user.data')->get('contact', $account->id(), 'enabled') : NULL;
2011-11-03 07:34:18 +00:00
$form['contact']['contact'] = array(
'#type' => 'checkbox',
'#title' => t('Personal contact form'),
2013-09-16 03:58:06 +00:00
'#default_value' => isset($account_data) ? $account_data : \Drupal::config('contact.settings')->get('user_default_enabled'),
2012-11-19 11:49:04 +00:00
'#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.'),
2011-11-03 07:34:18 +00:00
);
2013-07-29 07:52:14 +00:00
$form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit';
2009-10-09 02:34:07 +00:00
}
2010-03-20 14:55:06 +00:00
/**
2013-07-29 07:52:14 +00:00
* Submit callback for the user profile form to save the contact page setting.
2010-03-20 14:55:06 +00:00
*/
2013-07-29 07:52:14 +00:00
function contact_user_profile_form_submit($form, &$form_state) {
$account = $form_state['controller']->getEntity();
if ($account->id() && isset($form_state['values']['contact'])) {
2013-09-16 03:58:06 +00:00
\Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state['values']['contact']);
2012-11-27 22:26:22 +00:00
}
2010-03-20 14:55:06 +00:00
}
2009-09-26 00:13:19 +00:00
/**
2010-11-17 03:58:30 +00:00
* Implements hook_form_FORM_ID_alter().
2009-10-09 02:34:07 +00:00
*
* Add the default personal contact setting on the user settings page.
2011-12-05 14:23:20 +00:00
*
* @see user_admin_settings()
2009-09-26 00:13:19 +00:00
*/
2009-10-09 02:34:07 +00:00
function contact_form_user_admin_settings_alter(&$form, &$form_state) {
2009-09-26 00:13:19 +00:00
$form['contact'] = array(
2012-11-27 07:06:47 +00:00
'#type' => 'details',
2009-10-09 02:34:07 +00:00
'#title' => t('Contact settings'),
2009-09-26 00:13:19 +00:00
'#weight' => 0,
);
$form['contact']['contact_default_status'] = array(
'#type' => 'checkbox',
'#title' => t('Enable the personal contact form by default for new users.'),
2009-10-09 02:34:07 +00:00
'#description' => t('Changing this setting will not affect existing users.'),
2013-09-16 03:58:06 +00:00
'#default_value' => \Drupal::config('contact.settings')->get('user_default_enabled'),
2009-09-26 00:13:19 +00:00
);
2012-08-01 02:20:36 +00:00
// 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) {
2013-09-16 03:58:06 +00:00
\Drupal::config('contact.settings')
2012-08-01 02:20:36 +00:00
->set('user_default_enabled', $form_state['values']['contact_default_status'])
->save();
2009-09-26 00:13:19 +00:00
}