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.

Content & Markdown

Filter in pages/.MD with toggle options

Solved by pamtbaau View solution

Started by BKaernel 4 years ago · 4 replies · 441 views
4 years ago

Hello. i'm not sure it's possible...
I can filter content in pages/my_modular.md with:

YAML
content:
    items: 
       - '@taxonomy':
            category: [Actualité, En avant]

But is it possible to do the same thing with a button toggle
directly in my_modular.md with filter or just in my_modular.html.twig ?

I've created a field button toggle in blueprint options for post.yaml
(toggle or it can be a simple checkbox)

I hope my question is easy to understand.

I used the last version of GRAV
The website if is useful ? : https://terrater.org/fr

4 years ago

@BKaernel,

I hope my question is easy to understand.

Uhm... what shall I say....

Do you perhaps want to filter a collection based on a boolean field in the header/frontmatter of a page? If so, no you can't. See Complex Collections:

Additionally, you can filter the collection by using filter: type: value . The type can be any of the following: published , visible , page , module , routable . These correspond to the Collection-specific methods, and you can use several to filter your collection. They are all either true or false . Additionally, there is type which takes a single template-name, types which takes an array of template-names, and access which takes an array of access-levels

What you can do in Twig, when looping through the page collection, is checking the value page.header.mytoggle and skip the page when it is false (or true).

Untested sample code:

TWIG
{% for item in page.collection %}
  {% if item.header.mytoggle %}
    // show page details
  {% endif %}
{% endfor %}
4 years ago

I've already seen Complex collections but it's not very explicit...

In twig it's easy, but i want to know if it's possible in pages/modular.md ?
This is the boolean field (header.en_avant) in a modular.yaml

YAML
fields:
  header.en_avant:
    ordering@: 1
    type: toggle
    label: Post en avant (homepage)
    highlight: 1
    default: 0
    options:
      1: Enabled
      0: Disabled
    validate:
    type: bool

and pages/category.md (category is a modular ) with this

YAML
---
title: 'En avant'
body_classes: modular
content:
    items: '@self.siblings'
    filter:
        type: 'header.en_avant'
        options: 1
---

I suppose the solution is to set the good key/value in filter, but wich one ?

4 years ago Solution

@BKaernel,

I’ve already seen Complex collections but it’s not very explicit…

With which part of the documentation are you struggling?

In twig it’s easy, but i want to know if it’s possible in pages/modular.md ?

As said, you cannot create a custom filter in a collection definition. Only those mentioned in chapter 'Complex Collections'.

You can do it in Twig as shown above, or in PHP.

In PHP you would catch event 'onCollectionProcessed', loop through the collection and remove items from the collection depending on the page->header()->en_avant. Field en_avant must be added to the header of the modules that are part of the modular collection, not the modular itself.

PHP
  public function onCollectionProcessed(Event $event): void
  {
    /** @var Collection $collection */
    $collection = $event['collection'];

    foreach ($collection as $item) {
      $enAvant = $item->header()->en_avant ?? true;

      if ($enAvant === false) {
        $collection->remove($item->path());
      }
    }
  }
4 years ago

Ok. It's not possible to customize Complex Collections.
Tanks for your function, I will try it !

Suggested topics

Topic Participants Replies Views Activity
Content & Markdown · by Jochen, 8 months ago
6 94 8 months ago
Content & Markdown · by Ton Haarmans, 1 year ago
10 184 1 year ago
Content & Markdown · by Jan L'Am, 1 year ago
4 146 1 year ago
Content & Markdown · by Leonardo, 1 year ago
3 60 1 year ago
Content & Markdown · by belthasar, 1 year ago
4 254 1 year ago