' . 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 email 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_entity_type_alter(). */ function contact_entity_type_alter(array &$entity_types) { /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */ $entity_types['user']->setLinkTemplate('contact-form', 'contact.personal_page'); } /** * Implements hook_entity_extra_field_info(). */ function contact_entity_extra_field_info() { $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 email'), 'description' => t('Email'), 'weight' => -40, ); if ($bundle == 'personal') { $fields['contact_message'][$bundle]['form']['recipient'] = array( 'label' => t('Recipient user name'), 'description' => t('User'), 'weight' => -30, ); } $fields['contact_message'][$bundle]['form']['copy'] = array( 'label' => t('Send copy to sender'), 'description' => t('Option'), 'weight' => 50, ); } $fields['user']['user']['form']['contact'] = array( 'label' => t('Contact settings'), 'description' => t('Contact module form element.'), 'weight' => 5, ); return $fields; } /** * Implements hook_mail(). */ function contact_mail($key, &$message, $params) { $contact_message = $params['contact_message']; /** @var $sender \Drupal\user\UserInterface */ $sender = $params['sender']; $language = language_load($message['langcode']); $variables = array( '!site-name' => \Drupal::config('system.site')->get('name'), '!subject' => $contact_message->getSubject(), '!category' => !empty($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 ($sender->isAuthenticated()) { $variables['!sender-url'] = $sender->url('canonical', array('absolute' => TRUE, 'language' => $language)); } else { $variables['!sender-url'] = $params['sender']->getEmail(); } $options = array('langcode' => $language->id); 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->id); $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']->id() . '/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 emails, you can change your settings at !recipient-edit-url.", $variables, $options); $build = entity_view($contact_message, 'mail', $language->id); $message['body'][] = drupal_render($build); break; } } /** * Implements hook_form_FORM_ID_alter(). * * Add the enable personal contact form to an individual user's account page. * * @see \Drupal\user\ProfileForm::form() */ function contact_form_user_form_alter(&$form, FormStateInterface $form_state) { $form['contact'] = array( '#type' => 'details', '#title' => t('Contact settings'), '#open' => TRUE, '#weight' => 5, ); $account = $form_state['controller']->getEntity(); if (!\Drupal::currentUser()->isAnonymous() && $account->id()) { $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 : \Drupal::config('contact.settings')->get('user_default_enabled'), '#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.'), ); $form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit'; } /** * Submit callback for the user profile form to save the contact page setting. */ function contact_user_profile_form_submit($form, FormStateInterface $form_state) { $account = $form_state['controller']->getEntity(); if ($account->id() && $form_state->hasValue('contact')) { \Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state->getValue('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, FormStateInterface $form_state) { $form['contact'] = array( '#type' => 'details', '#title' => t('Contact settings'), '#open' => TRUE, '#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' => \Drupal::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, FormStateInterface $form_state) { \Drupal::config('contact.settings') ->set('user_default_enabled', $form_state->getValue('contact_default_status')) ->save(); }