Your first Panel field
This cookbook recipe is based on these two video episodes…
…and a question that recently came up on the forum.
What we will cover
In this recipe, we start with the basics of creating a field from scratch using a Vue.js single file component, and then build on this by reusing parts of Kirby's UI Kit. We will deviate a little from the example in the videos though, and our final result will be a text input for DOI names, with a link that allows us to verify the input by calling the link https://www.doi.org/ suffixed with the field input.
Finally, we build a second field that doesn't store anything, but only shows a list of links in the Panel.
For the purposes of this recipe, we assume you have read the Panel plugins guide on how to get started with our Panel plugin bundler kirbyup.
You can install the Pluginkit as a basis or create the file structure we need manually, that's up to you. Also, it doesn't matter if you use the Plainkit or the Starterkit as a starting point.
Let's start by creating a new folder in the plugins folder called doifield. Inside this folder, we create a package.json file with the contents copied from the Pluginkit example mentioned above.
{
"name": "doifield",
"private": true,
"scripts": {
"dev": "npx -y kirbyup src/index.js --watch",
"build": "npx -y kirbyup src/index.js"
}
}
This will take care of compiling our source files into an index.js file in the root of our doifield plugin folder.
Register the field: PHP part
As for any plugin, we first create an index.php file containing the Kirby plugin wrapper, in which we register the new doi field type.
Every field in Kirby is a PHP class, which contains the backend logic of the field: its options, its value and its validation. Our DOI field is a text input, so for now, we simply register Kirby's own class of the text field for our new field type. We will come back to this later.
<?php
use Kirby\Form\Field\TextField;
Kirby::plugin(
name: 'pluginauthor/doi',
extends: [
'fields' => [
// for now, our field uses the backend logic of the text field
'doi' => TextField::class
]
]
);
Register the field: Vue part
Next, we create a new folder called /src in the doifield folder, and in the /src folder an index.js file.
In index.js we also have to register the new field like in index.php, but here using JavaScript syntax:
window.panel.plugin("pluginauthor/doi", {
fields: {
doi: {
// here goes the code for the field
}
}
});
To make the field work without errors, we also have to add a template property. Let's start with something really basic.
window.panel.plugin("pluginauthor/doi", {
fields: {
doi: {
template: "<p>Hello</p>"
}
}
});
Build and run
Ok, now that we got the most basic field definition in place, we are ready to run…
npm run dev
…in the /doifield folder to compile the code. If everything works as expected, there will now be a compiled index.js file in the doifield folder.
Blueprint
Let's add this new field type in a blueprint and view the result in the Panel, for example in /site/blueprints/pages/sandbox.yml in the Starterkit (you can use any other blueprint).
title: A Blueprint
fields:
doi:
type: doi
label: DOI name
When we open the unlisted Sandbox page in the Panel, we can see the doi field in the Panel:

Ok, ok, this only prints Hello and is far from impressive, but hey, it's a start.
Using a single file component
Before we continue, let's clean up a bit and introduce a single file component, in which we will store the client-side logic of the field.
So let's create a /components folder in the /src folder, in that a /fields folder and inside that a DoiField.vue file, so that the folder structure looks like this:
doifield
src
components
fields
- DoiField.vue
- index.js
- index.js
- index.php
- package.json
In the single file component, we recreate the template inside template tags:
<template>
<p>Hello world</p>
</template>
<script>
export default {
// here goes the Vue code
};
</script>
<style>
/* optional styles for the component */
</style>
We are also adding the script tags that will shortly contain the logic for the field and an optional style tag (we will not use custom styles for this example, so you might as well omit them).
In /src/index.js, we import this new component, assign the component to the doi field and remove the template property from the first iteration, so that it looks like this:
import DoiField from "./components/fields/DoiField.vue";
window.panel.plugin("pluginauthor/doi", {
fields: {
doi: DoiField
}
});
In the Panel, we won't see any changes at this point.
Creating an HTML field input
Let's move a little further and turn this into a real field input using a standard HTML input tag:
<template>
<div>
<label for="textfield">Hello</label>
<input type="text" name="textfield">
</div>
</template>
After the code compiles, we now have an input field with a hard-wired Hello label. The Panel resets the styles of native form elements, so you'll only see the input once you click into it. And while you can type text into the input field, you will not be able to store it. But one step after the other, so how do we get the DOI name label we already put into the blueprint into our field label?

