backdrop / backdrop-issues

Issue tracker for Backdrop core.
145 stars 40 forks source link

Translated content doesn't get aliased path in views title #6745

Closed olafgrabienski closed 2 weeks ago

olafgrabienski commented 3 weeks ago

Description of the bug

Links to translated content which is listed in views, e.g. on the "Manage content" page, don't point to the aliased path.

In other words: Path aliases in views work only, if interface and content language are the same. (See next comment.)

Steps To Reproduce

To reproduce the behavior:

  1. Enable Content Translation, Language and Locale from Backdrop core.
  2. Add a second language, i.e. German.
  3. Configure the Page content type, go to the tab "Multilingual support", and choose "Enabled, with translation".
  4. Create a page of title "Hello world", put some content in the body, set the language to English, and save the page. The page gets a URL like example.com/hello-world.
  5. Hit the "Translate" tab, and add a translation. The translated page gets a URL like example.com/de/hallo-welt.
  6. Switch the Backdrop interface back to English, using the language switcher in the Admin bar.
  7. Go to the Manage content page, and inspect the page title link paths of the pages created above.

Actual behavior

The path to the English page "Hello World" is the alias example.com/hello-world (as expected). The translation "Hallo Welt" doesn't link to an alias but to the node ID path, e.g. example.com/node/7.

Expected behavior

The link to the translation "Hallo Welt" points to the alias, e.g. example.com/de/hallo-welt.

olafgrabienski commented 3 weeks ago

Two more observations:

(1) For testing purposes, I've added a "Path" field to the Manage content view. This field is supposed to show the "aliased path to content". Results in the setup described above are /hello-world for the English page and /node/7 for the translation.

The problem seems to be how Views handles aliased paths, no matter if in a Path field or in a Title field with the option "Link this field to the original piece of content".

(2) When you switch the Backdrop interface from English to the other language (e.g. German) and visit the Manage content page again, the link to the German page points (correctly) to the path alias, and the English page link is missing the alias.

Conclusion: Path aliases in views work only if interface and content language are the same.

robertgarrigos commented 3 weeks ago

I can confirm this behavior. Although I had to set the detection method in /admin/config/regional/language/detection, I reproduced your steps.

English interface: only English content gets the right alias : Content___My_Backdrop_Site_-_Vivaldi

Catalan interface: only Catalan content gets the right alias : Cursor_and_Contingut___My_Backdrop_Site_-_Vivaldi

argiepiano commented 3 weeks ago

Found the bug. PR provided.

Explanation of the fix:

  1. Views creates links by using views_handler_field::render_as_link()
  2. That method expects the fully loaded language object to be placed in $alter['language']. This sends the language object to function l() to create the link based on the alias if available πŸ‘πŸ½
  3. However, both views_handler_field_node and views_handler_field_node_revision put the fully loaded language object inside key $this->options['alter']['langcode'] πŸ‘ŽπŸ½
  4. This causes render_as_link() to never find that language object, since it's placed under the wrong key

The solution is to modify views_handler_field_node and views_handler_field_node_revision so that it puts the fully loaded language object under key language (which, incidentally, is what Backdrop uses for language objects, as opposed to langcode, which is used for the string language code).

robertgarrigos commented 3 weeks ago

It seems that the problem is fixed partially. Indeed, the link for each content shown when hovering the node title looks right, now, but not the path field.

Fullscreen_1_11_24__21_38

argiepiano commented 3 weeks ago

OK, I'll take a look at that. I bet it's the same issue that I found with the link.

argiepiano commented 3 weeks ago

Actually it's a different problem. Since these are two distinct problems, can you please open a new issue for the path Views field?

olafgrabienski commented 3 weeks ago

can you please open a new issue for the path Views field?

Sure, I'll do that.

robertgarrigos commented 3 weeks ago

Then, PR works for me.

argiepiano commented 3 weeks ago

I have a fix for the other issue as well. I'll wait until you post the new issue.

olafgrabienski commented 3 weeks ago

New issue: https://github.com/backdrop/backdrop-issues/issues/6746

herbdool commented 3 weeks ago

LGTM

quicksketch commented 2 weeks ago

Thank you @argiepiano! This looks like a mistake after we globally replaced "language" with "langcode", but that conversion didn't happen within Views, so it remains "language". Thank you @olafgrabienski, @robertgarrigos, and @herbdool for verifying the solution. I merged https://github.com/backdrop/backdrop/pull/4895 into 1.x and 1.29.x.