Collection with multiple item constraints

I have a folder /details of which some page have a category.

A collection defined as follows…

items:
  - '@page.children': /details
  - '@taxonomy.category': [MyCategory]

…will contain all child pages of folder /details plus all pages with category ‘MyCategory’. Since the both items are in folder /details I get all pages in /details

How do I create a collection that only contains pages from folder /details AND also have category ‘MyCategory’;

@Vince42,

I see two alternatives depending on your situation:

  1. If there are also pages with category ‘MyCategory’ outside folder /details, you will need to create two separate collections and apply method intersect.

    See Multiple Collections to create two collections in a page. One for all pages in the /details folder and the other for all pages with category “MyCategory”.

    Then in Twig, get both collections from the page and use an “intersect” (see Collection Object Methods):

    Collection::intersect($collection2) Merge two collections, keeping items that occur in both collections (like an “AND” condition)

  2. If there are no categories outside folder /details, then the following will give only the pages with the proper category:

    items:
      - '@taxonomy.category': [MyCategory]
    

Perfect, it worked out of the box!
Frontmatter of the module:

pages:
    items: '@page.children': /details
taxonomies:
    items: '@taxonomy.category': [Service]

and in pages.html.wtig:

{% set pages = page.collection('pages') %}
{% set taxonomies = page.collection('taxonomies') %}
{% set collection = pages.intersect(taxonomies) %}

:slight_smile:

I just made a second module on the same modular page with

pages:
    items: '@page.children': /details
taxonomies:
    items: '@taxonomy.category': [Support]

This led to the following:

  • The previous layout (two columns of mini-pages) has changed to a one-column layout.
  • The second collection is not displayed at all.

What should I do in order to have multiple “page collection modules” on my modular page?

@Vince42, A few remarks…

  1. The frontmatter snippet you’re showing contains incorrect Yaml. If you truly use that in your page’s frontmatter, an error should be thrown.

    The correct frontmatter should be:

    pages:
      items: 
        '@page.children': /details
    

    You might want to update your posts accordingly.

  2. It is a bad habit to override existing Twig variables. This may lead to hard to trace issues. pages is such a variable which is provided by Grav. See Theme Variables

  3. The previous layout (two columns of mini-pages) has changed to a one-column layout.
    You are implying that a new module is changing the layout of another module. I don’t see how a module could interfere with the layout of another module, unless the HTML generated by the Twig template is incorrect.

  4. The second collection is not displayed at all.
    How do you know it is ‘not displayed’ versus ‘empty’? Did you dump the collection to the debugger?

  5. What should I do in order to have multiple “page collection modules” on my modular page?
    I don’t know… You might want to check your Twig and check if your collection isn’t empty.