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

CommentFileSizeAuthor
#42 interdiff-2924653-39_42.txt935 bytesAnchal_gupta
#42 2924653-42.patch14.89 KBAnchal_gupta
#40 2924653-39.patch14.86 KBYatendra Singh
#39 link_attributes-3317051.patch844 bytesYatendra Singh
#38 2924653-38.patch14.89 KBhmendes
#38 interdiff_36-38.txt974 byteshmendes
#36 2924653-36.patch13.99 KBhmendes
#36 interdiff_33-36.txt4.87 KBhmendes
#34 2924653-33.patch14.95 KBayalon
#32 2924653-32.patch13.63 KB3li
#31 interdiff-27_31.patch1.45 KBlpeabody
#31 2924653-8.9.x-31.patch14 KBlpeabody
#29 2924653-29.patch13.85 KBseanB
#27 interdiff-25-27.txt629 byteslpeabody
#27 2924653-27.patch13.87 KBlpeabody
#25 2924653-25.patch13.85 KBseanB
#22 interdiff-17-22.txt4 KBlpeabody
#22 2924653-22.patch14.65 KBlpeabody
#17 2924653-17.patch13.78 KByogeshmpawar
#11 interdiff-9-11.txt1.17 KBseanB
#11 2924653-11.patch13.78 KBseanB
#9 2924653-9-reroll-8.5-8.6.patch12.61 KBseanB
#7 interdiff-6-7.txt882 bytesseanB
#7 2924653-7.patch12.71 KBseanB
#6 interdiff-4-6.txt9.87 KBseanB
#6 2924653-6.patch12.65 KBseanB
#4 interdiff-2-4.txt388 bytesseanB
#4 2924653-4.patch4.34 KBseanB
#2 2924653-2.patch3.85 KBseanB
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

seanB created an issue. See original summary.

seanB’s picture

Status: Active » Needs review
FileSize
3.85 KB

Patch is attached. The language select field is disabled by default. This should not change anything for existing sites.

Status: Needs review » Needs work

The last submitted patch, 2: 2924653-2.patch, failed testing. View results

seanB’s picture

Status: Needs work » Needs review
FileSize
4.34 KB
388 bytes

Ahh the schema..

Status: Needs review » Needs work

The last submitted patch, 4: 2924653-4.patch, failed testing. View results

seanB’s picture

Status: Needs work » Needs review
FileSize
12.65 KB
9.87 KB

Tried to fix the tests and added some more coverage for the new language selection field.

seanB’s picture

The default value for $this->getFieldSetting('language') is NULL for existing fields, so added a extra check for that.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

seanB’s picture

Reroll for 8.5 and 8.6 (applies to both).

Status: Needs review » Needs work

The last submitted patch, 9: 2924653-9-reroll-8.5-8.6.patch, failed testing. View results

seanB’s picture

Status: Needs work » Needs review
FileSize
13.78 KB
1.17 KB

This should fix the tests.

borisson_’s picture

I think this needs a change notice. The code looks very good though.

lpeabody’s picture

Status: Needs review » Reviewed & tested by the community

This 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.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: 2924653-11.patch, failed testing. View results

borisson_’s picture

Tagging to make sure that everyone knows what there is still to do to get this in.

yogeshmpawar’s picture

Assigned: Unassigned » yogeshmpawar
yogeshmpawar’s picture

Assigned: yogeshmpawar » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
13.78 KB

Re-rolled the #11 patch against 8.6.x branch so marking this issue for "Needs Review".

Status: Needs review » Needs work

The last submitted patch, 17: 2924653-17.patch, failed testing. View results

yareckon’s picture

Any 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?

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

lpeabody’s picture

So, 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.

lpeabody’s picture

FileSize
14.65 KB
4 KB

Patch 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.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

seanB’s picture

Reroll for 8.8.x of #11.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

lpeabody’s picture

FileSize
13.87 KB
629 bytes

Applies 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.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

seanB’s picture

Reroll of #27 for Drupal 9.1.x.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

lpeabody’s picture

Version: 9.3.x-dev » 10.0.x-dev
FileSize
14 KB
1.45 KB

Sometimes the language option for a link field is an empty array, which evaluates to TRUE with isset.

3li’s picture

Re-rolled #29 for Drupal 9.3

ayalon’s picture

Status: Needs work » Reviewed & tested by the community

Here is a reroll for 9.3.2:

ayalon’s picture

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 34: 2924653-33.patch, failed testing. View results

hmendes’s picture

Status: Needs work » Needs review
FileSize
4.87 KB
13.99 KB

Fixing deprecated code.

Status: Needs review » Needs work

The last submitted patch, 36: 2924653-36.patch, failed testing. View results

hmendes’s picture

Status: Needs work » Needs review
FileSize
974 bytes
14.89 KB

Fixing tests with the missing schema.

Yatendra Singh’s picture

FileSize
844 bytes

#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 .

Yatendra Singh’s picture

Yatendra Singh’s picture

Anchal_gupta’s picture

I have uploaded the patch with fixed cs error

Status: Needs review » Needs work

The last submitted patch, 42: 2924653-42.patch, failed testing. View results

Version: 10.0.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.