statamic / ssg

The official Statamic Static Site Generator
229 stars 23 forks source link

SSG seems to ignore taxonomy/contains filters on collection tag #96

Closed tao closed 2 years ago

tao commented 2 years ago

This one is a bit strange, I can't figure out what's going on... and I'm still using the old regex parser so it might change with the new one... but the collection in my Wiki page is being generated differently on SSG compared to the live website.

  {{ collection:wiki book:contains="{book}" category:contains="{category}" as="entries" }}
    <div class="flex flex-col pt-4">
      {{ entries }}
          {{ partial:wiki/item }}
      {{ /entries }}
    </div>
  {{ /collection:wiki }}

In this example the "category" could be a keyword like places or people or a few tags like places + unusual and my blueprint looks like this:

      -
        handle: category
        field:
          mode: select
          taxonomies:
            - wiki
          display: Category
          type: terms
          icon: taxonomy
          listable: hidden
          instructions_position: above
          create: false
      -
        handle: book
        field:
          mode: select
          taxonomies:
            - books
          display: Books
          type: terms
          icon: taxonomy
          listable: hidden
          instructions_position: above
          create: false

On my dev / live website this only returns places:

Screenshot 2022-05-18 at 11 28 29

But my static website just returns the entire collection:

Screenshot 2022-05-18 at 11 29 18

I tried to get around this by creating a custom Wiki tag to filter the categories myself and I found that sometimes it runs into a TypeError because the category can sometimes be an array and sometimes be a string variable.

    /**
     * The {{ wiki:category }} tag.
     *
     * @return string|array
     */
    public function category()
    {
        $category = $this->params->get('category');

        // fetch the entries in the collection
        return Entry::query()
            ->where('collection', 'wiki')
            ->where('published', true)
            ->get()
            ->filter(function($item) use ($category) {
                $categories = $item->get('category');
                $categories = gettype($categories) === "string" ? array($categories) : $categories;

                if ($categories) {
                    return in_array($category, $categories) ? $item : false;
                }
            });
    }

So you can see I had to force the collection category to be an array variable with this line:

    $categories = gettype($categories) === "string" ? array($categories) : $categories;

Perhaps the static site generator is running into the same error when trying to filter the collections? I'm not sure why the dev/live website is working without a problem though. I don't really understand why it would be different if SSG just visits each webpage to generate it.

tao commented 2 years ago

I'm actually having trouble with my new tag also... that works on my live / dev website:

Screenshot 2022-05-18 at 13 51 44

But the custom tag on SSG just generates Places

Screenshot 2022-05-18 at 13 51 49