2

I am using SEOmatic helper class to get localizedUrls which is making life much easier! I would like to only display the localizedurl if the current entry is enable on that site. Below is what I have, has anyone successful done this for craft 3?

{% set currentSite = craft.app.sites.currentSite %}

{% set currentSiteLanguage = currentSite['language'] %}

{% set localizedUrlsArray = seomatic.helper.getLocalizedUrls() %}


{% for localizedUrls in localizedUrlsArray %}

{% if currentSiteLanguage != localizedUrls['language'] %}

    {% if entry is defined %}

        {% if entry.enabledForSite %}

            <a href="{{ localizedUrls['url'] }}"> {{ localizedUrls['language'] }}</a>

        {% endif %}


    {% else %}

        <a href="{{ localizedUrls['url'] }}"> {{ localizedUrls['language'] }}</a>

    {% endif %}

{% endif %}

{% endfor %}
4
  • I'll have a look at this in detail in a bit, but something you can do is do a {{ dump(localizedUrlsArray) }} to look at the data that's being returned to you from seomatic.helper.getLocalizedUrls() to make sure it is what you think it is. Also you don't mention what is wrong or not working as expected with the code you've shown? Commented Apr 7, 2018 at 22:36
  • I tried your code... it seemed to work fine here, it outputted a link to the entry in another localization in the same Site Group. What is not working as you're expecting it to? Commented Apr 8, 2018 at 20:05
  • @andrew.welch What I would like to happen is if you go to an english entry, but that entry is not set to live on another site/language then not show the link. My code it shows the link regardless. I am 100% positive I'm not using enabledForSite for site correctly. Commented Apr 9, 2018 at 12:59
  • Oh... maybe you want to check on the .status to make sure it's live then? Commented Apr 9, 2018 at 16:42

1 Answer 1

1

I think I might finally have it working. If someone has a more elegant way to handle the index, tag and category I would love to see it!

        <!-- entry -->
        {% if entry is defined %}

            {% for siteForEntry in entry.getSupportedSites %}

                {% set site = craft.app.getSites.getSiteById(siteForEntry.siteId) %}

                {% if site.id != entry.siteId %}

                    {% set entryForOtherSite = craft.entries.id(entry.id).site(site).one() %}

                    {% if entryForOtherSite is not null %}

                        <li><a href="{{ entryForOtherSite.getUrl() }}">{{ site.name }}</a></li>

                    {% endif %}

                {% endif %}

            {% endfor %}

        <!-- category -->
        {% elseif category is defined %}

            {% set currentSite = craft.app.sites.currentSite %}

            {% set sites = craft.app.sites.getAllSites() %}

            {% for site in sites %}

                {% if site.id != currentSite.id %}

                    <li><a href="{{ site.baseUrl }}blog/category/{{craft.request.getLastSegment()}}">{{ site.name }}</a></li>

                {% endif %}

            {% endfor %}

        <!-- index and tags -->
        {% else %}

            {% set currentSite = craft.app.sites.currentSite %}

            {% set sites = craft.app.sites.getAllSites() %}

            {% for site in sites %}

                {% if site.id != currentSite.id %}

                    <li><a href="{{ site.baseUrl }}{% for segment in craft.request.getSegments() %}{{segment}}/{% endfor %}">{{ site.name }}</a></li>

                {% endif %}

            {% endfor %}

        {% endif %}
1

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.