jploch Posted August 16, 2022 Share Posted August 16, 2022 Hey folks. I have set the familiy tab to only allow one template for chldren and also set the childNameFormat. Now the user can create a page in one step. However the new page is unpublished by default and the user needs to press the "publish" button to publish, wich is usually what you want. But for my usecase I want to force the client to allways publish without the need to press the button. Is there a way to auto publish the new page? I tried it with an after save hook, and the page is then published but has a strange flash icon and seems to be corrupted. Link to comment Share on other sites More sharing options...
Jan Romero Posted August 16, 2022 Share Posted August 16, 2022 When ProcessPageAdd quick-creates a page, it gives it a temporary status Page::statusTemp that is removed when the page is next saved using the UI. I imagine you need to remove this status in your hook to make the lightning symbol go away. The temporary status is there so pages that were accidentally created can be cleaned up automatically. I would probably hook ProcessPageAdd::processQuickAdd instead of Pages::save: https://github.com/processwire/processwire/blob/master/wire/modules/Process/ProcessPageAdd/ProcessPageAdd.module#L1091 Edit: Can’t get processQuickAdd to work… Maybe because it redirects the hook doesn’t get a chance to run? Still, it should be sufficient to hook into ProcessPageEdit::execute: wire()->addHookBefore('ProcessPageEdit::execute', function(HookEvent $event) { /** @var Page $page */ $page = $event->object->getPage(); //if the page has statusTemp it was probably created by ProcessPageAdd::processQuickAdd if ($page->hasStatus(Page::statusTemp)) { $page->removeStatus(Page::statusTemp); $page->removeStatus(Page::statusUnpublished); $page->save(['quiet' => true, 'noHooks' => true, 'noFields' => true]); wire()->message('Auto-published that for you. You’re welcome.'); } }); 3 Link to comment Share on other sites More sharing options...
zoeck Posted August 16, 2022 Share Posted August 16, 2022 Bernhard has already posted a nice code example here ? 2 1 Link to comment Share on other sites More sharing options...
jploch Posted August 16, 2022 Author Share Posted August 16, 2022 Thanks! @Jan Romero and @zoeck. Berhards hook worked for me! Nice! It doesn't really matter now, but this was not working somehow: $this->addHookAfter('ProcessPageAdd::processQuickAdd', function (HookEvent $event) { $page = $event->arguments(0); $page->removeStatus('statusTemp'); $page->save(); }); 1 Link to comment Share on other sites More sharing options...
Jan Romero Posted August 16, 2022 Share Posted August 16, 2022 Yes, I couldn’t get it to work either, I amended my comment ? Bernhard’s snippet has a syntax error, here’s the working code: wire()->addHookAfter('Pages::saveReady(template=foo, id=0, status&'.Page::statusTemp.')', function($event) { $event->arguments(0)->status = 1; }); I also added the status check, because you don’t need this to run for every new page. API-created pages will be published by default, for example. Also I’m not sure I would just set the status to 1, in case PW or some other hook wants to set something else, like Locked or Draft or something. Probably better to just remove the ones you want to remove specifically. 2 Link to comment Share on other sites More sharing options...
bernhard Posted August 16, 2022 Share Posted August 16, 2022 58 minutes ago, Jan Romero said: Bernhard’s snippet has a syntax error, here’s the working code: Where? Thx fixed it ? 1 hour ago, Jan Romero said: wire()->message('Auto-published that for you. You’re welcome.'); Yeah, that's an improvement ? It's always good to add a note somewhere when doing magic via hooks to make debugging easier! 3 Link to comment Share on other sites More sharing options...
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