You are here

protected function InstallHelper::importContentFromFile in Drupal 10

Same name and namespace in other branches
  1. 8 core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php \Drupal\demo_umami_content\InstallHelper::importContentFromFile()
  2. 9 core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php \Drupal\demo_umami_content\InstallHelper::importContentFromFile()

Imports content.

Parameters

string $entity_type: Entity type to be imported

string $bundle_machine_name: Bundle machine name to be imported.

Return value

$this

File

core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php, line 726

Class

InstallHelper
Defines a helper class for importing default content.

Namespace

Drupal\demo_umami_content

Code

protected function importContentFromFile($entity_type, $bundle_machine_name) {
  $filename = $entity_type . '/' . $bundle_machine_name . '.csv';

  // Read all multilingual content from the file.
  [
    $all_content,
    $translated_languages,
  ] = $this
    ->readMultilingualContent($filename);

  // English is no longer needed in the list of languages to translate.
  $key = array_search('en', $translated_languages);
  unset($translated_languages[$key]);

  // Start the loop with English (default) recipes.
  foreach ($all_content['en'] as $current_content) {

    // Process data into its relevant structure.
    $structured_content = $this
      ->processContent($bundle_machine_name, $current_content, 'en');

    // Save Entity.
    $entity = $this->entityTypeManager
      ->getStorage($entity_type)
      ->create($structured_content);
    $entity
      ->save();
    $this
      ->storeCreatedContentUuids([
      $entity
        ->uuid() => $entity_type,
    ]);

    // Save taxonomy entity Drupal ID, so we can reference it in nodes.
    if ($entity_type == 'taxonomy_term') {
      $this
        ->saveTermId($bundle_machine_name, $current_content['id'], $entity
        ->id());
    }

    // Save media entity Drupal ID, so we can reference it in nodes & blocks.
    if ($entity_type == 'media') {
      $this
        ->saveMediaImageId($current_content['id'], $entity
        ->id());
    }

    // Go through all the languages that have translations.
    foreach ($translated_languages as $translated_language) {

      // Find the translated content ID that corresponds to original content.
      $translation_id = array_search($current_content['id'], array_column($all_content[$translated_language], 'id'));

      // Check if translation was found.
      if ($translation_id !== FALSE) {

        // Process that translation.
        $translated_entity = $all_content[$translated_language][$translation_id];
        $structured_content = $this
          ->processContent($bundle_machine_name, $translated_entity, $translated_language);

        // Save entity's translation.
        $entity
          ->addTranslation($translated_language, $structured_content);
        $entity
          ->save();
      }
    }
  }
  return $this;
}