W3cubDocs

/Drupal 8

function contact_form_user_form_alter

contact_form_user_form_alter(&$form, FormStateInterface $form_state)

Implements hook_form_FORM_ID_alter().

Add the enable personal contact form to an individual user's account page.

See also

\Drupal\user\ProfileForm::form()

File

core/modules/contact/contact.module, line 169
Enables the use of personal and site-wide contact forms.

Code

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->getFormObject()->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';
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!contact!contact.module/function/contact_form_user_form_alter/8.1.x