Allow underscore in menu machine name

Created on 17 August 2022, over 2 years ago
Updated 5 March 2025, about 1 month ago

Problem/Motivation

Machine names of menus use a hyphen instead of an underscore as default replacement.
This is different then most (all?) other machine-names.

A machine-name like menu_main is therefor not possible. It must be menu-main.

This is possibly only a UI feature for helping administrators with creating valid machine-names.
In that case there are no deeper dependencies where code related to the menu-system requires the hyphen-pattern.

Steps to reproduce

  • Create a new menu via the UI, use the underscore _ in the machine-name. Notice the underscore is replaced with a hyphen -
  • Create a new menu via code and use underscores in its machinename, edit the menu in the UI and notice this error: "A unique name to construct the URL for the menu. It must only contain lowercase letters, numbers and hyphens."

Proposed resolution

If this is a UI feature only the proposal is:

  1. Allow both _ and - in machine-names of menu's. This is a BC layer for both patterns.
  2. Default the replacement to underscore _ which is inline with other machine-names.
  3. Currently no upgrade path where existing menu machine-names are converted to the underscore pattern.
  4. Currently no changes in default config (install profiles) that create new menus in new installations.

After this change the following machine-names are valid

  1. menu_main
  2. menu-main
  3. menu-_main

If an administrator creates a new menu with the title "Menu main", the automatically created machine-name will be the first one with underscores i.e. menu_main

Remaining tasks

  • Create a patch
  • Get an answer if there is code in Drupal core or contrib that depends on the existing machine-name pattern without underscore

User interface changes

API changes

  • Both Underscores and hyphens are allowed in menu-machine names

Data model changes

Release notes snippet

Feature request
Status

Needs work

Version

11.0 🔥

Component

menu system

Created by

🇳🇱Netherlands ndf Amsterdam

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024