sulu / SuluArticleBundle

Bundle for managing localized content-rich entities like blog-posts in the Sulu content management system
MIT License
57 stars 77 forks source link

<link rel="alternate" href="[post_url]" hreflang="[locale]"> tags not generating correctly after upgrading to version 2.2.0 #525

Closed alexandrin-rus closed 3 years ago

alexandrin-rus commented 3 years ago
Q A
Bug? yes
SuluArticleBundle Version 2.2.0
Sulu Version 2.2.1

Actual Behavior

We are running a blog on the sulu article bundle serving 4 different locales. After upgrading the article bundle, we noticed the <link rel="alternate" href="[url to the same post in other languages]" hreflang="[locale]"/> are not populating correctly anymore.

We are seeing the tag populating correctly only for the currently selected locale. The tags are present for the other locales, but point to the root domain with locale segment only.

Assuming english is currently selected we get:

<link rel="alternate" href="[base_url]/post-slug" hreflang="en"/> <link rel="alternate" href="[base_url]/pl" hreflang="pl"/> <link rel="alternate" href="[base_url]/cs" hreflang="cs"/> <link rel="alternate" href="[base_url]/de" hreflang="de"/>

Expected Behavior

<link rel="alternate" href="[base_url]/post-slug" hreflang="en"/> <link rel="alternate" href="[base_url]/pl/polish-post-slug" hreflang="pl"/> <link rel="alternate" href="[base_url]/cs/czech-post-slug" hreflang="cs"/> <link rel="alternate" href="[base_url]/de/german-post-slug" hreflang="de"/>

Downgrading to version 2.1.3 fixed the issue for the moment.

Thank you.

niklasnatter commented 3 years ago

Thanks for reporting this. How exactly are you rendering this? Can you please provide a reproducible example?

alexandrin-rus commented 3 years ago

I am including the seo extension inside of my base template:

{% include '@SuluWebsite/Extension/seo.html.twig' with {
      seo: extension.seo|default(seo|default([])),
      content: content|default([]),
      urls: urls|default([]),
      shadowBaseLocale: shadowBaseLocale|default(),
      defaultLocale: request.defaultLocale|default('en')
} %}

As far as I can see, the link tags are rendered inside the @SuluWebsite/Extension/seo.html.twig

The code responsible for rendering those tags is:

{%- block urls -%}
    {#- when only one language do not show alternative -#}
    {%- if localizations|length > 1 -%}
        {%- for localization in localizations -%}
            {%- if defaultLocale == localization.locale -%}
                <link rel="alternate" href="{{ localization.url }}" hreflang="x-default"/>
            {%- endif -%}
            <link rel="alternate" href="{{ localization.url }}" hreflang="{{ localization.locale|replace({'_': '-'}) }}"/>
        {%- endfor -%}
    {%- endif -%}
{%- endblock -%}

I'm not sure how I can provide a complete reproductible example.

niklasnatter commented 3 years ago

That is already enough to reproduce it for me, thank you! 🙂

niklasnatter commented 3 years ago

Can you test if #526 fixes your issue?

alexandrin-rus commented 3 years ago

Hey, I've tested the commit, but it still does not work as expected. I do get a different behaviour:

Now I am seeing the following format:

<link rel="alternate" href="/blog/[en-post-slug]" hreflang="en"/> <link rel="alternate" href="/blog/[de-post-slug]" hreflang="de"/> <link rel="alternate" href="/blog/[pl-post-slug]" hreflang="pl"/> <link rel="alternate" href="/blog/[cs-post-slug]" hreflang="cs"/>

All urls are relative and are missing the locale segment.

Expected:

<link rel="alternate" href="[base_url]/blog/[en-post-slug]" hreflang="en"/> <link rel="alternate" href="[base_url]/pl/blog/[pl-post-slug]" hreflang="pl"/> <link rel="alternate" href="[base_url]/cs/blog/[cs-post-slug]" hreflang="cs"/> <link rel="alternate" href="[base_url]/de/blog/[de-post-slug]" hreflang="de"/>

It should generate absolute urls including the locale segment.

Thank you for looking into it.

niklasnatter commented 3 years ago

~Are you sure that the output is like that on 2.1.3 when using the same version of Sulu?~ Can you try again?

alexandrin-rus commented 3 years ago

Yes, I have reverted back to 2.1.3 and it is generating absolute urls. I only changed the sulu-article-bundle version.

niklasnatter commented 3 years ago

You are right, I already updated the answer above 🙂
Can you test #526 again?

alexandrin-rus commented 3 years ago

Yes, now I get the expected result. Thank you.

niklasnatter commented 3 years ago

Fixed by #526. Thanks for reporting!