Hello
I would like to make description of each of my filters.
Example : I click on a filter, it display every pages filtered by the tag AND a description of this filter. It could be nice to have a field for the user to custom the description of the filter.
I’m using a basic controller like this
<?php
return function($site, $pages, $page) {
$projects = page('projects')->children()->visible();
$tags = $projects->pluck('tags', ',', true);
if($tag = param('tag')) {
$projects = $projects->filterBy('tags', $tag, ',');
}
return compact('projects', 'tags');
};
?>
With this kind of list
<ul>
<?php $pageTags = $page->tags()->split(); ?>
<?php $p = kirby()->request()->params()->tag(); ?>
<?php foreach($tags as $tag): ?>
<li>
<a <?= in_array($tag, $pageTags)? 'class="active"':'' ?> <?php ecco($tag == $p, 'class="active"') ?> href="<?php echo url('index/' . url::paramsToString(['tag' => $tag])) ?>">
<?php echo html($tag) ?>
</a>
</li>
<?php endforeach ?>
</ul>
The problem with the ‘tag’ field is that we can’t describe the word. Do you have some advices ?
texnixe
February 20, 2017, 3:22pm
2
You could use a structure field (e.g. in site settings) with two fields, tag and description.
Cool, this is a good start. The hard part is to get the correspondence between the filters and the field ?
I did this
rubriques:
label: Rubriques
type: structure
entry: >
Rubrique : <h1>{{rubrique}}</h1><br />
Description : <br />{{description}}
fields:
rubrique:
label: Rubrique
type: text
description:
label: Description
type: textarea
texnixe
February 20, 2017, 3:50pm
4
You can use the findBy()
method to find a specific entry in a structure field:
<?php
$item = $site->rubriques()->toStructure()->findBy('rubrique', 'something');
echo $item->description();
BTW: If the descriptions are not too long, I’d choose a table layout for this.
Great thank you ! But how can I display dynamically this field for each filters only if I filter ? I try to get the tag value with an if loop but i get error message only when I click on a filter wich doesn’t have a “rubrique”
I tried
<?php $tags = $projects->pluck('tags', ',', true); ?>
<?php if($site->rubriques()->isNotEmpty()): ?>
<?php foreach($tags as $tag): ?>
<?php
$item = $site->rubriques()->toStructure()->findBy('rubrique', $tag);
echo $item->description(); ?>
<?php endforeach ?>
<?php endif ?>
Fatal error: Uncaught Error: Call to a member function description()
texnixe
February 21, 2017, 9:06am
6
Check if the item exists to avoid this error:
if($item) {
echo $item->description();
}
Wonderfull
<?php $tags = $projects->pluck('tags', ',', true); ?>
<?php foreach($tags as $tag): ?>
<?php $item = $site->rubriques()->toStructure()->findBy('rubrique', $tag);
if($item) {
echo $item->description();
} ?>
<?php endforeach ?>