Hello!
I am building a portfolio website on which I use different filters. This works fine so far. Below you can find the code snippets.
The problem I am trying to solve ist following:
Independently from the color filter, I would like to add a filter that allows to filter by the participants of a project (let’s call it cooperation). These collaboration partners are stored in the backend in a “structure” field because I want to link the names to a URL.
How can I fetch all the names of the cooperation entries, display them as a filter and filter the projects?
As far as I know the “->pluck” command does not work for a “structure” field. I thought about storing the entries in a variable as a sting (By getting them from a foreach loop). Then use this variable to pluck the data and continue – But I could not figure out how to do that in the controller… Maybe there is even a more clean / easy possibility.
Thanks for any help!
The Color-Filter in the …/templates/home.php
<div>
<?php foreach($colors as $cl): ?>
<?php if($cl == urldecode(param('color'))): ?>
<a href="<?= $page->url() ?>"><?= html($cl) ?> (Exit Filter) </a>,
<?php else : ?>
<a href="<?= url($page->url(), ['params' => ['color' => urldecode($cl)]]) ?>"><?= html($cl) ?></a>,
<?php endif;?>
<?php endforeach ?>
</div>
The Color-Filtermechanism as a controller in …/controllers/home.php
<?php
return function ($kirby, $pages, $page, $site) {
$allitems = $site->find('projects')->children()->listed();
$items = $site->find('projects')->children()->listed();
// add the Color filter
if($color = param('color')) {
//Used for multiple words categories
$color = urldecode(param('color'));
$items = $items->filterBy('color', $color, ',');
}
//If $items is used, groups all results filters
// fetch all Color
$colors = $allitems->pluck('color', ',', true);
return compact('items','allitems','color', 'colors');
};
The Blueprint of color and cooperations in …/blueprints/project.yml
color:
label: Color
type: tags
cooperation:
label: Cooperation
type: structure
fields:
name:
label: Name
type: text
link:
lanel: Webseite
type: url