' . t('About') . ''; $output .= '
' . 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 Contact module.', array('@contact' => 'http://drupal.org/documentation/modules/contact')) . '
'; $output .= '' . t('Add one or more categories on this page to set up your site-wide contact form.', array('@form' => url('contact'))) . '
'; $output .= '' . t('A Contact menu item is added to the Footer menu, which you can modify on the Menus administration page.', array('@menu-settings' => url('admin/structure/menu'))) . '
'; $output .= '' . 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 Blocks administration page.', array('@blocks' => url('admin/structure/block'))) . '
'; return $output; } } /** * Implements hook_permission(). */ function contact_permission() { return array( 'administer contact forms' => array( 'title' => t('Administer contact forms and contact form settings'), ), 'access site-wide contact form' => array( 'title' => t('Use the site-wide contact form'), ), 'access user contact forms' => array( 'title' => t("Use users' personal contact forms"), ), ); } /** * Implements hook_menu(). */ function contact_menu() { $items['admin/structure/contact'] = array( 'title' => 'Contact form categories', 'description' => 'Create a system contact form and set up categories for the form to use.', 'route_name' => 'contact_category_list', ); $items['admin/structure/contact/add'] = array( 'title' => 'Add category', 'route_name' => 'contact_category_add', 'type' => MENU_LOCAL_ACTION, 'weight' => 1, ); $items['admin/structure/contact/manage/%contact_category'] = array( 'title' => 'Edit contact category', 'route_name' => 'contact_category_edit', ); $items['admin/structure/contact/manage/%contact_category/edit'] = array( 'title' => 'Edit', 'type' => MENU_DEFAULT_LOCAL_TASK, ); $items['admin/structure/contact/manage/%contact_category/delete'] = array( 'title' => 'Delete', 'route_name' => 'contact_category_delete', 'type' => MENU_LOCAL_TASK, 'weight' => 10, ); $items['contact'] = array( 'title' => 'Contact', 'page callback' => 'contact_site_page', 'access arguments' => array('access site-wide contact form'), 'menu_name' => 'footer', 'type' => MENU_SUGGESTED_ITEM, 'file' => 'contact.pages.inc', ); $items['contact/%contact_category'] = array( 'title' => 'Contact category form', 'title callback' => 'entity_page_label', 'title arguments' => array(1), 'page callback' => 'contact_site_page', 'page arguments' => array(1), 'access arguments' => array('access site-wide contact form'), 'type' => MENU_VISIBLE_IN_BREADCRUMB, 'file' => 'contact.pages.inc', ); $items['user/%user/contact'] = array( 'title' => 'Contact', 'page callback' => 'contact_personal_page', 'page arguments' => array(1), 'type' => MENU_LOCAL_TASK, 'access callback' => '_contact_personal_tab_access', 'access arguments' => array(1), 'weight' => 2, 'file' => 'contact.pages.inc', ); return $items; } /** * Access callback: Checks access for a user's personal contact form. * * @param $account * The user object of the user whose contact form is being requested. * * @see contact_menu() */ function _contact_personal_tab_access($account) { global $user; // Anonymous users cannot have contact forms. if (!$account->uid) { return FALSE; } // Users may not contact themselves. if ($user->uid == $account->uid) { return FALSE; } // User administrators should always have access to personal contact forms. if (user_access('administer users')) { return TRUE; } // If requested user has been blocked, do not allow users to contact them. if (empty($account->status)) { return FALSE; } // If the requested user has disabled their contact form, do not allow users // to contact them. $account_data = Drupal::service('user.data')->get('contact', $account->id(), 'enabled'); if (isset($account_data) && empty($account_data)) { return FALSE; } // If the requested user did not save a preference yet, deny access if the // configured default is disabled. elseif (!config('contact.settings')->get('user_default_enabled')) { return FALSE; } return user_access('access user contact forms'); } /** * Implements hook_entity_bundle_info(). */ function contact_entity_bundle_info() { $bundles = array(); foreach (config_get_storage_names_with_prefix('contact.category.') as $config_name) { $config = config($config_name); $bundles['contact_message'][$config->get('id')]['label'] = $config->get('label'); } return $bundles; } /** * Implements hook_field_extra_fields(). */ function contact_field_extra_fields() { $fields = array(); foreach (array_keys(entity_get_bundles('contact_message')) as $bundle) { $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, ); // @todo Recipient only makes sense if user contact form is a bundle/category. $fields['contact_message'][$bundle]['form']['recipient'] = array( 'label' => t('Recipient user name'), 'description' => t('User'), 'weight' => -30, ); $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, ); } $fields['user']['user']['form']['contact'] = array( 'label' => t('Contact settings'), 'description' => t('Contact module form element.'), 'weight' => 5, ); return $fields; } /** * Loads a contact category. * * @param $id * The ID of the contact category to load. * * @return Drupal\contact\Plugin\Core\Entity\Category|false * A Category object or FALSE if the requested $id does not exist. */ function contact_category_load($id) { return entity_load('contact_category', $id); } /** * Entity URI callback. * * @param Drupal\contact\Plugin\Core\Entity\Category $category * A contact category entity. * * @return array * An array with 'path' as the key and the path to the category as the value. */ function contact_category_uri(Category $category) { return array( 'path' => 'admin/structure/contact/manage/' . $category->id(), ); } /** * Implements hook_mail(). */ function contact_mail($key, &$message, $params) { $contact_message = $params['contact_message']; $sender = $params['sender']; $language = language_load($message['langcode']); $variables = array( '!site-name' => config('system.site')->get('name'), '!subject' => $contact_message->subject, '!category' => isset($params['contact_category']) ? $params['contact_category']->label() : NULL, '!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)), '!sender-name' => user_format_name($sender), ); if (!empty($sender->uid)) { $sender_uri = $sender->uri(); $variables['!sender-url'] = url($sender_uri['path'], array('absolute' => TRUE, 'language' => $language) + $sender_uri['options']); } else { $variables['!sender-url'] = $params['sender']->mail; } $options = array('langcode' => $language->langcode); switch ($key) { case 'page_mail': case 'page_copy': $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); $build = entity_view($contact_message, 'mail', $language->langcode); $message['body'][] = drupal_render($build); break; case 'page_autoreply': $message['subject'] .= t('[!category] !subject', $variables, $options); $message['body'][] = $params['contact_category']->reply; break; case 'user_mail': case 'user_copy': $variables += array( '!recipient-name' => user_format_name($params['recipient']), '!recipient-edit-url' => url('user/' . $params['recipient']->uid . '/edit', array('absolute' => TRUE, 'language' => $language)), ); $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); $message['body'][] = t('Message:', array(), $options); $message['body'][] = $contact_message->message; break; } } /** * Implements hook_form_FORM_ID_alter(). * * Add the enable personal contact form to an individual user's account page. * * @see user_profile_form() */ function contact_form_user_profile_form_alter(&$form, &$form_state) { $form['contact'] = array( '#type' => 'details', '#title' => t('Contact settings'), '#weight' => 5, ); $account = $form_state['controller']->getEntity(); $account_data = Drupal::service('user.data')->get('contact', $account->id(), 'enabled'); $form['contact']['contact'] = array( '#type' => 'checkbox', '#title' => t('Personal contact form'), '#default_value' => isset($account_data) ? $account_data : config('contact.settings')->get('user_default_enabled'), '#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.'), ); } /** * Implements hook_user_update(). */ function contact_user_update($account) { if (isset($account->contact)) { Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $account->contact); } } /** * Implements hook_form_FORM_ID_alter(). * * Add the default personal contact setting on the user settings page. * * @see user_admin_settings() */ function contact_form_user_admin_settings_alter(&$form, &$form_state) { $form['contact'] = array( '#type' => 'details', '#title' => t('Contact settings'), '#weight' => 0, ); $form['contact']['contact_default_status'] = array( '#type' => 'checkbox', '#title' => t('Enable the personal contact form by default for new users.'), '#description' => t('Changing this setting will not affect existing users.'), '#default_value' => config('contact.settings')->get('user_default_enabled'), ); // 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) { config('contact.settings') ->set('user_default_enabled', $form_state['values']['contact_default_status']) ->save(); }