You are here

public function NodeImportFile::submitForm in Node export 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/NodeImportFile.php, line 46

Class

NodeImportFile
Provides a Node Import form.

Namespace

Drupal\node_Export\Form

Code

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

  // $json = $form_state->getValue('paste');.
  $validators = [
    'file_validate_extensions' => [
      'json',
    ],
  ];
  $file = file_save_upload('myfile', $validators, FALSE, 0);
  if (!$file) {
    return;
  }
  else {
    $data = file_get_contents($file
      ->getFileUri());
  }
  $nodes = json_decode($data, TRUE);
  $batch = [
    'title' => $this
      ->t('Importing Nodes...'),
    'operations' => [],
    'init_message' => $this
      ->t('Imporitng'),
    'progress_message' => $this
      ->t('Processed @current out of @total.'),
    'error_message' => $this
      ->t('An error occurred during processing'),
    'finished' => '\\Drupal\\node_export\\NodeImport::nodeImportFinishedCallback',
  ];
  foreach ($nodes as $node) {
    $batch['operations'][] = [
      '\\Drupal\\node_export\\NodeImport::nodeImport',
      [
        $node,
      ],
    ];
  }
  batch_set($batch);
  $this
    ->messenger()
    ->addStatus($this
    ->t('Node has been imported succesfully.'));
}