MenuItem route should not inherit language from menu

Created on 29 September 2023, about 1 year ago
Updated 18 January 2024, 11 months ago

Problem/Motivation

MenuItem's route entity is always in Menu's language regardless of request language.

Steps to reproduce

- Have multilanguage site. For example English default and German.
- Have your menu's language English
- Add menu link referencing a node.
- Enable translation for Menu links
- Create German translations of menu links.
- Create German translation for node referenced from menu link.
- Make request in German

menu(name: MAIN) {
    items {
      title
      url
      route {
        ... on RouteInternal {
          entity {
            ...on NodePage {
              title
            }
          }
        }
      }
    }
}

See that title and url are German but route > entity > title is in English.

This is caused by this code in MenusSchemaExtension.php:
First

// Set the menu language context.
$builder->context('langcode', $builder->callback(
  fn (?MenuInterface $menu) => $menu?->language()?->getId())
),

In our case it is always English regardless of request's language.
Then for MenuItem:route:

$builder->produce('url_or_redirect')
  ->map('path', $builder->fromParent())
  ->map('language', $builder->fromContext('langcode')),

In our case it is always English regardless of request's language.

Proposed resolution

For url_or_redirects's language get value from Url's option or just use request language.

Another option could be adding language argument to menu query.

🐛 Bug report
Status

Fixed

Version

2.1

Component

Code

Created by

🇧🇾Belarus Yury N

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024