Problems, need help? Have a tip or advice? Post it here.
8 posts Page 1 of 1
Hello Admin,
This is Nana Ampaw,
I tried using the page presave event to append the UID to the page names for a particular template but it diesnt work unless I save the page twice. It seems the page is saved before the UID is generated. Pleas help me with this issue. Thank you
Nana, if it is not about UID addon, then do not use php event and try to use cms:persist tag in template's config_form_view.
Hello trendoman,
I am using the UID addon.
nanaampaw wrote: Hello trendoman,
I am using the UID addon.

In such case, saving pages with cms:db_persist tag once in a loop will add UIDs to all you missing pages. There is no need to use events.
Hi trendoman,
What I actually meant was that, I am using the UID addon so that anytime I save a page a UID number is generated for the page.

However what I want to be able to do now is that I want to modify how page names are generated so that each time I save a new cloned page the page name will be something like.

That was why I was using the page pre save event but the UID number is not appending to the page name on saving.

I hope this makes it clearer

NB: from a solution I found on couch I am able to append page field values to the page name on saving a cloned page so I thought it would work for UID but unfortunately it doesn't
Aha, now everyone understands what you are trying to do. Please, do everyone a favor - explain yourself next time in the first post, not in the 3rd. It is really a problem to see things the way you see them without watching over the shoulder.
TLDR: By all means avoid wasting our time and probably your post will not be ignored next time ;)

Back to the topic. If you are a fan of PHP, open couch/page.php and see where the page_presave event happens. It happens too early - way before a new page gets added to db. So, my advice is this - use a hook alter_page_save which happens after the new page is processed and all fields should be available for modification.
I think my experiments were successful. After a very deep dive into uid addon and the way fields are structured and validated, I decided to allow admin to customize the required page name change. Some people will like a fully-written UID appended to page name, with suffix and prefix. Some people will need only a basic core number in the page name.

The solution to customization is in writing a validator with argument in the editable field's definition. Nothing can be simpler for a designer - just write in a validator name. Here I am taking an original field definition with UID by KK and add a custom validator there -

Code: Select all
    label='Invoice Number'
    desc='will be generated automatically'

Validator's name is my custom PHP function and val_separator is used to separate, obviously, validator's name from its argument (arbitrary parameter passed to validator function). I chose the argument to be a full or any other word (short, etc) - to indicate whether we take UID in the full form as in AT-0000458-20200521 or its short form 0000458 or, simply, 458.

So, the argument 'full' in validator makes the page 'My Product' name look like my-product-at-0000458-20200521. If such form is not required, then a line validator='pagename_plus_uid' is enough - this makes the page name to be my-product-458.

If you have any questions along the writing your own solution - whether it is a validator without event-hooking or just a quick hook on mentioned event, feel free to post questions to this thread. If you don't have time to do the dirty work I will gladly share tested polished and approved code in PM ($10 will make my day).
Thanks admin trendoman. I will try it out and give you feedback
8 posts Page 1 of 1

Who is online

In total there are 15 users online :: 0 registered, 0 hidden and 15 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 15 guests