PHP code example of hksagentur / kirby-facets

1. Go to this page and download the library: Download hksagentur/kirby-facets library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hksagentur / kirby-facets example snippets




use Hks\Facets\Http\Filter;
use Hks\Facets\Http\Filters;

$filters = Filters::for($page->children()->listed())
    ->add(Filter::in('season'));

$concerts = $filters->apply();

Filter::equals('category');
Filter::after('published'); // ?published=2024-03-01

Filter::atLeast('floorSize'); // ?floorSize=80 → floorSize >= 80
Filter::atMost('price');      // ?price=500000 → price <= 500000

Filter::search(['title', 'text']); // reads ?q=... by default, override via the 2nd argument

Filters::for($concerts)
    ->add(Filter::in('season'))
    ->add(Filter::belongsTo(name: 'series', relation: 'concert-series'));

Filter::callback('limit', fn (Collection $collection, mixed $value) => $collection->limit((int) $value));

Filter::not(Filter::equals('category')); // everything that does NOT match

<?= $page->children()->listed()->pipe([
    Filter::in('season'),
    Filter::belongsTo(name: 'series', relation: 'concert-series'),
]) 



use Hks\Facets\Cms\Filterable;
use Hks\Facets\Http\Filter;

class ConcertPage extends Page implements Filterable
{
    public static function filters(): array
    {
        return [
            Filter::in('season'),
            Filter::belongsTo(name: 'series', relation: 'concert-series'),
        ];
    }
}



$filters = Filters::for($concerts)->use(ConcertPage::filters(), ['season']);
$concerts = $filters->apply();

Filter::in('season');                                // reads ?season=..., filters the season attribute
Filter::in(name: 'spielzeit', attribute: 'season');   // reads ?spielzeit=..., filters the season attribute

Filter::in('season')->from('spielzeit');
// ->attribute() still returns 'season'; only ->name() becomes 'spielzeit'



use Hks\Facets\Form\Facet;
use Hks\Facets\Form\Facets;

$facets = new Facets([
    Facet::checkboxes('season', 'Season', fn () => Seasons::options()),
    Facet::date('date', 'Date'),
    Facet::toggle('musiktheater', 'Musiktheater'),
]);

echo $facets;

 foreach ($facets as $facet): 

 foreach ($facet->options() as $option): 

Facet::radio('season', 'Season', fn () => $kirby->collection('seasons')->toFacetOptions());
Facet::radio('category', 'Category', fn () => $categories->toFacetOptions(label: 'name')); // Structure: no title(), has 'id'/'name'
Facet::radio('house_type', 'House type', fn () => $page->houseTypes()->toFacetOptions(icon: 'icon')); // reads the page's own `icon` field

Facet::radio('house_type', 'House type', fn () => $page->houseTypes()->toFacetOptions(icon: 'icon'))
    ->hideLabels();

echo $facets->links(); // <ul> of removable chips, via the facets/links snippet

Facet::checkboxes('season', 'Season', fn () => Options::seasons())
    ->formatUsing(fn (string $value, string $label) => "Season {$label}");

public function __invoke(Collection $collection): Collection
{
    return $this->isEmpty() ? $collection : $this->apply($collection);
}



namespace App\Filters;

use Hks\Facets\Http\Filter\Attribute;
use Kirby\Cms\Collection;

class GreaterThan extends Attribute
{
    public function apply(Collection $collection): Collection
    {
        return $collection->filterBy($this->attribute(), '>', $this->value());
    }
}

$facets->toHtml(['class' => 'search-form']);

 foreach ($facets->featured() as $facet): 

site/snippets/facets/checkboxes.php
site/snippets/facets/radio.php
site/snippets/facets/select.php
site/snippets/facets/date.php
site/snippets/facets/toggle.php
site/snippets/facets/form.php
site/snippets/facets/links.php
site/snippets/facets/icon.php

site/snippets/facets/radio--house-type.php