5

I made a content ( type = article). I made a view ( list of articles) and i add link to content. I activate fr language in the site => so

www.example.com/liste-des-articles => english page.

www.example.com/fr/liste-des-articles => french page.

My problem is the link to content doesn't work correcly in french => when i click to it => redirection to english page.

what shall i do ??

Thanks for the response. enter image description here

1

7 Answers 7

6

One way to solve this would be to construct the "Link to content" field on your own, using Views. You will need to:

  1. Add 2 fields to your view:
    • a "Content: Node ID (Node ID)" field
    • a "Content: Translation language (Translation language)" field.
  2. Exclude the "Content: Node ID" field from display.
  3. Rewrite the "Content: Translation Language" field as a custom link.
    The text in the "Rewriting" would look something like this:
    Link path: {{ langcode_value }}/node/{{ nid }}

enter image description here

Hope this helps!

4
  • I came across your answer after coming to the same conclusion but I wonder if the latest version of Drupal 8 has changed the Translation Language views element. According to my site, in my Translation Language element, I have {{ langcode__value }} and I have {{ langcode }}. In my solution, I rewrite the Translation Language element and use the langcode__value to set a twig variable so that I can do some further processing (ie: remove the default value from being rendered). I also needed to set the rewrite results on my Translation Language element to remove white space. Commented May 10, 2018 at 20:59
  • That doesn't work. In english you will have for example en/node/8923, and this is a wrong drupal url...
    – jBlobsmith
    Commented Nov 1, 2018 at 15:26
  • You may be right @jBlobsmith ; however, setting your URLs to be served as /en/BLA_BLA is a matter of configuration, Drupal doesn't discourage or stop you from doing that, and in many ways provides more "generic" URLs (English being the default language, is not necessarily self-explanatory for non-english native speakers). Short version: The /en/ prefixed URL pattern is not wrong, as long as the english language configuration is updated from Drupal's defaults. Commented Nov 1, 2018 at 17:21
  • I added one more step. 3. Add a "Content: Title" field. Rewrite the "Content: title" field as a custom link. and use this. {{ nid }} And my links and Titles look good. The I removed both of these fields "Content: Node ID (Node ID)" field and "Content: Translation language (Translation language)" field. And added the "Content: Title" field without any rewriting and it works just the same. Commented Dec 11, 2018 at 16:40
3

Until the core issue gets resolved here is a SIMPLE workaround that produces aliased URL

  1. add ID as field and exclude from display
  2. use the following code whereever you want the translated node url

    {{ path('entity.node.canonical', {'node': nid}) }}

1
  • Worked for me! I basically took the link to content and set it up in the following way: puu.sh/DUjyy/1087eb1eda.png I exclude the "link to content" and output all fields together with a global text rewrite output.
    – Morten H
    Commented Jul 18, 2019 at 11:33
2

it seems to be a bug in Drupal 8.3 views module at the time

but based on @Stefanos answer I suggest using Content: Path field and then exclude it from display, following Stefanos answer instead of:

Link path: {{ langcode_value }}/node/{{ nid }}

you can just try {{ path }}

it's better this way just too keep your URL aliases working

1
  • 2
    I believe {{ path }} is no longer available as of Drupal 8.5.x so until the issue is resolved our best option is to use the lang code. Commented May 10, 2018 at 20:31
2

The solution of @Sohail is better, because it is generating the URL Alias of a content instead of the poor "/en/node/1" Drupal native link.

However, if you need to also translate the link text, and are not afraid of editing your twig template, you can use the twig path() function :

  • Add the nid (Content ID) field to your view, and exclude it from display
  • Edit your fields template [views-view-fields.html.twig], using overriding if possible
  • Add the following code for your link :

    < a href="{{ path('entity.node.canonical', {'node': fields.nid.content|striptags|trim }) }}">{% trans %}Text to translate{% endtrans %}

Infos :

  • The {% trans %}XXX{% endtrans %} twig block allow string translation.
  • The fields.FIELD_NAME.content allow you to print only one field value
  • The striptags filter is to avoid HTML tags (nice if you use theme debug)
  • The trim filter is to remove all spaces left
1
  • Actually no. the solution presented by @Stefanos Petrakis is probably the best/simplest workaround rather than {{ path }} because Drupal no longer provides the content:path field (deprecated). Furthermore /en/node/1 will be automatically translated by Drupal's routing into whatever the alias is. Commented May 10, 2018 at 21:24
1

Currentlhy there seems to be a working patch about this core issue

Make sure the patch from comment #24 is the latest working patch and apply as follows using drush

cd DRUPAL_ROOT_DIR
wget https://www.drupal.org/files/issues/2018-08-06/2877994-57.patch
git apply -v 2877994-57.patch
0

Some of us have multilingual sites where the main language uses no language code in the URL, but translations do. In such cases the /{{ langcode }}/node/{{ nid }} solution doesn't work.

Currently in Drupal 8.5 Views won't let you select that Title field's URL in a rewrite, but you can get around this with JavaScript and CSS. You then just hide that linked Title field with CSS, and use JS to set link 'read more' text to that linked Title field's URL. For example:

  1. Add a linked Title field (rather than "Link to Content"), and then exclude it.
  2. Add a Custom HTML field. Let's suppose it has a field class of views-field-nothing
  3. Add JavaScript to run on .ready:

    $(".some-custom-class-view .views-row").each(function(){ var thislink = $(this).find(".views-field-title a").attr("href"); $(this).find(".views-field-title a").hide(); $(this).find(".views-field-nothing").(''); });

  4. Add CSS: .some-custom-class-view .views-field-title{display: none;}

1
  • To get around the default language issue just write a twig conditional in your rewriting interface for the language field. I'll {% set langprefix = langcode__value %} then I'll put an {% if langprefix != "en" %} {{ langprefix }} {% endif %}. My link looks like: {{ langcode }}/node/{{ nid }} Commented May 10, 2018 at 20:36
0

There's a bug in core that shows a link to the same language when your label field (title/name) isn't translatable.

See https://www.drupal.org/project/drupal/issues/2447821

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.