Jump to content

page->addStatus in a module


Recommended Posts

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

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.)

  • Like 3
Link to comment
Share on other sites

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 ?

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...