3

I'm trying to translate the 'enabled' field of menu items in Drupal 8. So far I have written this code in MYMODULE.module file:

function MYMODULE_entity_base_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type) {
  if ($entity_type->id() == 'menu_link_content' && !empty($fields['enabled'])) {
    $fields['enabled']->setTranslatable(TRUE);
  }
  return $fields;
} 

This works fine and I am able to set the enabled field differently for different languages. eg: for the same menu item, I can check the 'enabled' field for English, but uncheck the 'enabled' field for French.

But it does not work as expected. It hides the menu item for all languages. Should I use hook_preprocess() or hook_preprocess_menu() or some other hook for the menu item to show/hide based on 'enabled' field in different languages? Can someone give me some hints on this?

0
2

For what you are trying to achieve I found out you can use the Menu Manipulator module. This module initially aimed to fix [Issue] Untranslated menu items are displayed in menus.

Notes

This module initially aims to fix this core issue with unstranslated menu items.


Having this module installed only menu items that are available in the current language will be displayed. If there's a menu item you don't need in a certain language, remove it or remove its translation.

To make this work properly, ensure that your menu, the menu title and "Show language selector on create and edit pages" is enabled for your menu under https://example.com/admin/config/regional/content-language.

  • Having a menu item you want only to be shown on the French site, select French as its language, and don't translate it.
  • Having a menu item to be shown only on the English site, select English as its language, and don't translate it.
  • Having a menu item to be shown on both sites, add it in one language and simply translate it.
0

Adding this code in Drupal/core/modules/menu_link_content/Plugin/Menu/MenuLinkContent.php made the functionality work exactly as expected. (Menu items were disabled in one language but enabled in another)

public function isEnabled() {

        if ($this->languageManager->isMultilingual()) {
          return $this->getEntity()->isEnabled();
        }
        return $this->pluginDefinition['enabled'];
    }

But Im not sure of how to extend this class in my custom module and make it visible to drupal.. Any ideas on that?

4
  • Uh, though I like the outcome I'd actually need to -1 this as hacking the core never is a good idea...
    – leymannx
    May 31 '19 at 14:54
  • Uh yeah, i do not want to hack the core either, whats the best way to add this method in custom moduleby extending the plugin MenuLinkContent.php class?
    – user93578
    May 31 '19 at 15:02
  • Unsure about that but stackoverflow.com/a/47940032/2199525 suggests to rebuild the whole menu in a preprocess hook while adding another manipulator to the tree...
    – leymannx
    Jul 23 '19 at 12:29
  • I tried that but couldn't find the language-specific enabled state in the single links. ->isEnabled() only uses the global state which gets updated with every change, no matter what language.
    – leymannx
    Jul 26 '19 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.