You are here

function _acl_edit_form_after_build in ACL 8

Same name and namespace in other branches
  1. 6 acl.admin.inc \_acl_edit_form_after_build()
  2. 7 acl.admin.inc \_acl_edit_form_after_build()

Process a form that had our buttons on it.

1 string reference to '_acl_edit_form_after_build'
_acl_edit_form in ./acl.admin.inc
Implementation of acl_edit_form().

File

./acl.admin.inc, line 99
Implementations of administration functions for the acl module.

Code

function _acl_edit_form_after_build($form, FormStateInterface $form_state) {

  // We can't use the form values because it's the entire structure
  // and we have no clue where our values actually are. That's
  // ok tho cause #value still works for us.
  $user_list = acl_edit_form_get_user_list($form);
  $button_name = 'acl_' . $form['acl_id']['#value'];
  $triggering_element = $form_state
    ->getTriggeringElement();
  if (!empty($triggering_element) && $triggering_element['#value'] == $form['delete_button']['#value']) {
    $deletions = $form['deletions']['#value'];
    foreach ($deletions as $uid) {
      unset($user_list[$uid]);
      unset($form['deletions']['#value'][$uid]);
    }
  }
  elseif (!empty($triggering_element['#value']) && $triggering_element['#value'] == $form['add_button']['#value'] && !empty($form['add']['#value'])) {
    $value = $form['add']['#value'];
    $match = EntityAutocomplete::extractEntityIdFromAutocompleteInput($value);
    if ($match === NULL) {
      $user = \Drupal::database()
        ->query("SELECT u.uid, ud.name FROM {users} u INNER JOIN {users_field_data} ud ON u.uid = ud.uid WHERE ud.name = :name", [
        'name' => $value,
      ])
        ->fetchObject();
    }
    else {
      $user = \Drupal::database()
        ->query("SELECT u.uid, ud.name FROM {users} u INNER JOIN {users_field_data} ud ON u.uid = ud.uid WHERE u.uid = :id", [
        'id' => $match,
      ])
        ->fetchObject();
    }
    if (!$user) {
      $form_state
        ->setError($form['add'], t("Invalid user specified."));
    }
    else {
      $user_list[$user->uid] = $user->name;
      $form['add']['#value'] = NULL;
    }
  }
  if (count($user_list) != 0) {
    $form['deletions']['#type'] = 'checkboxes';
    $form['deletions']['#title'] = t("Current users");
    $form['deletions']['#options'] = $user_list;
    $form['deletions']['#value'] = array();

    // don't carry value through.
    $form['deletions'] = \Drupal::formBuilder()
      ->doBuildForm(!empty($form['#post']) ? $form['#post']['form_id'] : 'acl_form', $form['deletions'], $form_state);
  }
  else {
    $form['delete_button']['#type'] = 'value';
  }
  $form['user_list']['#value'] = serialize($user_list);
  return $form;
}