Export menu doesn't sort item according to their position in menu tree

Created on 14 April 2023, over 1 year ago
Updated 27 April 2023, over 1 year ago

Problem/Motivation

When you export menus, you will get an array of menu_link_content that will be sorted by id.
This is the result of loading them using :

$this->entityTypeManager
  ->getStorage('menu_link_content')
  ->loadByProperties(['menu_name' => $menu_name]);

When you import them, you can have some items that will lose their parent depending on their creation order.

Steps to reproduce

Create 2 menu link items and make the second one the parent of the first one:

My menu
ā”œā”€ Second menu link (ID: 2)
ā”‚  ā”œā”€ First menu link (ID: 1)

The export will order them sorted by ID:

$exportMenuLinks = [
  1 => 'First menu link',
  2 => 'Second menu link',
];

When the import will run, the first item to be imported (ID: 1) won't have it's parent created so it will be at first level resulting in incorrect structure:

My menu
ā”œā”€ First menu link (ID: 1)
ā”œā”€ Second menu link (ID: 2)

Proposed resolution

Sort the menu links according to their position in the menu tree.

Remaining tasks

Patch to be written.

User interface changes

None

API changes

None

Data model changes

None

šŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

šŸ‡«šŸ‡·France tostinni

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024