You are here

public static function NodeImport::import in Node export 8

Imports a node with given field-value array.

Parameters

array $nodeArray: An exported node array.

Return value

int|bool Returns the ID of the imported node or FALSE if import is not possible.

3 calls to NodeImport::import()
drush_node_export_import in ./node_export.drush.inc
Drush 8 command to import nodes.
NodeExportCommands::importNode in src/Commands/NodeExportCommands.php
Import nodes.
NodeImport::nodeImport in src/NodeImport.php
Batch operations method to import a given node object.

File

src/NodeImport.php, line 63

Class

NodeImport
Provides a Node Import function.

Namespace

Drupal\node_export

Code

public static function import(array $nodeArray) {
  $fieldValues = self::unload($nodeArray);
  if (!$fieldValues) {
    return FALSE;
  }
  $config = \Drupal::config('node_export.settings');
  $operation = $config
    ->get('node_export_import');
  $id = $fieldValues['nid'][0]['value'];
  unset($fieldValues['nid']);
  unset($fieldValues['vid']);
  unset($fieldValues['uuid']);
  switch ($operation) {
    case 'new':
      $newNode = Node::create($fieldValues);
      break;
    case 'replace':
      $newNode = Node::load($id);
      if ($newNode) {
        foreach ($fieldValues as $field => $values) {
          $newNode
            ->set($field, $values);
        }
        $newNode
          ->setNewRevision(TRUE);
        $newNode
          ->setRevisionCreationTime(\Drupal::time()
          ->getRequestTime());
        $newNode
          ->setRevisionUserId($fieldValues['uid'][0]['target_id']);
      }
      else {
        $newNode = Node::create($fieldValues);
      }
      break;
    case 'skip':
      if (Node::load($id)) {
        return $id;
      }
      $newNode = Node::create($fieldValues);
      break;
  }
  try {
    $newNode
      ->save();
    $id = $newNode
      ->id();
  } catch (EntityStorageException $e) {
    \Drupal::logger('node_export')
      ->error($e
      ->getMessage());
  }
  return $id;
}