Panel filters - Additional to hooks

Hooks

When we use for example panel.page.update we can do something on update, for example parse the content file and then save it again. That means it is saved two times when we save.

Filters

Instead of saving the file two times it could be saved one time by introducing panel filters. With filters it could have the content file as input string and then save the output, like this:

function my_filter($content) {
    $content = str_replace('Replace something', 'With this', $content);
    return $content;
}

The above will run just before the content is saved.

Feedback?

What’s your use case, Jens?

There can be many cases, but one personal case I have is an idea for an SEO plugin.

One field two elements

In that I need ONE field that contains TWO element.s An input text and a textarea. Together they will generate a Google preview.

In this case the output would be nested yaml. One field with two “subfields”.

Template

It’s not very nice for the template. In the template you still want to just do echo $page->seo_title();. It’s probably possible to manipulate the $page object but that feels like an ugly workaround.

Output by filter

The best would be to filter the output before save to make the subfields as individual fields in the yaml. I can probably use a update hook but then it will be saved two times.

10 plugins 10 times

Think about having 10 plugins like this. Then it will save the file many times.

Wouldn’t it be easier to use a page model, so you could still output $page->seo_title(), instead of duplicating the content in the file?

But the seo_titles and seo_descriptions are global to the entire site. Models are connected to a template. With this solution I would need to duplicate the code 10 times for 10 templates. That’s not an option for an awesome SEO preview plugin.

Any other ideas?

You can workaround this by putting you’re base page model in a plugin and extend this class in your models, as @lukasbestle is explaining here: https://github.com/getkirby/kirby/issues/212

Another question I didn’t understand: why aren’t you using two separate fields? Do they have to be connected in some way?

That’s what I was asking myself as well, maybe because the plugin is a custom field?

@jakobploens @texnixe

Here is why I don’t use two fields:

As far as I’ve tried javascript is prevented to communicate between fields and it has to use javascript across fields if that is going to work.

Look at this:

http://www.contentforest.com/seo-tools/serp-preview

It’s a little messier than what I’m thinking of, but the function is quite the same. When writing a seo title it updates the preview. When writing the seo description it updates the same preview.

I don’t want a separate preview for the seo title and the seo description. The preview is a group that needs to be together.

More questions? More ideas?

Okay, now I got it. Didn’t get the preview-thing at first!

Have you tried the solution with the extendig base page class as a plugin? Not the ideal solution as the class is located elsewhere, but should work without problems (didn’t test it though).

No, I have not tried anything yet. I’m just coding in my own brain right now, but it seems like that would be the only almost ok solution.

Maybe keep this thread open if anyone else comes with something brilliant.

It’s just an idea, but you could create a custom field which then renders two or more input fields in the Panel and is saved as structured YAML-content.

I’m not quite sure how this could work, however, it seems to be possible, e. g. structure fields are a collection of multiple input fields that are treated as a single data field.

I’m thinking that this might also work:

https://getkirby.com/docs/advanced/field-methods

One field with two subfields and with the field method get one or the other. Will try it later.