1

I am trying to find a way to do the following:

  • Add languages to Drupal 9 menu items
  • Translate content/menu links
  • Provide (from the Main Navigation) a list of languages the current page is translated in

I was able to complete the first two, but I cannot see how to get a menu to list links that output the available language options for the current entity. I see that there is a Language Switcher block from core, but is there a way to make this appear baked into a Drupal menu itself?

enter image description here

4

1 Answer 1

0

You put the static infos in *.links.menu.yml where you also define dynamic menu link plugins, which provide the infos you can't put in a static yml. And the deriver to produce a plugin for each language. The local task example was just an example for the translation stuff.

A working static example:

mymodule.links.menu.yml

mymodule.language.top:
  title: 'EN'
  menu_name: main
  route_name: '<none>'
  expanded: true
  # Plugin class to override the title dynamically:
  # class: Drupal\mymodule\Plugin\Menu\TranslatedNodeTop
mymodule.language.item:
  title: 'Node 8 spanish'
  parent: mymodule.language.top
  route_name: entity.node.canonical
  route_parameters: { node: '8' }
  menu_name: main
  options:
    attributes:
      class:
        - foo-class
        - bar-class
  # deriver for installed languages
  deriver: Drupal\mymodule\Plugin\Derivative\TranslatedNodeTabDeriver
  # Plugin class extending MenuLinkDefault overriding static properties dynamically,
  # the route parameter from the current route match, the language from the deriver
  # the title from both and link attributes added for the current language:
  # class: Drupal\mymodule\Plugin\Menu\TranslatedNodeItem

You need to add the two dynamic plugins extending MenuLinkDefault. I've recycled the TranslatedNodeTabDeriver from How to get links of translated node edit page in the secondary tabs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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