My configs:
PHP 5.6
Soure:

  • Drupal 7.58
  • Category with hierarchy (max depth =3)

Destination:

  • Drupal 8.52
  • Migrate_plus
  • Migrate_tools

When i migrate Taxonomy Term (has parent), parent and parent_id always return array(0=>null). I looked at core/modules/taxonomy/migrations/d7_taxonomy_term.yml and found plugin: migration_lookup and migration: d7_taxonomy_term alway return NULL although plugin Get return valid value.

parent_id:
    -
      plugin: skip_on_empty
      method: process
      source: parent
    -
      plugin: migration_lookup
      migration: d7_taxonomy_term
  parent:
    plugin: default_value
    default_value: 0
    source: '@parent_id'

Please try to change the code:

# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- a/core/modules/migrate/src/MigrateExecutable.php
+++ b/core/modules/migrate/src/MigrateExecutable.php
@@ -363,8 +363,18 @@
           $break = FALSE;
           foreach ($value as $scalar_value) {
             try {
-              $new_value[] = $plugin->transform($scalar_value, $this, $row, $destination);
+//              $new_value[] = $plugin->transform($scalar_value, $this, $row, $destination);
+                // reserve salar value
+                            $result = $plugin->transform($scalar_value, $this, $row, $destination);
+                            // fix parent_id always return null on MigrationLookup::transform
+                            if (is_null($result)) {
+                                if ($scalar_value) {
+                                    $new_value[] = $scalar_value;
             }
+                            } else {
+                                $new_value[] = $result;
+                            }
+            }
             catch (MigrateSkipProcessException $e) {
               $new_value[] = NULL;
               $break = TRUE;


Support from Acquia helps fund testing for Drupal Acquia logo

Comments

trong.nguyen.tcec created an issue. See original summary.

trong.nguyen.tcec’s picture

Title: Fix: Migrate taxonomy terms lost hierarchy » Fix: Migrate taxonomy terms lost hierarchy after upgrade from Drupal 7 to Drupal 8
Issue summary: View changes
Related issues: +#2850084: Taxonomy lost hierarchy after import
trong.nguyen.tcec’s picture

Issue summary: View changes
AlanHDev’s picture

We are migrating nested taxonomies with a max depth of 3 levels from a 7.59 source to an 8.5.3 destination and have just experienced this issue too.

I have re-rolled the patch to observe coding standards and this does appear to address the issue. I'll mark this as needs further work as I am unsure whether the solution may have a detrimental effect on other migrations.

AlanHDev’s picture

Status: Active » Needs work
heddn’s picture

This seems like a duplicate of other already open issues. Can we review https://www.drupal.org/project/issues/drupal?text=migration_lookup&statu... and see if that is true?

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

heddn’s picture

Test needs to create a child term before the parent is created. Terms require parent, vid, name as inputs. Will need to be at least a Kernel test.

maxocub’s picture

Working on the test.

maxocub’s picture

Version: 8.6.x-dev » 8.7.x-dev
FileSize
12.9 KB

First of all, we should not modify MigrateExecutable to fix this.

I think I found the source of the problem and it's a mix of migration_lookup process plugin and migrate_plus.
Since d7_taxonomy_term is a derived migration, migrate_plus generate one migration per vocabulary, for example:

  • upgrade_d7_taxonomy_term_tags
  • upgrade_d7_taxonomy_term_categories
  • etc.

But when you look at those generated migrations, the migration in the migration_lookup process plugin is not the derived one, so it doesn't exists:

parent_id:
  -
    plugin: skip_on_empty
    method: process
    source: parent
  -
    plugin: migration_lookup
    migration: upgrade_d7_taxonomy_term

If you change it to upgrade_d7_taxonomy_term_tags, then it works.

Also to be noted, upgrading from th UI works, it's only when using migrate_plus that the hierarchy is lost.
I'll try to see if the fix should be in migrate_plus or in migration_lookup.

In the meanwhile, here's a test that shows that the parent IDs are migrated, even if the childs are created first (that is with a lower tid than their parent).

maxocub’s picture

Status: Needs work » Closed (outdated)

Thanks to @mikelutz help on Slack, we discovered that this problem is already fixed by this issue: #2976379: Expand all derived migrations in migration_lookup from the migrate_upgrade module.

I did test it manually with the patch from that issue and it fixes this term hierarchy lost. (I was also able to reproduce the bug before applying the patch)

I'm closing this issue as outdated and I'll ask you to try again with either the patch from that issue or the latest dev branch from migrate_upgrade. You'll have to re-run --configure-only or manually update your migrations to expand the derivatives.