Hello. i’m not sure it’s possible…
I can filter content in pages/my_modular.md with:
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
@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:
{% for item in page.collection %}
{% if item.header.mytoggle %}
// show page details
{% endif %}
{% endfor %}
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
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
---
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 ?
@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.
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());
}
}
}
Ok. It’s not possible to customize Complex Collections.
Tanks for your function, I will try it !