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

How to filter to specific folder collection and a custom header field

Started by NicolasG 🇫🇷 6 years ago · 3 replies · 745 views
6 years ago

Hello,

I can't filter on both a folder and a custom header field (or even taxonomy).

Either it takes the taxonomy, or the folder, I've never been able to do both at the same time even though it's in the same array.

TXT
  items:  {
            '@taxonomy':
              {
                'numdep': '03'
              },
            '@page': '/others'
          }

Also, I would like (unsuccessfully for the moment) to be able to filter on a custom header field without going through the taxonomy.

YAML
---
title: test title
taxonomy:
    numdep: '03'
    insee: '03102'
geocoding:
  lat: 1
  lng: 2
process:
  twig: true
location:
    insee: 34129
    numdep: 34
---

Is this possible? I've tried everything...

items: {
'@page.header.location':
{
'numdep': '34'
},
'@page': '/autres'
}

or

items: {
'@page.header.location.numdep': 34,
'@page': '/autres'
}

Thank you for the support

6 years ago

Finaly is possible with hooks...

PHP
public function onCollectionProcessed(Event $event)
{

    /** @var Collection $collection */
    $collection = $event['collection'];

    if ($infos = $this->isBarberDepartmentPage()) {

        $current_numdept = $infos['dept'];

        foreach ($collection as $item) {

            $numdept = $item->header()->location['numdep'];

            if (!empty($numdept) && $numdept != $current_numdept) {
                $collection->remove($item->path());
            }

        }
    }

}

But I find it's not very efficient to have to browse through all our pages to finally delete some of them...

5 years ago

Hi @nicolasG ,

I also try to reduce my pages by filtering with onCollectionProcessed(Event $event) function.
Doesn't the hook work in admin pages?

I give the reduced result back to $event['collection'] = $collection; but the list of pages in admins pages.html.twig stays the same.

Do I have to modify the twig in pages.html.twig, too? I thought the hook changes the collection.

PHP
 public function onCollectionProcessed(Event $event) {

    if ($this->isAdmin()) {

        $uri = $this->grav['uri'];

        if (isset($uri->paths()[1]) && $uri->paths()[1] === 'pages') {

            // $username = trim($this->grav['uri']->param('query'));

            /** @var Collection $collection */
            $collection = $event['collection'];

            $modify_user = 'just-for-test';

            foreach ($collection as $cpage) {

                if(!$cpage){
                    dump("An error has occured. Please contact site admin. No collection page found.");
                    continue;
                }
                $last_modified_by = property_exists($cpage->header(), 'draft') ? $cpage->header()->draft['last_modified_by'] : '';

                if ($last_modified_by !== $modify_user) {
                    $collection->remove($cpage);
                }
            }
            $event['collection'] = $collection;
        }
    }
}

Looking forward to your answer!

last edited 02/04/21 by Christiana

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 85 13 hours ago
General · by pamtbaau, 18 hours ago
1 60 18 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 350 5 days ago
General · by Duc , 6 days ago
3 44 5 days ago