You are here

function DefaultContentManager::getFilesInOrder in Default Content 8

1 call to DefaultContentManager::getFilesInOrder()
DefaultContentManager::importContent in src/DefaultContentManager.php
Imports default content for a given module.

File

src/DefaultContentManager.php, line 305
Contains \Drupal\defaultcontent\DefaultContentManager. @todo remove all references to linkmanager?

Class

DefaultContentManager
A service for handling import of default content. @todo throw useful exceptions

Namespace

Drupal\defaultcontent

Code

function getFilesInOrder($folder) {
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type_id => $entity_type) {
    if ($entity_type_id == 'menu_link_content') {
      continue;

      //we'll deal with these AFTER the nodes are imported at least with drush
    }
    $reflection = new \ReflectionClass($entity_type
      ->getClass());

    // We are only interested in importing content entities.
    if ($reflection
      ->implementsInterface('\\Drupal\\Core\\Config\\Entity\\ConfigEntityInterface')) {
      continue;
    }
    if (!file_exists($folder . '/' . $entity_type_id)) {
      continue;
    }
    $files = $this
      ->scanner()
      ->scan($folder . '/' . $entity_type_id, 'json');

    // Default content uses drupal.org as domain.
    // @todo Make this use a uri like default-content:.

    //$this->linkManager->setLinkDomain(static::LINK_DOMAIN);

    // Parse all of the files and sort them in order of dependency.
    foreach ($files as $file) {
      $contents = $this
        ->parseFile($file);

      // Decode the file contents.
      $decoded = $this->serializer
        ->decode($contents, 'hal_json');

      // Get the link to this entity.
      $self = $decoded['_links']['self']['href'];

      // Throw an exception when this URL already exists.
      if (isset($this->file_map[$self])) {
        $args = array(
          '@href' => $self,
          '@first' => $this->file_map[$self]->uri,
          '@second' => $file->uri,
        );

        // Reset link domain.

        //$this->linkManager->setLinkDomain(FALSE);
        throw new \Exception(t('Default content with href @href exists twice: @first @second', $args));
      }

      // Store the entity type with the file.
      $file->entity_type_id = $entity_type_id;

      // Store the file in the file map.
      $this->file_map[$self] = $file;

      // Create a vertex for the graph.
      $vertex = $this
        ->getVertex($self);
      $this->graph[$vertex->link]['edges'] = [];
      if (empty($decoded['_embedded'])) {

        // No dependencies to resolve.
        continue;
      }

      // Here we need to resolve our dependencies;
      foreach ($decoded['_embedded'] as $embedded) {
        foreach ($embedded as $item) {
          $edge = $this
            ->getVertex($item['_links']['self']['href']);
          $this->graph[$vertex->link]['edges'][$edge->link] = TRUE;
        }
      }
    }
  }

  // @todo what if no dependencies?
  return $this
    ->sortTree($this->graph);
}