You are here

public static function Nodes::import in Hook Update Deploy Tools 7

Performs the unique steps necessary to import node items from export files.

Parameters

string|array $node_paths: The unique identifier(s) of the thing to import, usually the machine name or array of machine names.

Overrides ImportInterface::import

File

src/Nodes.php, line 17

Class

Nodes
Public method for changing nodes programatically.

Namespace

HookUpdateDeployTools

Code

public static function import($node_paths) {
  $t = get_t();
  $completed = array();
  $node_paths = (array) $node_paths;
  $total_requested = count($node_paths);
  try {
    self::canImport();
    foreach ($node_paths as $key => $node_path) {
      $filename = self::normalizeFileName($node_path);
      $path = self::normalizePathName($node_path);

      // If the file is there, process it.
      if (HudtInternal::canReadFile($filename, 'node')) {

        // Read the file.
        $file_contents = HudtInternal::readFileToString($filename, 'node');
        eval('$node_import = ' . $file_contents . ';');
        if (!is_object($node_import)) {
          if (empty($errors)) {
            $errors = 'Node build error on eval().';
          }
          $message = 'Unable to get a node from the import. Errors: @errors';
          throw new HudtException($message, array(
            '@errors' => $errors,
          ), WATCHDOG_ERROR);
        }
        $error_msg = '';
        $result = self::processOne($node_import, $path);

        // No Exceptions so far, so it must be a success.
        $message = '@operation: @path - successful.';
        global $base_url;
        $link = "{$base_url}/{$result['edit_link']}";
        $vars = array(
          '@operation' => $result['operation'],
          '@path' => $path,
        );
        Message::make($message, $vars, WATCHDOG_INFO, 1, $link);
        $completed[$path] = $result['operation'];
      }
    }
  } catch (\Exception $e) {
    $vars = array(
      '!error' => method_exists($e, 'logMessage') ? $e
        ->logMessage() : $e
        ->getMessage(),
    );
    if (!method_exists($e, 'logMessage')) {

      // Not logged yet, so log it.
      $message = 'Node import denied because: !error';
      Message::make($message, $vars, WATCHDOG_ERROR);
    }

    // Output a summary before shutting this down.
    $done = HudtInternal::getSummary($completed, $total_requested, 'Imported');
    Message::make($done, array(), FALSE, 1);
    throw new HudtException('Caught Exception: Update aborted!  !error', $vars, WATCHDOG_ERROR, FALSE);
  }
  $done = HudtInternal::getSummary($completed, $total_requested, 'Imported Nodes');
  return $done;
}