You are here

public function ImporterForm::submitForm in CSV Importer 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/ImporterForm.php, line 341

Class

ImporterForm
Provides CSV importer form.

Namespace

Drupal\csv_importer\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $entity_type = $form_state
    ->getValue('entity_type');
  $entity_type_bundle = NULL;
  $csv = current($form_state
    ->getValue('csv'));
  $csv_parse = $this->parser
    ->getCsvById($csv, $form_state
    ->getUserInput()['delimiter']);
  if (isset($form_state
    ->getUserInput()['entity_type_bundle'])) {
    $entity_type_bundle = $form_state
      ->getUserInput()['entity_type_bundle'];
  }
  $entity_fields = $this
    ->getEntityTypeFields($entity_type, $entity_type_bundle);
  if ($required = $this
    ->getEntityTypeMissingFields($entity_type, $entity_fields['required'], $csv_parse)) {
    $render = [
      '#theme' => 'item_list',
      '#items' => $required,
    ];
    $this
      ->messenger()
      ->addError($this
      ->t('Your CSV has missing required fields: @fields', [
      '@fields' => $this->renderer
        ->render($render),
    ]));
  }
  else {
    $this->importer
      ->createInstance($form_state
      ->getUserInput()['plugin_id'], [
      'csv' => $csv_parse,
      'csv_entity' => $this->parser
        ->getCsvEntity($csv),
      'entity_type' => $entity_type,
      'entity_type_bundle' => $entity_type_bundle,
      'fields' => $entity_fields['fields'],
    ])
      ->process();
  }
}