You are here

public function WorkbenchAccessByUserForm::submitForm in Workbench Access 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/WorkbenchAccessByUserForm.php, line 200

Class

WorkbenchAccessByUserForm
Configure Workbench Access per user.

Namespace

Drupal\workbench_access\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $trigger = $form_state
    ->getTriggeringElement();
  $existing_editors = $form_state
    ->getValue('existing_editors');
  $section_id = $form_state
    ->getValue('section_id');

  // Add new editors.
  if ($trigger['#name'] == 'add') {
    $uids_added = [];
    if ($add_editors = $form_state
      ->getValue('editors_add')) {
      foreach ($add_editors as $target_entity) {
        $user_id = $target_entity['target_id'];
        if (!isset($existing_editors[$user_id])) {
          $uids_added[] = $user_id;
        }
      }
    }
    elseif ($add_editors = $form_state
      ->getValue('editors_add_mass')) {
      $add_editors = preg_split('/[\\ \\n\\,]+/', $add_editors);
      foreach ($add_editors as $uid_or_username) {

        // This is a uid.
        if ((int) $uid_or_username > 0) {
          if (!isset($existing_editors[(int) $uid_or_username])) {
            $uids_added[] = $uid_or_username;
          }
        }
        elseif (strlen($uid_or_username) > 1) {
          $user = user_load_by_name(trim($uid_or_username));
          if ($user) {
            if (!isset($existing_editors[$user
              ->id()])) {
              $uids_added[] = $user
                ->id();
            }
          }
        }
      }
    }
    if (count($uids_added)) {
      $this
        ->addEditors($uids_added, $section_id, $existing_editors);
    }
    else {
      \Drupal::messenger()
        ->addMessage($this
        ->t('No valid users were selected to add'), MessengerInterface::TYPE_WARNING);
    }
  }

  // Remove unwanted editors.
  if ($trigger['#name'] == 'remove') {
    if ($remove_editors = array_filter($form_state
      ->getValue('editors_remove'))) {
      $this
        ->removeEditors($remove_editors, $section_id, $existing_editors);
    }
    else {
      \Drupal::messenger()
        ->addMessage($this
        ->t('No users were selected to remove.'), MessengerInterface::TYPE_WARNING);
    }
  }
}