Xonox Posted Tuesday at 10:55 AM Posted Tuesday at 10:55 AM Hi everyone, I'm trying to achieve the following (for the template="race") in the back-end: User clicks "New"; Instead of seeing the Title and Name fields, it just creates the page with an automatic name (timestamp) and goes to page editing with the fields he needs. I'm doiing this because the title and the name are not really important for this content, and I don't want the back-end user to be bothered with information that is not clear to him. I already made the title not mandatory, so only the name should be set up. I have this code, that is not working: // Automatically generate a name for a race, so the user doesn't go throught this step $this->addHookAfter('ProcessPageAdd::execute', function($event) { // Get the page being created $page = $event->arguments(0); if($page->template->name == 'race') { // Turn off output formatting $page->of(false); // Make the timestamp as the page name and move on $page->name = time(); $page->save(); } }); For what I understand, at this point the page doesn't exist yet, so that's probably why this is not working. So I'm not really sure how to use the hooks to: attribute the name; save the page.
cwsoft Posted Tuesday at 05:52 PM Posted Tuesday at 05:52 PM (edited) Hi, maybe another hook event in site/ready.php works better. Make the title and name field not mandatory if needed an hide them from the user input via the PW backend page/template settings. // Note: Uses PW Function-API for sanitizer() call inside the function. You may need to enable this feature in yur PW config to work. $wire->addHookAfter('Pages::saveReady(template=your-template)', function (HookEvent $event) { // Ensure we have a valid event page. $page = $event->arguments(0); if (!$page || $page->isTrash) return; // Set a default title for newly created pages not yet saved, just created and shown in the PW admin backend. if ($page->id == 0) { $page->title = sanitizer()->name('your-unique-timestamp'); // or whatever sanitizer fits your needs. return; } // Check if there already exists a page with the same 'unique-timestamp' for actual page to be saved if needed. // If that could ever happen, you may want show a message and skip actual page from beeing saved. $tsPageAlreadyExists = false; // add whatever test will fit your needs if ($tsPageAlreadyExists) { $page->error("Page timestamp already exists."); $event->replace = true; return; } // If we end up here, we can save the page with a unique timestamp. $page->title = sanitizer()->name('your-unique-timestamp'); // or whatever sanitizer fits your needs. $page->name = sanitizer()->pageName('your-unique-timestamp'); }); P.S.: Written on an iPad out of my head, so it may our may not work 🙂 Cheers Edited Tuesday at 06:18 PM by cwsoft
Xonox Posted 21 hours ago Author Posted 21 hours ago @cwsoft thanks a lot for your help. It turned out to be a bit more complex than what you posted, unfortunately! I'm leaving the full code here, if anyone needs it. // Automatically generate a name for a new race page, // so the user doesn't go throught this step. // It will immediately land on the page editing screen with the page // already created $wire->addHookBefore('ProcessPageAdd::execute', function(HookEvent $event) { $input = wire('input'); $pages = wire('pages'); $session = wire('session'); // Let's generate a random string to append to name and avoid collisions $rand = random_int(0,9) . chr(random_int(97,122)) . random_int(0,9) . chr(random_int(97,122)); // Only when opening the Add New screen (GET request, no form submit yet) if(!$input->requestMethod('GET')) return; $parentID = (int) $input->get('parent_id'); if(!$parentID) return; $parent = $pages->get($parentID); if(!$parent->id) return; // Parent only allows "race" anyway, but let's be explicit $template = wire('templates')->get('race'); if(!$template) return; // Optional safety: only do this when the template is allowed under this parent if(!$parent->template->childTemplates || !in_array($template->id, $parent->template->childTemplates)) return; // Create page $p = new Page(); $p->template = $template; $p->parent = $parent; // Name must be unique + sanitized (prefix avoids numeric-only names) $p->name = 'race-' . time() . '-' . $rand; // Keep it unpublished until user saves/publishes $p->addStatus(Page::statusUnpublished); $pages->save($p); // Redirect straight to edit screen $session->redirect($p->editUrl); // Stop normal ProcessPageAdd rendering $event->replace = true; $event->return = ''; });
monollonom Posted 19 hours ago Posted 19 hours ago Is "race" the only template that can be used for children of your page? If so you could instead use the name format to skip the Page Add step 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now