Here come props
In Vue.js, we have to define props. Props are basically the data that once defined, you can then use in the component. In a Kirby context, they bridge the gap between the field options we define in our .yml blueprints and the frontend. The PHP class of the field takes the options from the blueprint and sends them to the field component automatically. Let's add a label prop to the props object:
<script>
export default {
props: {
label: String
}
};
</script>
A property consists of the key for the property and a value that contains at least the type we expect to have here, in this case we want a string.
We can now replace the hard-wired Hello string with the label defined in the blueprint using the most basic form of data binding. The Mustache tag will be replaced with the value of the label property.
<template>
<div>
<label for="textfield">{{ label }}</label>
<input type="text" name="textfield">
</div>
</template>
If you now change the field label in the blueprint and reload the page in the Panel, you can see how the value for the label changes as well:

Load value of field into Panel
Currently, our input field has no content yet. Before we start editing the field in the Panel, let's add some content in our text file and see how we can load the value of the field into the input field.
Title: Sandbox
----
Doi: Some boring text
Let's add value as a prop and bind this value to the input field with :value="value".
<template>
<div>
<label for="textfield">{{ label }}</label>
<input type="text" name="textfield" :value="value">
</div>
</template>
<script>
export default {
props: {
label: String,
value: String
}
};
</script>
:value="value" is shorthand syntax for the Vue.js v-bind directive, which can be used instead of the long form v-bind:value="value".

Great, we can now bind the value in the text field to the input. But if we try to change the value in the Panel, nothing will happen yet. The Panel doesn't notice any changes, and we cannot store them.
Events
At this point, the Panel doesn't know when something changes, so we have to tell it somehow.
In Vue.js, we can use the v-on directive to listen to DOM events and run some JavaScript when they’re triggered. In this case, we want to listen to the input event, and call the onInput() method when the event is triggered. We can do this in Vue.js with v-on:input="onInput", but since we are a bit lazy we use the shorthand @input="onInput" instead, so that the template code now looks like this:
<template>
<div>
<label for="textfield">{{ label }}</label>
<input type="text" name="textfield" :value="value" @input="onInput">
</div>
</template>
To make the code work, we still have to add the onInput() method:
<script>
export default {
props: {
label: String,
value: String
},
emits: ["input"],
methods: {
onInput(event) {
this.$emit("input", event.target.value);
}
}
};
</script>
We have to define this method in the methods definition.
We can now use standard JavaScript to fetch the value from the input field with event.target.value. Using Vue's $emit method, we emit the input event which the Panel expects here, and pass the field value to it.
The emits option declares that our component emits this event. Don't skip it: without it, Vue attaches the Panel's listener to our component's root element as well, so the native input event of the HTML input would reach the Panel, too, and the Panel would store the event object instead of our value.
And that's it. When we now return to the Panel, we can edit the field value and as soon as we do so, the Panel shows the save buttons, allowing us to either store the new or revert to the original value 🎉.

Reusing standard Kirby components
Our field is still looking ugly and quite unlike the other fields around it. How can we change their appearance without having to write many lines of custom CSS?
Well, Kirby comes with a large set of ready-to-use standard components that we can reuse in our plugins and that free us from the trouble of having to style everything manually.
These standard components all start with k- followed by the name of the component. You find the full set of Kirby's UI components in the Reference.
We start with replacing the wrapper div with the k-field component. Each component has a set of props that we can leverage to reduce the HTML code, in this case, the k-field component has a label property to which we can bind the value of the label property. Once we've added that, we can remove the <label> tag.
<template>
<k-field class="k-doi-field" :label="label">
<input type="text" name="textfield" :value="value" @input="onInput">
</k-field>
</template>
Hurray, our field immediately starts looking better with the label above the input.

The input field we replace with a k-input component, which makes it look like the input of any other field:
<template>
<k-field class="k-doi-field" :label="label">
<k-input type="text" :value="value" @input="onInput" />
</k-field>
</template>
While this looks good now, we get an error as soon as we start typing something now. That's because the input component also takes care of sending the value to the input event, so that we don't have to get it from the event.target anymore. Instead, we pass the value as parameter to the onInput() method and replace event.target.value with just value.
<script>
export default {
props: {
label: String,
value: String
},
emits: ["input"],
methods: {
onInput(value) {
this.$emit("input", value);
}
}
};
</script>
With this, we now have a fully working input field that looks like a normal text field.

We can now add more props, if we want, to make the standard field properties like help, disabled, required work as well.
The input element also accepts some props, like before and after.
Our field uses the PHP class of the text field, so it supports all options of the text field. Each option you set in the blueprint is sent to the frontend. To work with it, define the property in the props object of the component.
<template>
<k-field
class="k-doi-field"
:disabled="disabled"
:help="help"
:label="label"
:required="required"
>
<k-input
:after="after"
:before="before"
:disabled="disabled"
:icon="icon"
:required="required"
type="text"
:value="value"
@input="onInput"
/>
</k-field>
</template>
<script>
export default {
inheritAttrs: false,
props: {
after: String,
before: String,
disabled: Boolean,
help: String,
icon: String,
label: String,
required: Boolean,
value: String
},
emits: ["input"],
methods: {
onInput(value) {
this.$emit("input", value);
}
}
};
</script>
Kirby passes a few more props to every field, like name, type or width. inheritAttrs: false keeps the props our component doesn't declare from ending up as HTML attributes of our field.
Try it out by adding these properties in the blueprint and watch the results in the Panel.
Extending the field
As you probably know, the url field has a clickable button that when clicked opens the link stored in the field in the browser. We are going to add such a link to our doi field now.
We can achieve this by stealing the template code from the url field, it's just a button that is inserted inside the k-input field and takes the slot reserved for the icon.
<template>
<k-field
class="k-doi-field"
:disabled="disabled"
:help="help"
:label="label"
:required="required"
>
<k-input
:after="after"
:before="before"
:disabled="disabled"
:required="required"
type="text"
:value="value"
@input="onInput"
>
<template #icon>
<k-button
v-if="link"
:icon="icon"
:link="'https://doi.org/' + value"
:title="$t('open')"
class="k-input-icon-button"
tabindex="-1"
target="_blank"
/>
</template>
</k-input>
</k-field>
</template>
What's new here? With the v-if directive, we define that the button is only shown if the link prop is set to true, which it will be by default (we can disable this via the blueprint). In the link prop, we prepend a hard-wired URL to the field value. We could make this more dynamic by introducing a new property if we wanted, but we leave it as is.
We still have to define the new link prop. In the component, it's just another prop:
<script>
export default {
inheritAttrs: false,
props: {
after: String,
before: String,
disabled: Boolean,
help: String,
icon: String,
label: String,
link: Boolean,
required: Boolean,
value: String
},
emits: ["input"],
methods: {
onInput(value) {
this.$emit("input", value);
}
}
};
</script>
But the text field doesn't know a link option. Kirby only sends the options to the component that are defined in the PHP class of the field. So it's time to give our field a class of its own. It extends the class of the text field, which keeps all its options, and adds the new link option. It also gives the icon option of the text field a new default:
<?php
namespace PluginAuthor\Doi;
use Kirby\Form\Field\TextField;
use Kirby\Reflection\Attributes\Prop;
class DoiField extends TextField
{
// a new default for the icon option of the text field
protected string|null $icon = 'url';
/**
* Shows a button to open the DOI name
*/
#[Prop]
protected bool $link = true;
}
Each option of a field is a typed property with the #[Prop] attribute. Its default value is the default of the option. Read more about field classes in the reference.
In our index.php, we load the class and register it instead of the text field class:
<?php
use PluginAuthor\Doi\DoiField;
F::loadClasses([
'pluginauthor\\doi\\doifield' => 'src/DoiField.php'
], __DIR__);
Kirby::plugin(
name: 'pluginauthor/doi',
extends: [
'fields' => [
'doi' => DoiField::class
]
]
);
Voilà , our new doi field is ready for use.

Fields without a value
Our DOI field stores what editors type into it. But a field doesn't have to store anything. It can also just show something in the Panel, for example a few links that help editors get started:

Such a field is set up the same way as our DOI field. So let's create a second plugin folder called linksfield, add the same package.json with linksfield as its name and focus on what's different.
The PHP class
Instead of the class of the text field, the class of our new field extends Kirby's DisplayField class. Display fields come with the label and help options, but they don't have a value, so Kirby never writes anything to the content file for them. We add two options of our own: a layout and the links themselves.
<?php
namespace PluginAuthor\Links;
use Kirby\Form\Field\DisplayField;
use Kirby\Reflection\Attributes\Prop;
class LinksField extends DisplayField
{
/**
* Layout of the links: `list` or `cards`
*/
#[Prop]
protected string $layout = 'list';
/**
* The links to display
*/
#[Prop]
protected array $links = [];
}
We load and register the class like before, this time as links field:
<?php
use PluginAuthor\Links\LinksField;
F::loadClasses([
'pluginauthor\\links\\linksfield' => 'src/LinksField.php'
], __DIR__);
Kirby::plugin(
name: 'pluginauthor/links',
extends: [
'fields' => [
'links' => LinksField::class
]
]
);
The Vue component
In the src/index.js of the plugin, we register the component for the links field:
import LinksField from "./components/fields/LinksField.vue";
window.panel.plugin("pluginauthor/links", {
fields: {
links: LinksField
}
});
The component itself is much shorter than the one of our DOI field. There is no value prop, no input event and nothing to save. It only receives the options from the PHP class as props:
<template>
<k-field :label="label" class="k-links-field">
<k-collection :items="links" :layout="layout" />
</k-field>
</template>
<script>
export default {
inheritAttrs: false,
props: {
label: String,
layout: String,
links: Array
}
};
</script>
k-field takes care of the label again, and the k-collection component shows our links as a list or as cards, depending on the layout.
Run npm run dev in the linksfield folder to compile the component.
The blueprint
We add the field to the site.yml blueprint, so that the links show up on the start page of the Panel. Each link uses the keys the k-collection component expects for its items: a text, an info, the link with its target and an image, for which we use a white icon on a black background in a 3/2 ratio.
title: Site
fields:
links:
type: links
label: Getting started
layout: cards
links:
- text: Guide
info: Get an overview of all the cool things you can do with Kirby
link: https://getkirby.com/docs/guide
target: _blank
image:
icon: book
back: black
color: white
ratio: 3/2
- text: Cookbook
info: Yummy step by step recipes for all sorts of usage tastes
link: https://getkirby.com/docs/cookbook
target: _blank
image:
icon: palette
back: black
color: white
ratio: 3/2
- text: Reference
info: Discover Kirby's API in all detail
link: https://getkirby.com/docs/reference
target: _blank
image:
icon: list-bullet
back: black
color: white
ratio: 3/2
- text: Plugins
info: Find plugins to extend Kirby's core functionality and make the most of your site
link: https://plugins.getkirby.com
target: _blank
image:
icon: box
back: black
color: white
ratio: 3/2
- text: Forum
info: Meet Kirby's lovely community and get help whenever you get stuck
link: https://forum.getkirby.com
target: _blank
image:
icon: users
back: black
color: white
ratio: 3/2
Change the layout to list to see the links as a list instead.
The links don't have to come from the blueprint, either. The PHP class of the field could just as well fetch them from a database or an API in its props() method, before Kirby hands them to the component.