8 – Rebuild the menu after adding a menu item

I'm migrating nodes from D7 to D8. For several reasons I am writing my own code to do this. The migration will run on a couple of dozens of different sites, so the solution should be as generic as possible. I have implemented a custom RestAPI to do this. Part of the process is to transfer menu items (custom).

In a multilevel menu, I have to check if all parents exist before creating the actual menu item that is being migrated. Suppose you have a menu tree with three levels

Main menu -> Level 1 -> Level 2 -> Element of Menu 1 that is being migrated.

I'm migrating the node that will be linked to Menu item 1 that is being migrated. Yes Level 2 it does not exist on the Drupal 8 site, I have to create this primary menu before adding Menu element being migratere.

This part was easy. The problem is that when you process the next node (and its menu item), say:

Main menu -> Level 1 -> Level 2 -> Menu item 2 that is being migrated

So Level 2 Now it should exist, I created it during the first call. However, when I try to load Level 2 – It is not found, so the code ends up creating. Level 2 again.

This is the code that creates the main menu item:

                $ title = (trim ($ menu['title'])! == & # 39; & # 39;)? $ menu['title'] : $ menu['link_title'];
$ menuStorage =  Drupal :: entityTypeManager ()
-> getStorage (& # 39; menu_link_content & # 39;);
$ menuItem = $ menuStorage-> loadByProperties (['title' => $title, 'menu_name' => self::MAIN_MENU]);
if (is_array ($ menuItem) &&! empty ($ menuItem)) {
$ menuItem = array_pop ($ menuItem);
}

yes (! $ menuItem) {
$ data = [
        'title'    => $title,
        'link'     => 'internal:' . $this->fixPath($menu['link_path'])
& # 39; expanded & # 39; => $ menu['expanded'],
];

yes (! $ father) {
$ data['menu'] = auto :: MAIN_MENU;
}
else {
$ data['parent'] = $ parent-> getPluginId ();
}

$ menuItem = $ menuStorage-> create ($ data);
$ menuItem-> save ();
}

returns $ menuItem;
}

On the second pass, the second node / menu item that is being created, this line of code returns an empty array

$ menuItem = $ menuStorage-> loadByProperties (['title' => $title, 'menu_name' => self::MAIN_MENU]);

So, basically, the menu item that I created during the first pass was not found.

What makes this strange is that if I run the code once and then load the menu in the administration area (admin / structure / menu / manage / main) – I can see that it was created, so it's not a caching thing, and it's not about rebuilding the menu tree (I tried several ways to rebuild the menu tree before calling it) loadByProperties

I even tried to delete all the caches in the administration area between the two calls: I'm pretty sure it's not a cache thing, it's just that the menu is not found.

It is worth noting that if Level 2 exists before you start migrating the nodes, find Level 2 Each time and the menu is created correctly.

Main menu
Level 2
Menu item 1 that is being migrated
Menu item 2 that is being migrated

Yes Level 2 It does not exist at the beginning of the migration, I land with:

Main menu
Level 2
Menu item 1 that is being migrated
Level 2
Menu item 2 that is being migrated

Needless to say, I am frustrated and fresh from the ideals, so any suggestion or suggestion would be great.