Unique ID on the cheap

Hello. I’m looking to generate a unique ID for a page or a structure item without relying on a plugin. Is it possible to set the default value of a field to the current time in milliseconds, say with microtime() or uniqid(). This doesn’t have to be absolutely bulletproof, but just better than nothing.

Just out of curiosty, whats wrong with using AutoID?

Hello @jimbobrjames, I’m sure your plugin is excellent. I just tend to avoid third-party tools when possible. The less out-of-the box tools I have installed, the safer I feel with my builds. Slim and simple whenever possible. In the end I may wind up exploring your plugin, but for now I wanted to explore any in-the-box options.

Its not my plugin :slight_smile: it belongs to @bnomei

There have been few discussions about this over the years - ive used time stamps myself but its not entirely solid.

How many pages are we talking about here? Theres an emergency system that describes each part of the world as a three word code. You could probably adopt the logic and get a random id via a page create hook. If you put 40 words in 3 arrays and pick one from each at random from each so you end up with a kebab string like “fox-book-halo” - that should be enough :slight_smile:

Then you can just stamp that into a hidden field as your unique ID via the page create hook. If that ends up not being quite random enough, you can just add more words to the arrays.

Ah yes. I see.

There will be no more than say 50 of these items. How do you go about populating a hidden field with something like uniqid()?

Well that will give you a unique ID based on the current time. The reason thats not safe is because there is a chance you could create a page the next day at the exact same time. I know the chance of that are probably really slim, but it would leave you with two pages with the same ID if it happened.

You can update the content of a page programatically via $page->update()

I have no idea how to do this with structure fields but @texnixe might be able to help there.

In this case, the chance that two users will create the same entry at the same millisecond would be extremely rare. I do understand that this isn’t a 100% enterprise level solution, but if I could save that now time value in a field that will be enough.

Fair enough… should be pretty simple then. Just use $page->update() in a page create hook with uniqueid() as the field value.

Got it. Thanks for your time. So there’s no way to prepopulate a field to include that value? Some query language magic?

Not sure i follow - that is pre-populating it. The File create hook fires when you create a new page in the panel, which in turn will populate that field with a unique id.

I was thinking of setting the default value of a field using the query language. Maybe there’s some advanced ability that could allow me to call that uniqueid() function.

What you could do is use a page model to populate a field, and as I already said in another thread I’d prefer that compared to a hook. Query language currently doesn’t work for default values (although I think it’s on the roadmap).

You could use uniqid() function to get a unique string or Kirby’s build-in Str::random() method:

Thanks @texnixe.

I did wind up using a model for those location entries, as per your suggestion. Works exactly as expected, defaults to unlisted. I should read up on models a bit more, seems more solid than a hooks.

1 Like