Creating link tags with hreflang attributes in CraftCMS

I want to preface this with the following; I think CraftCMS is a poor CMS. I dislike many things that it does and cannot recommend it as a professional CMS. Having said, that, I am working with it a lot these days at work and recently had to add link tags with hreflang attributes to the head. For some reason the CMS does not do this for you. SproutSEO does not do this either.

{% for locale in craft.i18n.getSiteLocales() %}
  {% if entry is defined %}
    {% set localeEntry = craft.entries.id(entry.id).locale(locale).first %}
    {% if localeEntry.locale == locale %}
      <link rel="alternate" hreflang="{% if locale == 'en_us' %}x-default{% else %}{{ locale|replace('_','-') }}{% endif %}" href="{{ craft.config.siteUrl[locale.getId()] }}/{{ (localeEntry.uri != '__home__' ? localeEntry.uri) }}">
    {% endif %}
  {% else %}
    <link rel="alternate" hreflang="{% if locale == 'en_us' %}x-default{% else %}{{ locale|replace('_','-') }}{% endif %}" href="{{ craft.config.siteUrl[locale.getId()] }}/{{ craft.request.getPath() }}">
  {% endif %}
{% endfor %}

What this Twig snippet is doing is iterating over all of the defined site locales, looks up the current entries locale information to get the current entries specific locale slug. We grab the SiteURL parameter defined in the general config file and render the url with the locale specific entry slug and site url. And since this particular site wants to make en-us the default, we check for that and render accordingly if the locale matches. If there is no entry, we just take the request path and locale specific site url to render the url.

General.php config example:

    '*' => array(
        'siteUrl'  => array(
          'en_us' => 'http://us.example.org',
          'es_es' => 'http://es.example.org',
          'de_de' => 'http://de.example.org'
        ),
    )

It really bugs me that there isn't a Twig variable or method in Craft CMS to do this for me.

Did you like this post? Let me know by sending me a message. Is there a topic you would like me to cover? Let me know about that too. I look forward to hearing from you!

Let's Connect!