Problem/Motivation
In multilingual sites it's currently not possible to select a specific translation for internal path's when creating links. For instance, I have a Dutch/English site:
- Node 1 is the homepage and has a translation for both languages.
- Node 2 is a Dutch node with a link field set to link to node 1.
- When showing the link on node 2, I want the link to go to the English translation of Node 1.
I ran into this while trying to find a solution for #2845245: Allow Redirecting to another language.
This seems to be related to the following issues:
#2462279: Language prefix for custom menu link paths are saved but not used - The menu link doesn't use the entered language prefix, having a seperate language selection field will make the destination language explicit.
#2466553: Untranslated menu items are displayed in menus - Menu links can have a language and it would be nice to only habe Dutch menu links show up in the Dutch menu. When you can have a Dutch menu link but link it to a English translation, this will allow for more flexibility.
#2144377: Entity reference autocomplete lists entity labels only in current content language - The entity_autocomplete form shows available entity titles in the translation of the current interface language. This doesn't mean you will actually get a link to that specific translation. It would help to explicitly choose the preferred language of the destination.
Proposed resolution
Allow a preferred language for link fields.
Remaining tasks
User interface changes
Optionally added select list for 'Preferred language' to link fields.
API changes
None
Data model changes
None
Comment | File | Size | Author |
---|---|---|---|
#42 | interdiff-2924653-39_42.txt | 935 bytes | Anchal_gupta |
#42 | 2924653-42.patch | 14.89 KB | Anchal_gupta |
#39 | link_attributes-3317051.patch | 844 bytes | Yatendra Singh |
#38 | 2924653-38.patch | 14.89 KB | hmendes |
| |||
#38 | interdiff_36-38.txt | 974 bytes | hmendes |
Comments
Comment #2
seanBPatch is attached. The language select field is disabled by default. This should not change anything for existing sites.
Comment #4
seanBAhh the schema..
Comment #6
seanBTried to fix the tests and added some more coverage for the new language selection field.
Comment #7
seanBThe default value for
$this->getFieldSetting('language')
is NULL for existing fields, so added a extra check for that.Comment #9
seanBReroll for 8.5 and 8.6 (applies to both).
Comment #11
seanBThis should fix the tests.
Comment #12
borisson_I think this needs a change notice. The code looks very good though.
Comment #13
lpeabody CreditAttribution: lpeabody commentedThis worked perfectly for our use case. We have a language selection modal which leverages the link field type to link to internal references in different languages. Thanks for this.
EDIT: Should specify, the patch from #7 was RTBCd.
Comment #15
borisson_Tagging to make sure that everyone knows what there is still to do to get this in.
Comment #16
yogeshmpawarComment #17
yogeshmpawarRe-rolled the #11 patch against 8.6.x branch so marking this issue for "Needs Review".
Comment #19
yareckon CreditAttribution: yareckon commentedAny chance we could call the flag whether specifying language is allowed something other than just "language"? We also have the actual specified language named "language" and it gets confusing. Could we call the flag "specify_language" or "set_language" or "allow_set_language" or someone has a better idea?
Comment #21
lpeabody CreditAttribution: lpeabody commentedSo, I feel like there needs to be a change here. I feel like there needs to be another option for "Use page language", and if that option is set then the language option is not sent along to the formatter (reverting the formatting to previous functionality where the language option was never passed to the URL builder).
Reason being, when this patch is applied, and the field settings are updated to "Optional", when re-saving old content 1 of several things can happen:
1) A language is picked and saved in the field's options.
2) Not specified (und) is written to options.
3) Not applicable (zxx) is written to options.
When you're running a site where ALL of the aliases have an assigned language this causes a problem because the language option is now always sent to the URL builder, which inevitably gets passed to the alias manager to get the alias for the path and the alias manager looks at one of two things when determining what alias to pick:
1) If there is a langcode passed, use that langcode.
2) Otherwise, get default language.
As soon as you save a field with this patch applied, a langcode will always be passed to the alias manager and your field will never render in the language of the page again.
The counter argument to this is, if you're editing the content you know what language you're editing the content in, so just set the language of the link as the same version of the language you're editing. Okay, but say you have hundreds of links that now have this option enabled for it, you are now asking the site builder to remember to set the link's preferred language to the language you're currently editing every single time. That's a lot to ask in my opinion.
I think the best way to make this backwards compatible with existing sites is to add a preferred language option of "Page language" or "Current language", something akin to that, and make that the default choice for the preferred language property. That will save people the headache of saving their content and all of a sudden their links on pages stop showing the alias of the page and show "node/325" instead.
In any case, if anyone needs clarification on what I'm trying to say, would love to clear anything up. It's the end of the day and I'm a little spent!
EDIT 1: One more point, I think this only needs to be done if the preferred language is set as optional. If it's required then it's required, no real argument there, you MUST pick a language. If it's optional though you should be able to ignore this setting altogether. If the property is disabled, then if there already exists a language option in the field's storage, the language option should be removed from storage.
Comment #22
lpeabody CreditAttribution: lpeabody commentedPatch for signaling to use current page's language, which actually defaults to removing the language option stored with the field. Effectively it reproduces the previous behavior.
This needs more work. It should ONLY be available if the link field is configured to optionally select preferred language. In addition, when the use page language checkbox is displayed it would be nice to leverage state api to hide the preferred language select.
Comment #25
seanBReroll for 8.8.x of #11.
Comment #27
lpeabody CreditAttribution: lpeabody commentedApplies to 8.9.x and explicitly checks for language being a valid index value, otherwise you get a notice which is breaking AJAX interactions on paragraphs.
Comment #29
seanBReroll of #27 for Drupal 9.1.x.
Comment #31
lpeabody CreditAttribution: lpeabody commentedSometimes the language option for a link field is an empty array, which evaluates to TRUE with isset.
Comment #32
3liRe-rolled #29 for Drupal 9.3
Comment #33
ayalon CreditAttribution: ayalon at Liip commentedHere is a reroll for 9.3.2:
Comment #34
ayalon CreditAttribution: ayalon at Liip commentedReroll 9.3.2
Comment #36
hmendes CreditAttribution: hmendes at CI&T commentedFixing deprecated code.
Comment #38
hmendes CreditAttribution: hmendes at CI&T commentedFixing tests with the missing schema.
Comment #39
Yatendra Singh CreditAttribution: Yatendra Singh as a volunteer commented#38 patch is working when we use link option for link type on Manage form display.
But when we select
'link (with attributes)'
, we need to apply a patch in link_attributes module .
Comment #40
Yatendra Singh CreditAttribution: Yatendra Singh as a volunteer commentedComment #41
Yatendra Singh CreditAttribution: Yatendra Singh as a volunteer commentedComment #42
Anchal_gupta CreditAttribution: Anchal_gupta at Srijan | A Material+ Company for Drupal India Association commentedI have uploaded the patch with fixed cs error