Looking for a good way to alter the desired translated link to a destination if it's different from what Linkit provides

Created on 3 August 2024, 6 months ago
Updated 20 September 2024, 4 months ago

In some cases, Linkit thinks that a link should be one way, but the website's logic would prefer another link. Here is an example:

  • Start with Drupal 10.2.4
  • drush site:install -y
  • mkdir -p modules/custom
  • cd modules/custom
  • git clone --branch '7.x' https://git.drupalcode.org/project/linkit.git
  • cd ../..
  • drush en -y linkit language content_translation
  • drush uli
  • Go to /admin/config/regional/language/add
  • Choose "Custom language"
  • Language code: en-gb
  • Language name: British English
  • Click "Add custom language"
  • /admin/structure/types/manage/page
  • In Language settings, check "Enable translation"
  • Save
  • /node/add/page
  • Title: English destination page
  • Save
  • Visit /en-gb/node/1, which is the British English version of /node/1, which in fact is identical to the base version.
  • Go to /admin/config/content/formats/manage/full_html
  • Select Linkit
  • Linkit profile: default
  • Check Linkit URL converter
  • Save configuration
  • /node/add/page
  • Title: Source
  • Text format: full HTML
  • Type "This link to node 1 will be processed by Linkit" in the body.
  • Select the text and click the link button
  • In link URL put /node/1
  • Save
  • Now we are in a situation where /en-gb/node/2 displays the same thing as /node/2, and /en-gb/node/1 displays the same thing as /node/1.
  • When you visit /en-gb/node/2, you will see a link to /node/1, not /en-gb/node/1.
  • If you want /en-gb/node/2 to display a link to /en-gb/node/1, you need to do two things: (1) /en-gb/node/1/translations/add/en/en-gb then save (2) /en-gb/node/2/translations/add/en/en-gb then save

The problem

For certain websites, the above behaviour may be reasonable; for other sites, it may be reasonable for a link on the page /en-gb/node/2 to link to /en-gb/node/1, even though neither node 1 or 2 have an explicit "en-gb" translation. For other sites with more complex language structures, one might have en, en-gb, en-ca, fr, fr-ca, fr-fr, es-mx... and there may be custom logic to implement (for example en-ca would revert to en, fr-ca would revert to fr, es-mx would revert to fr).

It should be noted that ./src/Plugin/Filter/LinkitFilter.php::process() takes the arguments $text, and $langcode, where $langcode is the language of the existing translation.

Therefore, in our example with node 2, if no en-gb translation exists, the filter process function will apply to node/2's English (original) version, and then the original English version of the the node will be displayed whether we are in /en, or /en-gb. (There cannot be two versions of the body text.)

My thought at this point is to modify the link in the frontend using JavaScript.

I shall post my findings as they come about.

💬 Support request
Status

Active

Version

7.0

Component

Code

Created by

🇨🇦Canada alberto56

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

Comments & Activities

Production build 0.71.5 2024