Score:1

Dynamic language links from menu?

in flag

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

cn flag
I don't think this has been fully solved in core yet, there are modules to help like https://www.drupal.org/project/menu_block_current_language, https://www.drupal.org/project/menu_manipulator, https://www.drupal.org/project/menu_multilingual
Kevin avatar
in flag
Hmmm... there must be a way to mimic this. I'll take a look around
4uk4 avatar
cn flag
For a code solution you would need a dynamic menu link plugin pointing at the current node derived for each language. Like this example for local tasks https://drupal.stackexchange.com/questions/292739/how-to-get-links-of-translated-node-edit-page-in-the-secondary-tabs
Kevin avatar
in flag
I see - that would work even if its not a local task? Like how would you get it appended to "Main Navigation"?
Score:0
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.