You are here

public function ContentLoader::loadContent in YAML Content 8

Load all demo content for this loader.

Parameters

string $content_file: A file name for the content file to be loaded. The file is assumed to be located within a directory set by `setPath()`.

Return value

array An array of created entities.

Overrides ContentLoaderInterface::loadContent

File

src/ContentLoader/ContentLoader.php, line 302

Class

ContentLoader
ContentLoader class for parsing and importing YAML content.

Namespace

Drupal\yaml_content\ContentLoader

Code

public function loadContent($content_file, $skip_existence_check = TRUE) {
  $this
    ->setExistenceCheck($skip_existence_check);
  $content_data = $this
    ->parseContent($content_file);
  $loaded_content = [];

  // Create each entity defined in the yml content.
  foreach ($content_data as $content_item) {
    $entity = $this
      ->buildEntity($content_item['entity'], $content_item);

    // Dispatch the pre-save event.
    $entity_pre_save_event = new EntityPreSaveEvent($this, $entity, $content_item);
    $this
      ->getEventDispatcher()
      ->dispatch(YamlContentEvents::ENTITY_PRE_SAVE, $entity_pre_save_event);
    $entity
      ->save();

    // Dispatch the post-save event.
    $entity_post_save_event = new EntityPostSaveEvent($this, $entity, $content_item);
    $this
      ->getEventDispatcher()
      ->dispatch(YamlContentEvents::ENTITY_POST_SAVE, $entity_post_save_event);
    $loaded_content[] = $entity;
  }

  // Trigger a hook for post-import processing.
  $this
    ->getModuleHandler()
    ->invokeAll('yaml_content_post_import', [
    $content_file,
    &$loaded_content,
    $content_data,
  ]);
  return $loaded_content;
}