We’re currently redesigning our website and needed a way to reference pages to each other. We needed a bullet-proof way because the referenced page names/urls are likely to change in the future.
Since kirby doesn’t natively come with unique ids, we developed a Plugin which does exactly that (link at the bottom).
The plugin writes a unique id (numeric or hash) into a predefined field of every page you create. It also works for existing pages – just edit them and hit save once.
Usage
In your blueprint, add a new field and use autoid as the field name. This way the plugin knows on which field to act on. The fieldtype is up to you, but we highly recommend to make it read-only.
fields:
autoid:
type: text
readonly: true
Example Use Case
Let’s say you want to have a field that allows you to add related projects to a project page. Normally you would query each sibling and reference them by their name/url/uid. But what if they names change? You would need to update each reference individually.
AutoIDs to the rescue! You can use the autoid
field to uniquely reference the projects. This way even if the project names/urls change, the references won’t break.
fields:
relatedprojects:
label: Related Projects
type: checkboxes
options: query
query:
fetch: siblings
value: '{{autoid}}'
text: '{{title}}'
Options
Field Name
Some of you might want to have a custom name for your autoid field. You can override the name inside the site/config.php
file of your Kirby installation.
c::set('autoid.name', 'yourcustomfieldname');
This allows you to use yourcustomfieldname
as the field name for your autoid field.
Type
If you’re working in a larger team, you might run into problems using numeric ids. If more than one teammember creates content in their respective local repositories, pages will end up getting the same ids, which kind of defeats the purpose of this plugin.
So we built in a option to use unique md5 hashes instead. They are based on a microtimestamp + your session id. This way it’s (nearly) impossible to generate the same hash again.
c::set('autoid.type', 'hash');
Here’s the link to our Plugin Page on Github.
We’d love to hear your thoughts and appreciate suggestions for improvements!