Allow menu item alteration per environment before import

Created on 10 December 2023, about 1 year ago
Updated 12 December 2023, about 1 year ago

Problem/Motivation

Currently, there is no way of altering the menu items during import per environment or at all. In some situations, like a multisite, we might have menu items that point from Site A to Site B, so an absolute url might be required. In this case, a dev or staging environment would naturally have the production URLs instead of the test urls.

This is also useful if you have a structure on a separate site that is very similar with a new site, and you want to have a starting point.

Steps to reproduce

Create a menu, export it, and import it on multiple sites/environments. Observe that some URLs do not match the environments' URLs.

Proposed resolution

Create an event that allows altering the structure of the menu item before it's turned into a MenuLinkContent and saved.

Have this defined in settings.local as follows:

// Menu Migration replacements.
$settings['menu_migration_replacements'] = [
  // Add as many as needed.
  [
    // Example for the link of the menu item. This is the nested key.
    'parents' => ['link', 'uri'],
    // The function to call for the current nested key.
    'method' => 'str_replace',
    // The arguments to pass to the
    'arguments' => [
      'example.com',
      'local.example.com',
      // "[SUBJECT]" is a token that will be replaced with the actual value. Mandatory.
      '[SUBJECT]',
    ],
  ],

With the above example, the menu link "https://example.com" will become "https://local.example.com" using str_replace.

Remaining tasks

Things to consider:
- Add a setting for turning this on/off, could be in the drush command as option.
- Should we consider export to convert back to original to avoid messing with the original tree if we make changes?
- Anything else?

User interface changes

None

API changes

None

Data model changes

None

Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

🇷🇴Romania bbu23

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024