AutoID Plugin (create unique page ids)

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!

15 Likes

Hello,

Thank for the Plugin! But i install the Plugin and become the Error:

Fatal error: Cannot redeclare class AutoIdPlugin in E:\xampp\htdocs\k212\site\fields\autoid\autoid.php on line 13

I work with Kirby 2.2.1 on my Localhost.

Greeting JanG

This error means that you included the plugin twice. Could you please check whether the plugin has been installed to another file as well?

I don’t think so, I get the same error message and there is definitely no second file either.

Hm, that’s very strange. That basically means that somehow Kirby loads the file twice.

1 Like

So it seems :confused:

We found the bug. “autoid.php” is a plugin, not a field. The description was wrong.

So just move
/site/field/autoid/
to
/site/plugins/autoid/
and everything will work finde.

Please check out the lastest build at https://github.com/helllicht/kirby-autoid/tree/master (1.1). We fixed a KIRBY 2.2.1 bug, related to this issue https://github.com/getkirby/panel/issues/667. But you can use the plugin with 2.2.1.

1 Like

Hm … but now Kirby say: The Directory could not be moved ?

How are you trying to move the directory?

I am only install the Plugin as described. Then i go to an older Page und save the Page. Then, the AutoID probably should be forgiven? When i save the Page i get the error message.

Have you moved the plugin as described above?
Your folder structure should then look like this:

site
  plugins
    autoid
        autoid.php
        ...

As the latest version also had a small bug, it might be best to get the latest version.

Hm, the plugin is working now but now it’s not generating an Id for existing pages when I press ‘Save’, the field stays blank. It only adds an id when I create a new page.

Yes, i have the new version. And now it works … by new sites!

My fault, i follow this here in the post above

to
/site/plugins/autoid.php
and everything will work finde.

That would have to look like this

/site/plugins/autoid/autoid.php

As it is properly described in the documentation :wink:

Thats right Irvin, read here

You’re right! Will edit our answer above. Good to hear that it’s working now.

Thanks, JanG.

Irvin, the function will be aviable as soon the hook-bug is fixed. Maybe this will be part of version 2.2.2.

Update for 2.2.2

An update is now available for kirby/panel version 2.2.2. Auto Id will work for new pages and updates (save).

1 Like

Great and very useful little plugin!

I’ve created a kirbytag which allows you to use the autoid inside a mardown textarea as link tag, e.g. (autoid: 5 text: Linktext). If no text is given the targets page title will be used.

Here’s my fork and the pull request :wink:

Would love to get some feedback from you, @helllicht

2 Likes

Hey Flo,

great that you enjoy our plugin!
I’ve just commented on your pull request over on github

While we think it’s a great addition to our plugin, we also see some challenges:

  1. You would have to know the id of the page you’re referencing to. Unlike with images you can’t just drag & drop the pages from the sidebar. But while it’s inconvenient, of course it’s not a deal breaker.
  2. Kirbytags are supposed to live inside the /site/tags folder as separate .php files. Placing the tag inside the plugin does work, but it’s not very clean. In this case it would probably be better to split it up into a dedicated kirby tag to keep things neat and tidy.

Hey @helllicht,
how can I convert the autoId in a page object then. With the url you can fetch the object with ->toPage but I didn’t found a way to covert any other field value to a page object?

1 Like