You are here

function user_revision_form_user_form_alter in User Revision 8

Implements hook_form_BASE_FORM_ID_alter() for user_form.

See also

user_revision_form_user_form_builder()

File

./user_revision.module, line 121
User Revision module.

Code

function user_revision_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $config = \Drupal::config('user_revision.settings');

  /* @var $user \Drupal\user\Entity\User */
  $user = $form_state
    ->getFormObject()
    ->getEntity();
  if ($user
    ->isNew() || !isset($form['revision_log'])) {
    if (isset($form['revision_log'])) {
      unset($form['revision_log']);
    }
    return;
  }
  $form['revision'] = array(
    '#type' => 'checkbox',
    '#title' => t('Create new revision'),
    '#default_value' => $config
      ->get('user_revision_default_enabled') || $config
      ->get('user_revision_always_enabled'),
    '#access' => \Drupal::currentUser()
      ->hasPermission('administer users') && !$config
      ->get('user_revision_always_enabled'),
    '#group' => 'revision_information',
  );
  if (!\Drupal::currentUser()
    ->hasPermission('administer users') && (!$form['revision']['#default_value'] || !$config
    ->get('user_revision_user_log_enabled'))) {

    //Don't include the revision_log field if user is non-privileged, and the module

    //is configured either not to create new revisions by default,

    //or not to allow ordinary users to enter revision log messages.
    unset($form['revision_log']);
  }
  else {

    // Add a revision_information group to contain the "Create new revision" checkbox
    // and the "Revision log message" text box - but only if at least one of these
    // needs to be displayed.
    $form['revision_information'] = array(
      '#type' => 'details',
      '#title' => t('Revision information'),
      '#open' => $config
        ->get('user_revision_default_open'),
      '#attributes' => array(
        'class' => array(
          'user-form-revision-information',
        ),
      ),
      '#weight' => 20,
      '#optional' => TRUE,
    );

    //Only display the revision_log text box if the "Create new revision" option is checked
    $form['revision_log'] += array(
      '#states' => array(
        'visible' => array(
          ':input[name="revision"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
      '#group' => 'revision_information',
    );
  }

  // Define entity builder
  $form['#entity_builders'][] = 'user_revision_form_user_form_builder';
}