You are here

public function FlaggingCollectionBulkForm::viewsFormSubmit in Flag Lists 4.0.x

Submit handler for the bulk form.

Parameters

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

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

Throws

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Thrown when the user tried to access an action without access to it.

Overrides BulkForm::viewsFormSubmit

File

src/Plugin/views/field/FlaggingCollectionBulkForm.php, line 182

Class

FlaggingCollectionBulkForm
Defines a flagging Collection operations bulk form element.

Namespace

Drupal\flag_lists\Plugin\views\field

Code

public function viewsFormSubmit(&$form, FormStateInterface $form_state) {
  if ($form_state
    ->get('step') == 'views_form_views_form') {
    $user_input = $form_state
      ->getUserInput();
    $baseFlag = $this->flagListsService
      ->getFlagForListById($user_input['template'])
      ->getBaseFlag();
    if ($user_input['flaglist'] == 0) {

      // Create a new flaglist with name $newlist.
      // No cleaning of input?
      $id = strtolower($user_input['newlist']);
      $id = preg_replace("/[^a-z0-9_]/", "_", $id);
      $template = [
        'id' => $id,
        'name' => $user_input['newlist'],
        'type' => $user_input['type'],
      ];
      $flaggingCollection = $this->entityTypeManager
        ->getStorage('flagging_collection')
        ->create($template);
      $flaggingCollection
        ->setBaseFlag($user_input['template']);
      $flaggingCollection
        ->setRelatedFlag($baseFlag);
      $flaggingCollection
        ->save();
      $flag = $flaggingCollection
        ->getRelatedFlag();
      $flagList = $flaggingCollection
        ->id();
    }
    else {
      $flag = $this->flagListsService
        ->getFlaggingCollectionById($user_input['flaglist'])
        ->getRelatedFlag();
      $flagList = $user_input['flaglist'];
    }

    // Find the entities to execute the action on.
    $selected = array_filter($user_input[$this->options['id']]);
    $entities = [];
    $count = 0;
    foreach ($selected as $bulk_form_key) {
      $entity = $this
        ->loadEntityFromBulkFormKey($bulk_form_key);

      // Skip execution if current entity does not exist.
      if (empty($entity)) {
        continue;
      }
      if ($user_input['action'] == 'flag' && !empty($this->flagListsService
        ->getFlagListItemIds($flag
        ->id(), $flagList, $entity
        ->id()))) {

        // Skip execution if the entity is already in the collection.
        continue;
      }
      elseif ($user_input['action'] == 'unflag' && empty($this->flagListsService
        ->getFlagListItemIds($flag
        ->id(), $flagList, $entity
        ->id()))) {

        // Skip execution if the entity isn't in the collection.
        continue;
      }
      $count++;
      $entities[$bulk_form_key] = $entity;
    }

    // If there were entities selected but the action isn't allowed on any of
    // them, we don't need to do anything further.
    if (!$count) {

      // Maybe call the parent?
      // parent::viewsFormSubmit($form, $form_state).
      return;
    }

    // Execute the action via its link.
    //
    // Due to the following
    // https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Action%21ActionInterface.php/interface/ActionInterface/8.9.x
    // it is not implemented as an Action as it might break Drupal 9.
    foreach ($entities as $entity_id => $entity) {
      $actionLink = new ActionLinkController(\Drupal::service('flag'), $this->flagListsService, \Drupal::service('renderer'));
      switch ($user_input['action']) {
        case 'flag':
          $actionLink
            ->flag($flag, $entity
            ->id(), $flagList);
          break;
        case 'unflag':
          $actionLink
            ->unflag($flag, $entity
            ->id(), $flagList);
          break;
        default:
          break;
      }
    }
    $this->messenger
      ->addStatus($this
      ->formatPlural($count, '@count entity affected in %flaglists.', '@count entities affected in %flaglist.', [
      '%flaglist' => $this->flagListsService
        ->getFlaggingCollectionById($flagList)
        ->getName(),
    ]));
  }
}