diff --git a/core/profiles/demo_umami/modules/demo_umami_content/default_content/languages/en/recipe_categories.csv b/core/profiles/demo_umami/modules/demo_umami_content/default_content/languages/en/recipe_categories.csv index d8a3f78eaa..0733d75ce6 100644 --- a/core/profiles/demo_umami/modules/demo_umami_content/default_content/languages/en/recipe_categories.csv +++ b/core/profiles/demo_umami/modules/demo_umami_content/default_content/languages/en/recipe_categories.csv @@ -1,5 +1,6 @@ -Accompaniment -Dessert -Main course -Snack -Starter +id,term +1,Accompaniment +2,Dessert +3,Main course +4,Snack +5,Starter diff --git a/core/profiles/demo_umami/modules/demo_umami_content/default_content/languages/en/tags.csv b/core/profiles/demo_umami/modules/demo_umami_content/default_content/languages/en/tags.csv index 4bee57b27a..e06cbec2c4 100644 --- a/core/profiles/demo_umami/modules/demo_umami_content/default_content/languages/en/tags.csv +++ b/core/profiles/demo_umami/modules/demo_umami_content/default_content/languages/en/tags.csv @@ -1,28 +1,29 @@ -Alcohol free -Baked -Baking -Breakfast -Cake -Carrots -Chocolate -Cocktail party -Dairy-free -Dessert -Dinner party -Drinks -Egg -Grow your own -Healthy -Herbs -Learn to cook -Mushrooms -Oats -Party -Pasta -Pastry -Seasonal -Shopping -Soup -Supermarkets -Vegan -Vegetarian +id,term +1,Alcohol free +2,Baked +3,Baking +4,Breakfast +5,Cake +6,Carrots +7,Chocolate +8,Cocktail party +9,Dairy-free +10,Dessert +11,Dinner party +12,Drinks +13,Egg +14,Grow your own +15,Healthy +16,Herbs +17,Learn to cook +18,Mushrooms +19,Oats +20,Party +21,Pasta +22,Pastry +23,Seasonal +24,Shopping +25,Soup +26,Supermarkets +27,Vegan +28,Vegetarian diff --git a/core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php b/core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php index 75c3471908..b434ffe681 100644 --- a/core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php +++ b/core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php @@ -134,12 +134,13 @@ public function importContent() { * @throws \Drupal\Core\Entity\EntityStorageException */ protected function importTerms($vocabulary, $filename) { - $row_number = 1; $module_path = $this->moduleHandler->getModule('demo_umami_content')->getPath(); $term_storage = $this->entityTypeManager->getStorage('taxonomy_term'); if (($handle = fopen($module_path . "/default_content/languages/en/$filename", 'r')) !== FALSE) { + $header = fgetcsv($handle); while (($data = fgetcsv($handle)) !== FALSE) { - $term_name = trim($data[0]); + $data = array_combine($header, $data); + $term_name = trim($data['term']); $term = $term_storage->create([ 'name' => $term_name, @@ -148,43 +149,42 @@ protected function importTerms($vocabulary, $filename) { ]); $term->save(); $this->storeCreatedContentUuids([$term->uuid() => 'taxonomy_term']); - $this->saveTermId($vocabulary, $row_number, $term->id()); - $row_number++; + $this->saveTermId($vocabulary, $data['id'], $term->id()); } } return $this; } /** - * Saves a Term ID generated when saving a taxonomy term. + * Retrieves the Term ID of a term saved during the import process. * * @param string $vocabulary * Machine name of vocabulary to which it was saved. - * @param int $row_number - * Row number it was located in the taxonomy CSV file. - * @param int $tid - * Term ID generated when saved in the Drupal database. + * @param int $term_csv_id + * The term's ID from the CSV file. + * + * @return int + * Term ID, or 0 if Term ID could not be found. */ - protected function saveTermId($vocabulary, $row_number, $tid) { - $this->termIdMap[$vocabulary][$row_number] = $tid; + protected function getTermId($vocabulary, $term_csv_id) { + if (array_key_exists($vocabulary, $this->termIdMap) && array_key_exists($term_csv_id, $this->termIdMap[$vocabulary])) { + return $this->termIdMap[$vocabulary][$term_csv_id]; + } + return 0; } /** - * Retrieves the Term ID of a term saved during the import process. + * Saves a Term ID generated when saving a taxonomy term. * * @param string $vocabulary * Machine name of vocabulary to which it was saved. - * @param int $row_number - * Row number it was located in the taxonomy CSV file. - * - * @return int - * Term ID, or 0 if Term ID could not be found. + * @param int $term_csv_id + * The term's ID from the CSV file. + * @param int $tid + * Term ID generated when saved in the Drupal database. */ - protected function getTermId($vocabulary, $row_number) { - if (array_key_exists($vocabulary, $this->termIdMap) && array_key_exists($row_number, $this->termIdMap[$vocabulary])) { - return $this->termIdMap[$vocabulary][$row_number]; - } - return 0; + protected function saveTermId($vocabulary, $term_csv_id, $tid) { + $this->termIdMap[$vocabulary][$term_csv_id] = $tid; } /** @@ -251,8 +251,8 @@ protected function importArticles() { if (!empty($data['tags'])) { $values['field_tags'] = []; $tags = explode(',', $data['tags']); - foreach ($tags as $row_number) { - if ($tid = $this->getTermId('tags', $row_number)) { + foreach ($tags as $tag_id) { + if ($tid = $this->getTermId('tags', $tag_id)) { $values['field_tags'][] = ['target_id' => $tid]; } } @@ -324,8 +324,8 @@ protected function importRecipes() { if (!empty($data['recipe_category'])) { $values['field_recipe_category'] = []; $tags = array_filter(explode(',', $data['recipe_category'])); - foreach ($tags as $row_number) { - if ($tid = $this->getTermId('recipe_category', $row_number)) { + foreach ($tags as $tag_id) { + if ($tid = $this->getTermId('recipe_category', $tag_id)) { $values['field_recipe_category'][] = ['target_id' => $tid]; } } @@ -366,8 +366,8 @@ protected function importRecipes() { if (!empty($data['tags'])) { $values['field_tags'] = []; $tags = array_filter(explode(',', $data['tags'])); - foreach ($tags as $row_number) { - if ($tid = $this->getTermId('tags', $row_number)) { + foreach ($tags as $tag_id) { + if ($tid = $this->getTermId('tags', $tag_id)) { $values['field_tags'][] = ['target_id' => $tid]; } }