You are here

public function AssetAddLogActionForm::submitForm in farmOS 2.x

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/core/log/src/Form/AssetAddLogActionForm.php, line 159

Class

AssetAddLogActionForm
Provides an asset add log confirmation form.

Namespace

Drupal\farm_log\Form

Code

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

  // Filter out entities the user doesn't have access to.
  $inaccessible_entities = [];
  $accessible_entities = [];
  foreach ($this->entities as $entity) {
    if (!$entity
      ->access('view', $this
      ->currentUser())) {
      $inaccessible_entities[] = $entity;
      continue;
    }
    $accessible_entities[] = $entity;
  }

  // Default redirect url.
  $redirect_url = $this
    ->getCancelUrl();
  if (!empty($form_state
    ->getValue('confirm')) && !empty($accessible_entities)) {
    $log_type = $form_state
      ->getValue('log_type');
    if (!empty($log_type)) {

      // If a destination query param is set, save it and remove it.
      // First we need to redirect to the /log/add/{log_type} form.
      $destination = $this
        ->getCancelUrl()
        ->setAbsolute()
        ->toString();
      if ($this
        ->getRequest()->query
        ->has('destination')) {
        $destination = $this
          ->getRequest()->query
          ->get('destination');
        $this
          ->getRequest()->query
          ->remove('destination');
      }

      // Build list of asset ids.
      $asset_ids = array_map(function ($asset) {
        return $asset
          ->id();
      }, $accessible_entities);

      // Build query params to include in the redirect.
      $query_params = [
        'destination' => $destination,
        'asset' => $asset_ids,
      ];
      $redirect_url = Url::fromRoute('entity.log.add_form', [
        'log_type' => $log_type,
      ], [
        'query' => $query_params,
      ]);
    }
  }
  $this->tempStore
    ->delete($this
    ->currentUser()
    ->id());
  $form_state
    ->setRedirectUrl($redirect_url);
}