6

I'm having trouble outputting a list of tags that only includes tags from the current locale.

Here's the setup:

  1. I have a site with two locales: english and spanish
  2. I have a section with handle newsAndEvents
  3. I have a tag field with handle newsTags on entries in that section that is set to translatable to allow my clients to assign locale-specific tags to the entries
  4. I have an entry in that section created with a published version for each of my locales. In the english version, I've added a tag "Turtles" to the entry. In the spanish version, I've added a tag "Tortugas" to the entry.
  5. On the landing page of my news section, I am displaying a list of recent tags in a sidebar. Here's the code I'm using to do it:

    {# grab the entries in the specific locale #}
    {% set entries = craft.entries.find({section: 'newsAndEvents', locale: craft.locale}) %}
    
    {# Grab the related tags for those entries #}
    {% set tags = craft.tags.relatedTo(entries) %}
    
    {# Build the list #}
    {% if tags | length %}
      <ul class="tag-list">
      {% for tag in tags %}
        <li><a class="tag" href="#">{{ tag.name }}</a></li>
      {% endfor %}
      </ul>
    {% endif %}
    
  6. The problem is, when I'm looking at the english version of my landing page, I get both "Tortugas" and "Turtles" in the list output by the code above. What I want is to only see the tags on english versions entries on the english landing page; and the tags on spanish versions of entries on the spanish landing page.

Some additional info:

  • If an entry has only one published version, english for example, any tags I assign to that version do not appear on the spanish landing page, as would be expected.

  • On my entry detail pages, I'm using similar code to generate a list of tags assigned to the current entry, and the output is what I expect: english versions only show tags assigned to english versions, not to both english and spanish like on the landing page.

What am I missing?

6

Brandon at P&T was able to fill in the piece I was missing. He said:

To only get tags that were related from a specific locale, you just need to specify that in your relatedTo param:

    {% set tags = craft.tags.relatedTo({
      sourceElement: entries,
      sourceLocale: craft.locale
    }) %}

With the addition of order('dateCreated desc').limit(10) to put some additional constraints on what I was getting back, this solved it for me.

0

I think this is actually two problems you have, Adam.

Isn't dateCreated – which you are now using with the code you provided – something different to "recently used", your actual plan to filter your tags by? I don't know it there's a built in way to get the therefore necessary info, the "entry-tag-relation creation date".

You could try dateModified on the tag model, but I'm not sure if this works (maybe worth a feature request though? → support@buildwithcraft.com or "Craft: Feedback and Ideas" section on g+).

  • You're right carlcs: dateCreated is not exactly what I would want. What I really want is "date last used or created, whichever is newer." Thanks for the feature request link. I'll use it. – Adam Snetman Jul 31 '14 at 12:01
  • Did you try dateModified desc? That'd only work if a tag is updated in the DB after each new relation registered on it. Maybe worth a try.. – carlcs Jul 31 '14 at 12:05
  • I thought of that and did try it, but from what I was getting back, it didn't appear that Craft considers applying a tag as a modification. – Adam Snetman Jul 31 '14 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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