Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection filter based on more than one taxonomy discards criteria where terms are not in the taxonomy #1184

Closed
hughbris opened this issue Nov 23, 2016 · 6 comments
Labels

Comments

@hughbris
Copy link
Contributor

hughbris commented Nov 23, 2016

This could be contentious, it's a case where surprising behaviour happened when creating a filtered collection by more than one taxonomy vocabulary.

This snippet demonstrates the collection I am trying to make in the page frontmatter:

content:
    items:
        '@taxonomy': {tag: staff, team: hygienists}

So I want pages tagged 'staff' and (the default) also ascribed the term 'hygienists' from the vocabulary 'team'. The example is taken from examples provided in Grav documentation.

There are currently no pages where taxonomy:team=hygienists, but this is a placeholder for when there may be, and I expect the filter to return an empty collection until then. That's the part that may be controversial, but in my context it makes perfect sense.

What I see instead in the resulting collection is all pages tagged staff (i.e. the first @taxonomy criterion).

(I have tried the alternate YAML formats suggested. When setting similar page collections and the term does exist in the 'team' vocabulary, the filter works as expected.)

I can see the code that is responsible for this behaviour in Taxonomy.php, and it is unchanged in Grav 1.1:

        foreach ((array)$taxonomies as $taxonomy => $items) {
            foreach ((array)$items as $item) {
                if (isset($this->taxonomy_map[$taxonomy][$item])) {
                    $matches[] = $this->taxonomy_map[$taxonomy][$item];
                }
            }
        }

Expected behaviour could be achieved by adding an else clause on line 96 after the if stanza, something like:

else {
   $matches[] = []; /* actually I'm not exactly sure what type $matches should be, .. 
                       but we want a value here that will produce an empty set when ..
                       $operator is 'and' and make no difference when it's 'or'. */
}

Let me know if you want me to write that patch. :)

@flaviocopes
Copy link
Contributor

flaviocopes commented Nov 23, 2016

I tested and I'm not getting the problem. Did you add team to the site taxonomies in the Site configuration (or user/config/site.yaml)?

@hughbris
Copy link
Contributor Author

Thanks for checking. Yes, I forgot to mention that I have registered the vocab there. Also, that I am using Grav v.1.1.8! (sorry, was late and I have to move on to another project)

I'm surprised you can't reproduce because, if I traced the code back correctly, you can see in that predicate (isset($this->taxonomy_map[$taxonomy][$item])) where it will not add the criterion if the term does not exist in the vocabulary.

I'll try reproducing on a clean install, and also adding that patch in to Taxonomy.php. Don't have time right now though.

!remindme one week
(does that work on Github?)

@flaviocopes
Copy link
Contributor

I tried with taxonomies already defined (tag and category)

@hughbris
Copy link
Contributor Author

hughbris commented Dec 1, 2016

I was able to reproduce this. Github won't allow me to attach a git patch file, so I created a Gist, hope that works for you :) You need to apply it to the user directory of a clean Grav 1.1.8.

As mentioned, I also tested my suggested fix to Taxonomy.php where I add an else block. It produced the behaviour I wanted/expected, i.e. no results.

After you've confirmed a bug, would you like me to submit a pull request?

Edit: the path for testing this will be /staff

@flaviocopes
Copy link
Contributor

Thanks! Fixed in develop. I was able to reproduce the problem with your patch. So basically if there are 2 taxonomies required but one has 0 items, it's going to simply pick the ones corresponding to the other taxonomy, thus generating a wrong result. Didn't get the problem in the first place. 👍

@hughbris
Copy link
Contributor Author

hughbris commented Dec 4, 2016

I thought maybe you didn't grok, but that is because I gave a poor explanation :/ My bad. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants