You are here

function private_message_form_user_form_alter in Private Message 8

Same name and namespace in other branches
  1. 8.2 private_message.module \private_message_form_user_form_alter()

Implements hook_form_FORM_ID_alter().

Add private message module specific form elements to the user form.

See also

hook_form_alter()

File

./private_message.module, line 342
Contains hooks for the private message module.

Code

function private_message_form_user_form_alter(array &$form, FormStateInterface $form_state) {
  $config = \Drupal::config('private_message.settings');

  // Get the user whose account is being modified.
  $user = $form_state
    ->getFormObject()
    ->getEntity();

  // The form elements are only added if/when email notifications have been
  // added, and the user being edited has permission to use the private message
  // system.
  if ($config
    ->get('enable_email_notifications') && $user
    ->hasPermission('use private messaging system')) {
    $form['private_messages'] = [
      '#type' => 'fieldset',
      '#title' => t('Private Messages'),
      '#tree' => TRUE,
    ];

    // User specific settings are stored and retrieved using the UserData
    // service.
    $user_data = \Drupal::service('user.data');
    $user_setting = $user_data
      ->get('private_message', $user
      ->id(), 'email_notification');

    // If the user has not set a value, the system-wide default is used.
    $default_value = is_numeric($user_setting) ? $user_setting : $config
      ->get('send_by_default');
    $form['private_messages']['email_notification'] = [
      '#type' => 'checkbox',
      '#title' => t('Email notification of private messages'),
      '#default_value' => $default_value,
    ];

    // Add a custom submit handler so the form values can be saved.
    $form['actions']['submit']['#submit'][] = 'private_message_user_form_submit';
  }
}