You are here

public function SimpleUserConfirmImportForm::submitForm in Simple Node 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/SimpleUserConfirmImportForm.php, line 66

Class

SimpleUserConfirmImportForm
Defines a confirmation form to confirm deletion of something by id.

Namespace

Drupal\simple_node_importer\Form

Code

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

  // Remove unnecessary values.
  $form_state
    ->cleanValues();
  $node_storage = $this->entityTypeManager
    ->getStorage('node');
  $file_storage = $this->entityTypeManager
    ->getStorage('file');
  $snp_nid = $form_state
    ->getValue('snp_nid');
  $node = $node_storage
    ->load($snp_nid);
  $map_values = $this->sessionVariable
    ->get('mapvalues');
  $fid = $node
    ->get('field_upload_csv')
    ->getValue()[0]['target_id'];
  $file = $file_storage
    ->load($fid);
  $csv_uri = $file
    ->getFileUri();
  $handle = fopen($csv_uri, 'r');
  $columns = [];
  $columns = array_values($this->services
    ->simpleNodeImporterGetAllColumnHeaders($csv_uri));
  $record = [];
  $map_fields = array_keys($map_values);
  $i = 1;
  $records = [];
  while ($row = fgetcsv($handle)) {
    if ($i == 1) {
      $i++;
      continue;
    }
    foreach ($row as $k => $field) {
      $column1 = str_replace(' ', '_', strtolower($columns[$k]));
      foreach ($map_fields as $field_name) {
        if ($map_values[$field_name] == $column1) {
          $record[$field_name] = trim($field);
        }
        else {
          if (is_array($map_values[$field_name])) {
            $multiple_fields = array_keys($map_values[$field_name]);
            foreach ($multiple_fields as $j => $m_fields) {
              if ($m_fields == $column1) {
                if ($m_fields == 'roles') {
                  $field = str_replace(' ', '_', strtolower($field));
                }
                if (!empty($field)) {
                  $record[$field_name][$j] = trim($field);
                }
                else {
                  $record[$field_name][$j] = NULL;
                }
              }
            }
          }
        }
      }
    }
    $record['nid'] = $node
      ->id();
    $record['type'] = 'user';
    $records[] = $record;
  }

  // Preapring batch parmeters to be execute.
  $batch = [
    'title' => t('Importing User'),
    'operations' => [
      [
        '\\Drupal\\simple_node_importer\\Controller\\UserImportController::userImport',
        [
          $records,
        ],
      ],
    ],
    'finished' => '\\Drupal\\simple_node_importer\\Controller\\UserImportController::userImportBatchFinished',
    'init_message' => t('Initialializing User importing.'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('User creation has encountered an error.'),
  ];

  // Set the batch operation.
  batch_set($batch);
  fclose($handle);
}