Problem/Motivation

I named my custom menus starting with a * to distinguish them from the system menus e.g:

*Navigation Left
*Navigation Middle
*Navigation Right

The machine names are like that:

-navigation-left
-navigation-middle
-navigation-right

Menus are not rendered because the corresponding theme hook becomes like menu___name. The ThemeManager::render() searches for hook implementation by stripping double underscores. In this case it fails to find it because the hook name contains triple underscore.
https://git.drupalcode.org/project/drupal/blob/8.8.2/core/lib/Drupal/Cor...

Steps to reproduce

  • Create a new menu in /admin/structure/menu
  • title: *Left Navigation
  • Add a menu link to this menu (no matter to where this link is)
  • Create another new menu in /admin/structure/menu (to show the difference)
  • title: Right navigation
  • Add a menu link to this menu (no matter to where this link is)
  • Go to /admin/structure/block to place both menus in your main menu
  • Place the Primary Menu Block and select the first menu ( *Left Navigation ) and save it
  • Place the Primary Menu Block and select the second menu (Right Navigation) and save it
  • Now go to your homepage, you will notice that Right Navigation was created with the link, while *Left Navigation was not

Proposed resolution

Prevent menu names from starting with a leading -

Remaining tasks

Patch review

User interface changes

Before patch is applied:

After patch applied:

Comments

letrollpoilu created an issue. See original summary.

chi’s picture

Title: Machine name issue starting with hyphen » Menus are not rendered when machine name starting with hyphen
Issue summary: View changes
Related issues: +#3111141: drupal_menu not rendering * machine names

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
larowlan’s picture

Issue summary: View changes
Issue tags: +Novice, +Bug Smash Initiative, +Needs tests
hmendes’s picture

Version: 9.3.x-dev » 9.5.x-dev
Issue summary: View changes

I can see that this problem is happening on Drupal 9.5.x, updating the IS with the steps to reproduce.

hmendes’s picture

Issue summary: View changes
hmendes’s picture

Status: Active » Needs review
StatusFileSize
new1.94 KB

First try at this issue following the Proposed resolution.
Please review.

ranjith_kumar_k_u’s picture

StatusFileSize
new1.93 KB
new843 bytes

CS fixes

ranjith_kumar_k_u’s picture

StatusFileSize
new1.93 KB
new614 bytes

Cspell fix

asha nair’s picture

Applied patch #11 successfully. Now, will get error when menu starts with special characters

asha nair’s picture

StatusFileSize
new33.66 KB
new28.34 KB

Adding screenshots

hmendes’s picture

Issue tags: -Needs tests

Thanks @ranjith_kumar_k_u for fixing the problems :D

@Asha Nair, did you tested it and did a code review?
Can you change this to RTBC if it is all right?

jobsons’s picture

Status: Needs review » Reviewed & tested by the community

Hi, after applying @ranjith_kumar_k_u #11 patch, I did the code review of the issue and manually tested the following charachters with the beginning of the title:

