You are here

public function PurgeBlockForm::submitForm in Purge 8.3

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

modules/purge_ui/src/Form/PurgeBlockForm.php, line 208

Class

PurgeBlockForm
End-user form for \Drupal\purge_ui\Plugin\Block\PurgeBlock.

Namespace

Drupal\purge_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Instantiate the invalidation objects with the prepared expressions.
  $invalidations = [];
  foreach ($form_state
    ->getBuildInfo()['expressions'] as $expression) {
    try {
      $invalidations[] = $this->purgeInvalidationFactory
        ->get($this->config['type'], $expression);
    } catch (TypeUnsupportedException $e) {
      $this->messenger
        ->addError($this
        ->t("No purger supports the type '@type', please install one!", [
        '@type' => $this->config['type'],
      ]));
      return;
    }
  }

  // Queue execution is the easiest, as it always succeeds.
  if ($this->config['execution'] === 'queue') {
    $this->purgeQueue
      ->add($this->queuer, $invalidations);
    $this->messenger
      ->addStatus($this
      ->t('Please wait for the queue to be processed!'));
  }
  else {
    try {
      $this->purgePurgers
        ->invalidate($this->processor, $invalidations);

      // Prepare and issue messages for each individual invalidation object.
      foreach ($invalidations as $invalidation) {
        $object = $invalidation
          ->getType();
        if (!is_null($invalidation
          ->getExpression())) {
          $object = $this
            ->t('@object with expression "@expr"', [
            '@object' => $invalidation
              ->getType(),
            '@expr' => (string) $invalidation
              ->getExpression(),
          ]);
        }
        if ($invalidation
          ->getState() === InvStatesInterface::SUCCEEDED) {
          $this->messenger
            ->addMessage($this
            ->t('Succesfully cleared @object.', [
            '@object' => $object,
          ]));
        }
        elseif ($invalidation
          ->getState() === InvStatesInterface::PROCESSING) {
          $this->messenger
            ->addWarning($this
            ->t('Requested to clear multistep object of type @object!', [
            '@object' => $object,
          ]));
        }
        else {
          $this->messenger
            ->addError($this
            ->t('Failed to clear @object!', [
            '@object' => $object,
          ]));
        }
      }
    } catch (DiagnosticsException $e) {
      $this->messenger
        ->addError($e
        ->getMessage());
    } catch (CapacityException $e) {
      $this->messenger
        ->addError($e
        ->getMessage());
    } catch (LockException $e) {
      $this->messenger
        ->addError($e
        ->getMessage());
    }
  }
}