Cms: Taxonomy date-based filters don’t work

Created on 14 Sep 2020  Â·  10Comments  Â·  Source: statamic/cms

Bug Description

The taxonomy parameters state that since and until should filter taxonomy dates, but these don’t appear to be working.

How to Reproduce

  1. I have a collection of date-ordered entries that are tagged with terms from a fonts taxonomy
  2. Adding since or until parameters to {{ taxonomy:fonts }} doesn’t restrict the output to those dates

Extra Detail

Here’s the template code:

{{ taxonomy:fonts sort="entries_count:desc" limit="10" since="january 1st, 2019" until="december 31st, 2019" title:not="georgia" }}
    <li><a href="{{ slug }}">{{ title }} ({{ entries_count }})</a></li>         
{{ /taxonomy:fonts }}

Environment

Statamic 3.0.7 Solo
Laravel 7.27.0
PHP 7.4.10
statamic/ssg 0.2.0

Install method (choose one):
Migration from V2.

bug taxonomies

All 10 comments

I could be mistaken here, but I think the since and until parameters are only applied to {{ entries }} that you want to fetch within a certain taxonomy. At least that's what the docs state:

Limits the date the earliest point in time from which date-based entries should be fetched.

However I couldn't get that to work either. So something is definitely wrong here or the docs need some more love. I've zipped my experiment up so it's easy for core team to test this out.

Thanks, @robdekort, you legend! 🙌

Would this instance not class as _date-based entry_ as the collection entries associated to the taxonomy are date-based? I might be misunderstanding that...

I'm not quite sure tbh. However you're not fetching entries in your example, just the taxonomies. So inside your tag you can also use {{ entries }}{{ /entries }} to fetch tagged entries for each taxonomy. The way I read the docs the since and until parameters should apply to those {{ entries }}. But that however doesn't work. So if I understand correctly (which is rare) there definitely something up here. PM if to explain what you wanna do. Maybe we can find another solution?

Thanks!

Yes, what I need to do – which has been possible before (but from v1) – is present usage data for each font (each font being a taxonomy term). For instance, there would be a list that presents the top-tagged fonts within a specified year.

So, what I was really wondering was if since and until filtered the data collected for each term by the number of related entries...

Something that tripped up a ton of people was how the taxonomy tag's parameters would be filtering the entries. It was honestly pretty counter intuitive, so we changed it so its always just filtering the terms themselves.

It was fairly uncommon to need to filter the content anyway.

But, in your case, you _do_ want that. So, here's a tag that should get you there:

<?php

namespace App\Tags;

use Illuminate\Support\Carbon;
use Statamic\Facades\Entry;
use Statamic\Facades\Term;
use Statamic\Tags\Tags;

class EntryBasedTaxonomy extends Tags
{
    public function index()
    {
        $taxonomy = $this->params->get('taxonomy');

        // Get all the entries, filtered appropriately.
        $entries = Entry::query()
            ->where('collection', $this->params->get('collection'))
            ->where('date', '>', Carbon::parse($this->params->get('since')))
            ->where('date', '<', Carbon::parse($this->params->get('until')))
            ->get();

        // Group the entries by terms. An entry may appear under multiple terms.
        $terms = [];
        foreach ($entries as $entry) {
            foreach ($entry->value($taxonomy) as $term) {
                $terms[$term] = $terms[$term] ?? [];
                $terms[$term][] = $entry;
            }
        }

        // Convert the term strings into terms and all their available
        // variables (like title, slug, etc) and add the term's entries.
        $terms = collect($terms)->map(function ($entries, $term) use ($taxonomy) {
            $obj = Term::find($taxonomy.'::'.$term) ?? Term::make($term)->taxonomy($taxonomy);
            $arr = $obj->toAugmentedArray();
            $arr['entries'] = $entries;
            return $arr;
        });

        // The array needs to be zero indexed in order to be looped through.
        return $terms->values();
    }
}


md5-61a5aff8e02ebde45236df630e82bbbf


🔥

Wow, thank you @jasonvarga 🙏

I php please made the tag and added the above. The template (with the above code) is returning:

Call to a member function toAugmentedArray() on null

Is that an issue with the collection?

In this case, with the code @jasonvarga provided above, it must mean that the term can't be found.

Ah, sorry, it also returns with:

{{ entry_based_taxonomy
    collection="site-of-the-day"
        taxonomy="fonts"
    since="2019-01-01"
    until="2019-12-31"
    limit="10"
    sort="entries_count:desc"
    title:not="georgia" }}

Or...

{{ entry_based_taxonomy
    collection="site-of-the-day"
        taxonomy="fonts"
    since="2019-01-01"
    until="2019-12-01" }}

Hmm... 🤔

Sorry, the issue was when you used an on-the-fly term (one that doesn't actually exist as a file).

I've updated the code above.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrewying picture andrewying  Â·  4Comments

dakisan picture dakisan  Â·  4Comments

austenc picture austenc  Â·  3Comments

sauerbraten picture sauerbraten  Â·  3Comments

robdekort picture robdekort  Â·  3Comments