Problem/Motivation

In the current implementation, if a page does not have a translation, it is still included in the menu.

Steps to reproduce

Create a page with a translation (eg. EN and FR)
Create a page without a translation (eg. only EN)

If you are on FR, only the page with FR should appear. Instead they both appear.

Proposed resolution

Do not include the languages that do not have a translation for current language.

Remaining tasks

  1. Wait for the core issue #2466553: Untranslated menu items are displayed in menus to be fixed.
  2. Test and commit the patch on this issue
  3. Write some tests to confirm the issue is fixed (should fail currently, will pass once core issue and patch are included)
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

adrianpintilie created an issue. See original summary.

heikkiy’s picture

This issue is related to the core issue https://www.drupal.org/project/drupal/issues/2466553.

We also noticed this when we applied the Menu multilingual module which fixes the core menu block and blocks using Menu block (https://www.drupal.org/project/menu_block) module. Those blocks were fixed but the blocks using this module were not.

There are a couple ways how to solve this. We could use for example https://www.drupal.org/project/menu_multilingual to write a plugin for this module. Other option would be add a dependency to https://www.drupal.org/project/menu_manipulator which allows you to filter out untranslated menu items.

But as said in the Menu manipulator module page, all these fixes are related to the core issue which actually might now have a working patch for D9. Its still in Needs work state but there could be working patches also.

tanc’s picture

Thank you for reporting this issue. It would be ideal if it could be fixed in core as I don't want to add workarounds within this module for core issues. If there is a 3rd party module that solves the issue and doesn't require anything from this module that would be the way to go until the problem is fixed in core (or use a core patch).

heikkiy’s picture

I tested this issue a bit more with the core patch from https://www.drupal.org/project/drupal/issues/2466553. It seems like the core patch will resolve the issue for the core menu block but not for this module.

Also I found out that even unpublishing a translation for a node won't hide it from the responsive menu. Clearing caches etc. did not resolve the issue. Clicking on the menu item took me to the "You are not authorized to access this page." page which is definitely not ideal.

I tried unpublishing both the translated node and the translated menu item but still it was visible in the responsive menu.

heikkiy’s picture

Here is a patch which takes advantage of the new core menu manipulator to filter out menu items by the current language. Tested that it works if you have the patch applied from the core issue https://www.drupal.org/project/drupal/issues/2466553.

heikkiy’s picture

tanc’s picture

Fantastic! Can we check if the manipulator exists before applying it so that we can be backwards compatible? Or is there no error if the core patch hasn't been applied and we try to apply the manipulator?

tanc’s picture

Maybe by adding the new callable to the array after the initial array is created and wrapped in a condition like: if (class_exists('LanguageMenuLinkManipulator'))

heikkiy’s picture

Very good point. I tested adding the class_exists() or is_callable() checks but they don't work probably because the classes are not injected but used through a service.

What worked was adding a hasService() check for the new LanguageMenuLinkManipulator service and then adding the new manipulator to the original manipulator array.

I tested this with and without the core patch. No errors without the core patch but of course the language items are not filtered then correctly as expected.

Here is an updated patch.

tanc’s picture

Thanks for providing the patch @HeikkiY. It's probably sensible for us to wait until the core patch is stable or even committed in case something like the class name changes. The good thing is that someone that needs this functionality now can patch both core and this module and should have a working set up.

I'll leave this issue open and we can check in once the core issue develops. Thanks again!

tanc’s picture