You are here

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

Exports a single Node based on its nid. (Typically called from Drush).

Parameters

string $nid: The nid of the node to export.

Return value

string The URI of the item exported, or a failure message.

Overrides ExportInterface::export

File

src/Nodes.php, line 441

Class

Nodes
Public method for changing nodes programatically.

Namespace

HookUpdateDeployTools

Code

public static function export($nid) {
  $t = get_t();
  try {
    Check::notEmpty('nid', $nid);
    Check::isNumeric('nid', $nid);
    self::canExport();
    $msg_return = '';

    // Load the node if it exists.
    $node = node_load($nid);
    Check::notEmpty('node', $node);
    $storage_path = HudtInternal::getStoragePath('node');
    $node_path = drupal_lookup_path('alias', "node/{$nid}");
    Check::notEmpty('node alias', $node_path);
    $node_path = self::normalizePathName($node_path);
    $file_name = self::normalizeFileName($node_path);
    $file_uri = DRUPAL_ROOT . '/' . $storage_path . $file_name;

    // Made it this far, it exists, so export it.
    $export_contents = drupal_var_export($node);

    // Save the file.
    $msg_return = HudtInternal::writeFile($file_uri, $export_contents);
  } catch (\Exception $e) {

    // Any errors from this command do not need to be watchdog logged.
    $e->logIt = FALSE;
    $vars = array(
      '!error' => method_exists($e, 'logMessage') ? $e
        ->logMessage() : $e
        ->getMessage(),
    );
    $msg_error = $t("Caught exception:  !error", $vars);
  }
  if (!empty($msg_error)) {
    drush_log($msg_error, 'error');
  }
  return !empty($msg_return) ? $msg_return : $msg_error;
}