You are here

function simple_ldap_user_form_user_form_alter in Simple LDAP 8

Implements hook_form_FORM_ID_alter().

File

modules/simple_ldap_user/simple_ldap_user.module, line 28

Code

function simple_ldap_user_form_user_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $server = \Drupal::service('simple_ldap.server');

  /** @var \Drupal\user\UserInterface $user */
  $user = $form_state
    ->getFormObject()
    ->getEntity();

  // Lock down the fields if applicable
  $readonly = FALSE;
  if ($server
    ->isReadOnly() && !$user
    ->isNew() && $user
    ->id() != 1) {
    \Drupal::messenger()
      ->addWarning(t('Some fields have been marked <em>read-only</em>, as they are controlled by the LDAP server.'));
    $readonly = TRUE;
  }

  // @TODO add server-specific conditions for readonly fields. Active Directory will have some requirements.
  $form['account']['name']['#disabled'] = $readonly;
  $form['account']['mail']['#disabled'] = $readonly;
  $form['account']['pass']['#disabled'] = $readonly;
  $form['account']['status']['#disabled'] = $readonly;

  // @TODO add other attributes from attribute mapping.
  array_unshift($form['#validate'], 'simple_ldap_user_profile_validate');
}