You are here

class NodeExport in Node export 8

Provides a Node Export function.

Hierarchy

Expanded class hierarchy of NodeExport

4 files declare their use of NodeExport
BulkNodeExport.php in src/Form/BulkNodeExport.php
NodeExportCommands.php in src/Commands/NodeExportCommands.php
NodeExportForm.php in src/Form/NodeExportForm.php
node_export.drush.inc in ./node_export.drush.inc
Drush integration for Node Export module.

File

src/NodeExport.php, line 11

Namespace

Drupal\node_export
View source
class NodeExport {

  /**
   * Prepares a URI for the file to be saved.
   *
   * @param string $format
   *   The format of the file to be saved.
   *
   * @return string
   *   A URI for the file to be saved.
   */
  protected static function getFileUri($format) {
    return uniqid(\Drupal::config('system.file')
      ->get('default_scheme') . '://node_export_') . '.' . strtolower($format);
  }

  /**
   * Prepares an array of field-value pairs of nodes.
   *
   * @param int[] $ids
   *   The ids of the nodes to be exported.
   *
   * @return array
   *   An array of field-value pairs of nodes.
   */
  protected static function getNodesArray(array $ids) {
    $nodes = empty($ids) ? Node::loadMultiple() : Node::loadMultiple($ids);
    $index = 0;
    $nodesArray = [];
    foreach ($nodes as $node) {
      foreach ($node as $key => $value) {
        $nodesArray[$index][$key] = $node
          ->get($key)
          ->getValue();
      }
      $index++;
    }
    return $nodesArray;
  }

  /**
   * Exports nodes to a specified format to a file/code.
   *
   * @param int[] $ids
   *   The ids of the nodes to be exported.
   * @param string $format
   *   The format of the file to be saved.
   * @param bool $save
   *   Whether to save the file or return the code.
   *
   * @return \Drupal\file\FileInterface|false|string
   *   Returns either the code to be exported, file entity or FALSE.
   */
  public static function export(array $ids, $format, $save) {
    $nodesArray = NodeExport::getNodesArray($ids);
    switch ($format) {
      case 'dsv':

        // TODO: Implement delimiter separated values.
        break;
      case 'json':
        $data = json_encode($nodesArray);
        break;
      case 'serialize':

        // TODO: Implement PHP serialized object.
        break;
      case 'xml':

        // TODO: Implement XML.
        break;
    }
    return $save ? file_save_data($data, NodeExport::getFileUri($format), FileSystemInterface::EXISTS_REPLACE) : $data;
  }

  /**
   * Batch operations method to export a node with given node ID.
   *
   * @param int $nid
   *   ID of node to be exported.
   * @param array $context
   *   An array of contextual key/value information for rebuild batch process.
   */
  public static function nodeExport($nid, array &$context) {
    $message = 'Exporting Nodes...';
    $context['results'][] = $nid;
    $context['message'] = $message;
  }

  /**
   * Batch finish callback method. Handles results & display messages to user.
   *
   * @param bool $success
   *   Whether or not any fatal PHP errors were detected.
   * @param array $results
   *   An array containing the results key of context array.
   * @param array $operations
   *   An array containing batch operations.
   */
  public static function nodeExportFinishedCallback($success, array $results, array $operations) {

    // The 'success' parameter means no fatal PHP errors were detected. All
    // other error management should be handled using 'results'.
    if ($success) {
      $message = \Drupal::translation()
        ->formatPlural(count($results), 'One node exported ', '@count nodes exported ');
    }
    else {
      $message = $this
        ->t('Finished with an error.');
    }
    $file = NodeExport::export($results, 'json', TRUE);
    if ($file) {
      $message = $message . ' to ' . \Drupal::service('file_system')
        ->realpath($file
        ->getFileUri());
      \Drupal::messenger()
        ->addStatus($message);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NodeExport::export public static function Exports nodes to a specified format to a file/code.
NodeExport::getFileUri protected static function Prepares a URI for the file to be saved.
NodeExport::getNodesArray protected static function Prepares an array of field-value pairs of nodes.
NodeExport::nodeExport public static function Batch operations method to export a node with given node ID.
NodeExport::nodeExportFinishedCallback public static function Batch finish callback method. Handles results & display messages to user.