Kirby3 AutoID - unique ID for pages, files and structures to create safe relations

I am creating posts for my plugins to make it easier to find them using the forum search and not just the docs search.

My AutoID Plugin adds an automatic unique ID for Pages, Files and nested Structures including performant helpers to retrieve them to your Kirby installation. Bonus: Tiny-URL.

Kirby does not (yet) have a persistent unique id for Page- and File-Objects, which could be useful in various situations. Using the $page->id() will not solve this since it changes when the $page->slug() / $page->url() changes, Files could get renamed. What would be needed was a Field defined in Page/File Blueprints where that unique id will be stored and some logic to generate it automatically (using Hooks). Thus the idea of autoid was born.

To sum it up with the AutoID Plugin you can solve problems like these:

  • Automatically get an unique id for each Page/File which has the autoid Field in its Blueprint.
  • Store a reference to a Page/File which does not break if that objects or parents are renamed.
  • Get a Page/File quickly using that reference.
 content:
  type: fields
  fields:
    text:
      type: textarea
    autoid:             # <-------
      type: autoid  
special:
  label: Special Child
  type: checkboxes
  options: query
  query:
    fetch: page.children.filterBy("template", "special")
    text: "{{ page.title }}"
    value: "{{ page.AUTOID }}" # uppercase is recommended
$autoid = 'any-autoid-value';

$result = autoid($autoid); // global helper function
// or
$result = $page->myFieldWithAutoIDReference()->fromAutoID(); // fieldMethod

if(is_a($result, 'Kirby\Cms\Page')) {
    // got a Page
} elseif(is_a($result, 'Kirby\Cms\File')) {
    // got a File
} elseif(is_a($result, 'Kirby\Cms\StructureObject')) {
    // got a StructureObject
    // $result->myFieldname()
    // $result->id: $autoid
    // $result->parent: Site|Page-Object hosting the Structure
}