Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Collection with multiple item constraints

Solved by pamtbaau View solution

Started by Vince42 4 years ago · 4 replies · 870 views
4 years ago

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

A collection defined as follows...

YAML
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';

last edited 05/27/22 by pamtbaau
4 years ago Solution

@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:

    TXT
    items:
     - '@taxonomy.category': [MyCategory]
    
last edited 05/27/22 by pamtbaau
4 years ago

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

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

and in pages.html.wtig:

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

🙂

4 years ago

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

YAML
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?

4 years ago

@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:

    YAML
    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.

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 95 15 hours ago
General · by pamtbaau, 20 hours ago
1 61 19 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 356 5 days ago
General · by Duc , 6 days ago
3 44 6 days ago