Problem/Motivation
Menu link URLs are not translatable. This is a problem regarding external URLs. Currently the only solution seems to be making a duplicate menu for the other language.
Proposed resolution
Make menu link URL translatable.
| Comment | File | Size | Author |
|---|---|---|---|
| #42 | menu_link_url_should_be-2595839-40-reroll-drupal-11-2-0.patch | 14.88 KB | gilles_webstanz |
| #41 | menu_link_url_should_be-2595839-40.patch | 3.32 KB | gaurav_manerkar |
| #13 | interdiff.txt | 2.14 KB | jsbalsera |
| #3 | menu-link-url-should-be-translatable-2595839-3.patch | 1.9 KB | juhog |
Issue fork drupal-2595839
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
Comment #2
juhog commentedComment #3
juhog commentedMakes the URL translatable pretty much the same way as title and description are translatable.
Comment #4
lauriiiComment #6
dawehnerThe problem is, its not that easy, when the URL is translatable, you also need multiple entries in the menu_tree table, you need to take care of multilingual etc. and boom it will get hard.
Comment #7
juhog commentedAbout menu_tree table:
I'm not sure how exactly this table is used, but isn't there a similiar problem about the title?
For example, if I create a menu link in English and then I translate it to Finnish, in menu_tree table I will have just one row and the title of that row is like this:
s:17:"Menu link finnish";
There's no trace of the English title in menu_tree table.
Comment #8
dawehnerNo, its not. For static menu links we store the
TranslatableStringobject.For manually created menu links we have some logic on render time for that:
\Drupal\menu_link_content\Plugin\Menu\MenuLinkContent::getTitleThe problem though is that in case your menu link URLs are translatable queries against the {menu_tree} table against the
URL/routeneeds to behave differently. This is the main difference, you don't filter by the title.Do you mind explain how general usecase, I think we should more try to solve that, I hope better hope in that.
Comment #9
lauriiiGeneral usecase is when you try to link something to external service, you need to be able to set different URL for different languages because of the internal link generator is unable to create them.
Comment #10
dawehnerOkay well for external URLs it is easier indeed, maybe for those we could actually allow it when we have all the various validation in place.
Comment #11
dawehnerThis is certainly a feature request for 8.1.x now.
Comment #13
jsbalseraHi everyone,
I have the need for this in one project, so I used the patch in #3, but also tried to add some validations to ensure that only external URLs can be translated. Then, when we add a translation and edit the original, I don't know what should happen if we try to change the link to internal, or if we try to add an internal link to the translation.
For this project it was enough to make the internal links always internal, and the externals also always externals, but I'm sure that's not the desired solution. Also, I'm not sure that I'm doing this in the right place. Anyway, I upload the patch in case someone can find it useful.
Comment #16
ErnoVanhala commentedFor me it was big surprise, that I cannot translate menu item urls. Now I need to build huge menus (including mainly external links) twice to get the wanted behaviour.
So +1 if someone creates solutions to this :)
Comment #18
jlbellidoRerolled #13 to 8.4.x
Comment #22
unrealauk commentedI got the error with old links. I can't change internal URL to external URL its validation issue. So I removed this part from the patch.
Please review it.
Comment #32
hikkypo commentedI used patch 19 from #22 on drupal version 8.9.14
The patch almost works. After I save my menu item with an external link the text (all translations) will be removed from the url field and it will save the content separate from the original language. However I had the following issues with the current version of this patch.
1. This only worked on the translations but the original language kept being for all translations so changing and saving the original item would always overwrite all other languages.
2. You can technically now fill in an external url and then save the page. Then you can fill in an internal link and it will save the translation with this link.
So there should either be some ajax call which i think makes more sense because then you can swap between internal and external and/or on submit there should be a check to make sure the link being saved is still internal or external. And the original translation should be translatable. On top of that it needs to be updated for the newer drupal versions and tests need fixing.
Comment #33
seyfettinkahveci commentedHi,
There was a multisite build for core 9.4. One of these installations was not multilingual. Patch #22 was giving the following error. I updated the patch as attached.
InvalidArgumentException: The URI '' is invalid. You must use a valid URI scheme.Array ( ) in Drupal\Core\Url::fromUri() (line 296 of /var/www/html/web/core/lib/Drupal/Core/Url.php).Comment #38
akalam commentedI've created a MR rerolling the patch from #33 to the latest Drupal 11.x release. I'm attaching also a static patch to apply safely from composer
Comment #39
smustgrave commentedBelieve this feature will need test coverage.
Have not reviewed
Comment #40
gaurav_manerkar commentedIssue:
When
linkfield is made untranslatable for a particular menu type from/admin/config/regional/content-language, I get the following error:Error: Call to undefined method Drupal\Core\Field\Entity\BaseFieldOverride::setItemDefinition() in Drupal\menu_link_content\Entity\MenuLinkContent->setTranslationConstraints() (line 444 of /var/www/html/docroot/core/modules/menu_link_content/src/Entity/MenuLinkContent.php).While debugging, i found that when
linkfield is translatable, the$item_definitionobject is an instance of the classDrupal\Core\Field\BaseFieldDefinitionand when it is non-translatable, the object is an instance of the classDrupal\Core\Field\Entity\BaseFieldOverridewhere thesetItemDefinitionmethod is not defined.We should add an early return if the
linkfield is non-translable in the field configuration.Comment #41
gaurav_manerkar commentedFix for issue described in #40
Comment #42
gilles_webstanz commentedI made a reroll patch for the drupal core version 11.2.0