# Config filters for plugin development

**URL:** <https://forum.getkirby.com/t/config-filters-for-plugin-development/8506>\
**Category:** Solutions\
**Tags:** v2\
**Created:** [September 14, 2017, 7:07am UTC](https://forum.getkirby.com/t/config-filters-for-plugin-development/8506 "2017-09-14T07:07:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [September 14, 2017, 7:07am UTC](https://forum.getkirby.com/t/config-filters-for-plugin-development/8506/1 "2017-09-14T07:07:03Z")

</div>

# Config filters

In Kirby we already have something called collection filters. In this post I will write about a solution I came up with that I now call config filters.

## Introduction

Let’s say you have a plugin that output values from a collection. Maybe we want to the user to be able to change these values on different conditions.

**Config.php**

In `$args` we have `defaults` which is an array with default values from the plugin, that can be replaced. We can send other data as well, for example the current page object of the collection.

```php
c::set('my_plugin.filter', function($args) {
    $args['defaults']['title'] = 'hello world';
    return $args['defaults'];
});

```

**Plugin**

I made some comments in the code.

```php
// $item is a page object in a foreach loop

// Set up default values of the collection
$defaults = [
    'title' => $item->title(),
    'url' => $item->url(),
];

// Set results to defaults as fallback
$results = $defaults;

// Set the config to a filter variable
$filter = c::get('my_plugin.filter');
// Check if the config filter is an anonymous function
if(is_callable($filter)) {
    // Call the config filter with defaults array and the page object
    $callback = call($filter, [
        'args' => [
            'defaults' => $defaults,
            'page' => $item
        ]
    ]);
}
// Set results to the config filter results if it's an array
if($callback && is_array($callback)) {
    $results = $callback;
}

print_r($results); // In a real case you would probably return it from a function here

```

It may look much to do but it’s quite powerful for the user in the end.

---

<div class="post-metadata">

**Author:** ![bnomei](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bnomei/32/775_2.png) [@bnomei](https://forum.getkirby.com/u/bnomei)\
**Post date:** [September 15, 2017, 6:48am UTC](https://forum.getkirby.com/t/config-filters-for-plugin-development/8506/2 "2017-09-15T06:48:59Z")

</div>

missing the part of filtering said collection. maybe adjust example code in config file to make use of $page and $filter?  
i understand it can be applied to anything not just filtering collections, but it would make it easier to follow.

otherwise i like the ideas of closures for plugins. @pedroborges did something like that for his [sitemap plugin](https://github.com/pedroborges/kirby-xml-sitemap/blob/master/readme.md) ‘sitemap.process’ afaik.

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [September 15, 2017, 7:20am UTC](https://forum.getkirby.com/t/config-filters-for-plugin-development/8506/3 "2017-09-15T07:20:36Z")

</div>

Yes, it looks similar to that sitemap plugin configs. I will soon release a brand new plugin where I use this idea.

I agree that there is room for improvement in the example above. I made a function out if it that looks kind of like this:

```php
$filtered_result = callback('filter.array', $page->field_with_array(), $page),

```

In the above I use `$page->field_with_array()` as the default / fallback. I send the `$page` to make it accessable in the config option. The `filter.array` is my config option key (without prefix).

**In `config.php` it will look something like this:**

```php
c::set('filter.array', function($args) {
    $args['data']['title'] = 'hello world';
    $args['data']['url'] = $args['page']->slug() . '/something-else';
    return $args['data'];
});

```

If this option does not exist, it will just return `$page->field_with_array()` to `$filtered_result`.

It takes something in, filter it, and return it out again. It’s really similar how [WordPress filters](https://developer.wordpress.org/reference/functions/add_filter/) work. I don’t like everything with WordPress, but I think they succeded with actions and filters.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:32pm UTC](https://forum.getkirby.com/t/config-filters-for-plugin-development/8506/4 "2025-02-06T17:32:10Z")

</div>

Archived on February 6, 2025, 5:32pm UTC