~ ,
` ,
! ,
@ ,
# ,
$ ,
% ,
^ ,
& ,
* ,
( ,
) ,
_ ,
- ,
+ ,
= ,
{ ,
[ ,
} ,
] ,
| ,
\ ,
: ,
; ,
" ,
' ,
< ,
, ,
> ,
. ,
? ,
/ .

All of them led to the Error message: The title can not begin with special characters.

For this reason, I'm changing the status of the issue to RTBC.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Menus are entities, so I think this should be handled via an entity validation constraint instead of in the form.

michelecris’s picture

Assigned: Unassigned » michelecris

Hi!

I will try to work on that.
thanks!

paulocs’s picture

Hello @catch, Menu is a config entity as we can see in Drupal\system\Entity\Menu.
Should we really use entity validation constraint for it? If yes, do you have any example how to implement it for config entities? All examples I see in core are used for content entities only.
I have also noticed that other config entities use the validateForm() to check the field values like in Drupal\node\NodeTypeForm

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);

    $id = trim($form_state->getValue('type'));
    // '0' is invalid, since elsewhere we check it using empty().
    if ($id == '0') {
      $form_state->setErrorByName('type', $this->t("Invalid machine-readable name. Enter a name other than %invalid.", ['%invalid' => $id]));
    }
  }
michelecris’s picture

Assigned: michelecris » Unassigned

Hi!
Sorry, I really try to work on this validation, but I couldn't make it run. So I'm unassigned here.

paulocs’s picture

Status: Needs work » Needs review
StatusFileSize
new6.63 KB

I have updated the patch based on the suggestion from @catch in #16.

vinmayiswamy’s picture

StatusFileSize
new144.77 KB
new113.67 KB

I applied #20 patch against 9.5.x-dev. Patch applied cleanly but I'm not able add menu link with special character after applying patch.
Attaching screenshots for reference.
Thanks

Edit: Apologies for not posting my review clearly. I meant that the patch I tested preventing the menus which are having special characters. So patch working as expected.

Thanks for the patch @paulocs

paulocs’s picture

But that's the idea of the issue, isn't it?

anjali rathod’s picture

Status: Needs review » Reviewed & tested by the community

The patch applied successfully and works as required.
Moving this ticket to RTBC.
Thanks!

quietone’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs work

Almost there, a few more things to do.

I've only skimmed the comments but I see that the code has not been reviewed. Setting to NW.

Since this is changing validation of user input there should be before and after screenshots in the issue summary.

paulocs’s picture

Issue summary: View changes
Status: Needs work » Needs review
StatusFileSize
new42.49 KB
new32.28 KB

Uploading screenshots and updating IS.

smustgrave’s picture

Status: Needs review » Needs work

Don't see a tests-only patch if we can get that please.

paulocs’s picture

StatusFileSize
new1.06 KB
lucassc’s picture

Status: Needs work » Needs review
lucassc’s picture

StatusFileSize
new6.62 KB
new568 bytes

Code looks good!

For ../core/modules/menu_ui/tests/src/Functional/MenuUiTest::testMenuName, maybe we don't need to assign $page in this line?
$page = $this->getSession()->getPage();

This is not used afterwards.

Adding patch #29 and interdiff for the consideration above in case it makes sense.

Manibharathi E R’s picture

StatusFileSize
new133.21 KB
new149.11 KB

Patch #29 Tested and Applied successfully on Drupal 9.5.x.

Before Patch:

Before-Patch

After Patch:

After-patch

charchil khandelwal’s picture

StatusFileSize
new22.33 KB
new21.31 KB

#29 Patch is working fine and shared the screenshot for the reference.

heni_deepak’s picture

Status: Needs review » Reviewed & tested by the community

#29 looks like working and green.
need to reviewed by community.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 29: 3111255-29.patch, failed testing. View results

lucassc’s picture

Status: Needs work » Needs review

I think it was a random fail of ckeditor5.

Thanks for the reviews @Manibharathi E R, @Charchil Khandelwal and @heni_deepak.

Patch works, but we still need a core review as @quietone said in #24:

I've only skimmed the comments but I see that the code has not been reviewed. Setting to NW.

heni_deepak’s picture

Yes, @lucassc the error coming form based class trait TestFileCreationTrait { }
and mean code is $this->getTestFiles('image')[0]->uri

that function need to update as per parent function.

I found this #3231694: TestFileCreationTrait::getTestFiles() has incorrect @return type

heni_deepak’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

This is related to #2920678: Add config validation for the allowed characters of machine names and #2869792: [meta] Add constraints to all config entity types

I feel that we should be able to call validate on config entities much the same way we can on content entities - that way we wouldn't need to have so much in the MenuForm class.

+++ b/core/modules/menu_ui/src/MenuForm.php
@@ -73,12 +74,15 @@ class MenuForm extends EntityForm {
-  public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuLinkTreeInterface $menu_tree, LinkGeneratorInterface $link_generator, MenuLinkContentStorageInterface $menu_link_content_storage) {
+  public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuLinkTreeInterface $menu_tree, LinkGeneratorInterface $link_generator, MenuLinkContentStorageInterface $menu_link_content_storage, TypedConfigManagerInterface $typed_config_manager) {
     $this->menuLinkManager = $menu_link_manager;
     $this->menuTree = $menu_tree;
     $this->linkGenerator = $link_generator;
     $this->menuLinkContentStorage = $menu_link_content_storage;
+    $this->typedConfigManager = $typed_config_manager;
   }

If we do do this we'll need to make the argument optional and issue a deprecation.

ghost of drupal past’s picture

I am not sure about this. I would guess it is possible with current code to fix the suggestions in an appropriate alter hook and so maybe people can have menus which will break on import after this? Is there a reason beyond the theme suggestion to disallow them? If not, then maybe fix that code?

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Needs work » Closed (outdated)

I tested this on 11.x, fresh standard install, today and was not able to reproduce the problem using the steps in the Issue Summary. Therefor closing as outdated.

If you are experiencing this problem on a supported version of Drupal reopen the issue, by setting the status to 'Active', and provide complete steps to reproduce the issue (starting from "Install Drupal core").

Thanks!