You are here

public function BookEventSubscriber::exportBookOutlines in Tome 8

Exports all book outlines.

File

modules/tome_sync/src/EventSubscriber/BookEventSubscriber.php, line 62

Class

BookEventSubscriber
Event subscriber that keep book outlines in sync with content changes.

Namespace

Drupal\tome_sync\EventSubscriber

Code

public function exportBookOutlines() {
  $storage = $this->entityTypeManager
    ->getStorage('node');
  $results = $this->connection
    ->select('book')
    ->fields('book')
    ->execute()
    ->fetchAll();
  $id_map = [];
  $rows = [];
  foreach ($results as $result) {
    $row = [];
    foreach ($result as $key => $value) {
      if (!$value || in_array($key, [
        'has_children',
        'weight',
        'depth',
      ], TRUE)) {
        $row[$key] = $value;
      }
      else {
        if (!isset($id_map[$value]) && ($node = $storage
          ->load($value))) {
          $id_map[$value] = $node
            ->uuid();
        }
        $row[$key] = $id_map[$value];
      }
    }
    $rows[] = $row;
  }
  $directory = $this
    ->getExportDirectory();
  $this->fileSystem
    ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
  file_put_contents("{$directory}/book_outlines.json", json_encode($rows, JSON_PRETTY_PRINT));
}