Custom fields for Kirby's add dialog. This plugin allows to define the fields shown on Kirby's page add dialog in the corresponding page's blueprint.
Use one of the alternatives below.
Download and copy this repository to /site/plugins/kirby-plugin-custom-add-fields.
git submodule add https://github.com/steirico/kirby-plugin-custom-add-fields.git site/plugins/kirby-plugin-custom-add-fields
composer require steirico/kirby-plugin-custom-add-fields
This plugin adds the extra property addFields to page blueprints.
To define custom add fields do as you would do for defining regular fields
but put the definition in the property addFields.
/blueprints/pages/remote.yml:title: Blueprint with custom Add Fields fields: # field definitions title: label: Title type: text content: label: Content type: textarea # custom add fields definition addFields: title: label: Title type: text required: true icon: title remoteUrl: label: URL to external Content type: select options: 'https://jaspervdj.be/lorem-markdownum/markdown-html.html?no-wrapping=on': Lorem Markdownum 'https://raw.githubusercontent.com/steirico/kirby-plugin-custom-add-fields/master/README.md': README icon: url
Values of custom add fields that correspond to fields of the page blueprint are
taken into account for the new page straightforwardly. In the example above the value of title in the add page dialog will be set as page's title.
In order to have kirby adding pages correctly the property slug has to be set.
There are three ways to define a page's slug:
- Add a custom add field named
slugin order to define theslugmanually. - If a field named
slugis missing the plugin will set theslugbased on the current timestamp. - Set/overwrite the
slugin a pages hook script (see below).
The values of the custom add fields can be used on the server side for modifying the page to be added.
To do so one can register a page.create:after hook and modify the page object.
The plugin also registers a generic hook which automatically detects and calls the
page model's static
method named hookPageCreate($page). Define a page model and the method as follow:
/site/models/remote.php:<?php class RemotePage extends Page { public static function hookPageCreate($page){ // get value of add field remoteUrl $remoteUrl = $page->remoteUrl()->value(); // fetch remote content $content = file_get_contents($remoteUrl); // update page field content $page->update(array( 'content' => $content )); // set slug according to add field title $page->changeSlug(Str::slug($page->title()->value())); } }
The template to be used for the new page can be forced by a field of the current page. By default,
if a field called forcedTemplate exists on the current page its value is taken into account
as template for the new page.
The field can be changed by kirby options:
/site/config/config.php:<?php return [ // exitsing configurations 'steirico.kirby-plugin-custom-add-fields.forcedTemplate.fieldName' => 'myForcedTemplateField' ];
There are some known issues related to this plugin:
- Some fields Fields such as the pages field perform additional requests to the backend. Although the pages field works as of v1.1.1, such fields may not work with this plugin. Feel free to file an issue if you encounter a broken field.
- Conditional fields are untested.
- Kirby offers no possibility to redirect to the newly created page if the
slughas been modified in a hook. Therefore, after adding a page the panel remains on the actual page.
MIT
