function ContactHooks::formUserFormAlter

Implements hook_form_FORM_ID_alter() for \Drupal\user\ProfileForm.

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

See also

\Drupal\user\ProfileForm::form()

File

core/modules/contact/src/Hook/ContactHooks.php, line 178

Class

ContactHooks
Hook implementations for contact.

Namespace

Drupal\contact\Hook

Code

public function formUserFormAlter(&$form, FormStateInterface $form_state) : void {
    $form['contact'] = [
        '#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'] = [
        '#type' => 'checkbox',
        '#title' => t('Personal contact form'),
        '#default_value' => $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';
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.