You are here

public function DefaultContentManager::importContent in Default Content 8

Imports default content for a given module.

Parameters

string $module: The module to create the default content for.

Return value

array[\Drupal\Core\Entity\EntityInterface] The created entities.

Overrides DefaultContentManagerInterface::importContent

File

src/DefaultContentManager.php, line 103
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

public function importContent($module) {
  $created = array();
  $folder = drupal_get_path('module', $module) . "/content/";
  if (file_exists($folder)) {
    $this->file_map = [];
    foreach ($this
      ->getFilesInOrder($folder) as $link => $details) {
      if (!empty($this->file_map[$link])) {
        $file = $this->file_map[$link];
        $entity_type_id = $file->entity_type_id;
        $entity = $this
          ->generateEntity($file, $file->entity_type_id);
        if ($this->entityTypeManager
          ->getStorage('node')
          ->loadByProperties([
          'uuid' => $entity
            ->uuid(),
        ])) {
          drupal_set_message(t('node @uuid already exists', [
            '@uuid' => $entity
              ->uuid(),
          ]));
        }
        else {

          //drupal_set_message("Importing node ".$entity->uuid());
          $entity
            ->save();
          $created[] = $entity;
        }
      }
    }

    //import the menu links

    //@todo at the moment this is just importing the entity types the author needed
    if ($files = $this
      ->scanner()
      ->scan($folder . '/menu_link_content', 'yml')) {
      foreach ($files as $file) {
        $menu_link = Yaml::decode(file_get_contents($file->uri));
        $entities = $this->entityTypeManager
          ->getStorage('node')
          ->loadByProperties([
          'uuid' => $menu_link['dest_uuid'],
        ]);

        //assume the entity exists!
        unset($menu_link['dest_uuid']);
        $menu_link['link'] = [
          'uri' => 'entity:node/' . reset($entities)
            ->id(),
          'name' => $menu_link['title'],
          'options' => [],
        ];
        $link = \Drupal\menu_link_content\Entity\MenuLinkContent::create($menu_link);
        $link
          ->save();
      }
    }
  }

  // Reset the tree.
  $this
    ->resetTree();

  // Reset link domain.

  //$this->linkManager->setLinkDomain(FALSE);
  return $created;
}