entschleunigung Posted July 27, 2018 Share Posted July 27, 2018 Hello, i'm writing my first module to learn and i would give my editors a handy way to unpublish pages. (i know this is possible with the settings-tab, but i want to solve it this way) on a page i have a checkbox "not active". if a editor check the box, pw should unpublish the page. for this i modified the helloworld-module like this but nothing changing on the page. how should i do this? thx public function init() { $this->pages->addHookAfter('save', $this, 'checkStateAndHide'); } public function checkStateAndHide($event) { $page = $event->arguments[0]; if(!$page->active){ $this->message("{$page->title} now hidden"); $this->page->addStatus(Page::statusUnpublished); } } Link to comment Share on other sites More sharing options...
dragan Posted July 28, 2018 Share Posted July 28, 2018 I don't see any page->save() in your code... Link to comment Share on other sites More sharing options...
entschleunigung Posted July 28, 2018 Author Share Posted July 28, 2018 hi dragan, i tried page->save() but i ran always in "internal server error". i think page->save() and this hook addHookAfter('save', $this, 'checkStateAndHide') looped everytime. i can't solved it. Link to comment Share on other sites More sharing options...
teppo Posted July 28, 2018 Share Posted July 28, 2018 What you probably want to do is hook after Pages::saveReady instead. Something like this, perhaps: public function init() { $this->pages->addHookAfter('saveReady', $this, 'checkStateAndHide'); } public function checkStateAndHide($event) { $page = $event->arguments[0]; if(!$page->active){ $this->message("{$page->title} now hidden"); $page->addStatus(Page::statusUnpublished); } } Written in browser and not tested, but that's the general idea anyway. Note also the $this->page->addStatus() -> $page->addStatus() change: here you're trying to change the status of $page, not a class property $this->page. This should also work if you hook before Pages::save and just set the value there, but in my opinion saveReady is usually what you should use. If you hook after Pages::save, the page has already been saved, so you'd need to do another $page->save() or $page->save('field') in order to save any changes – but f you hook before Pages::save or to Pages::saveReady, the page has not yet been saved, so you can just set the value and it will be stored soon enough ? (By the way, you might want to check that the template of this page actually has "active" field – otherwise you could end up in a situation where pages without this field will be unpublished each and every time they're saved.) 3 Link to comment Share on other sites More sharing options...
entschleunigung Posted July 28, 2018 Author Share Posted July 28, 2018 hi teppo, muchas gracias, i will try it on monday in office, but i think i could work. 4 minutes ago, teppo said: By the way, you might want to check that the template of this page actually has "active" field – otherwise you could end up in a situation where pages without this field will be unpublished each and every time they're saved of course ? 1 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