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

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bbu23 created an issue. See original summary.

bbu23’s picture

This will definitely end up in a Merge Request, but for now I'm adding a quick patch here because I need it myself also. Though the problem is that this patch depends on the Merge Request from here: https://www.drupal.org/project/menu_migration/issues/3347964. I didn't test if it applies without it.

Will get back to this later.

bbu23’s picture

Status: Active » Needs review
bbu23’s picture

  • bbu23 committed c835d77f on 2.0.x
    Issue #3407787: Allow menu item alteration per environment before import...
bbu23’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.