You are here

public function PriceListItemImportForm::submitForm in Commerce Pricelist 8.2

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/PriceListItemImportForm.php, line 217

Class

PriceListItemImportForm

Namespace

Drupal\commerce_pricelist\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $file = file_save_upload('csv', $form['csv']['#upload_validators'], 'temporary://', 0, FileSystemInterface::EXISTS_RENAME);
  $values = $form_state
    ->getValues();
  $batch = [
    'title' => $this
      ->t('Importing prices'),
    'progress_message' => '',
    'operations' => [],
    'finished' => [
      $this,
      'finishBatch',
    ],
  ];
  if ($values['delete_existing']) {
    $batch['operations'][] = [
      [
        get_class($this),
        'batchDeleteExisting',
      ],
      [
        $form_state
          ->get('price_list_id'),
      ],
    ];
  }
  $batch['operations'][] = [
    [
      get_class($this),
      'batchProcess',
    ],
    [
      $file
        ->getFileUri(),
      $values['mapping'],
      $values['options'],
      $form_state
        ->get('price_list_id'),
      (bool) $values['delete_existing'],
    ],
  ];
  $batch['operations'][] = [
    [
      get_class($this),
      'batchDeleteUploadedFile',
    ],
    [
      $file
        ->id(),
    ],
  ];
  batch_set($batch);
  $form_state
    ->setRedirect('entity.commerce_pricelist_item.collection', [
    'commerce_pricelist' => $form_state
      ->get('price_list_id'),
  ]);
}