Jump to content

Recommended Posts

Posted

In a module I'm writing I want to run a hook function when a page is saved EXCEPT for when the module creates it's own page.

So of course the problem is that as part of creating a new page it triggers the after save hook which I don't want in that case.

I've tried with a session variable hoping that would give me a toggle (as seen below) but it's not going saving for some reason (I've tried with a blunt $_SESSION['newPageCreated'] non-PW session variable too).

Snipped down code below.  Not even sure if this approach is the best way to go so happy to hear alternative approaches if I'm trying to fit a square peg into a round hole this way.

 

	public function init() {
		// add a hook before the page is added (to auto generate a name)
		$this->pages->addHookBefore('added', $this, 'beforePageAdd');
		// add a hook after the $pages->save
		$this->pages->addHookAfter('save', $this, 'afterPageSave');
	}

	public function afterPageSave($event) {
		global $wire;
		$mypage = $event->arguments[0];
		//handle what to do if this is a modal popup
		if(in_array($mypage->template, $this->tplToTransfer) and $wire->session->get("newPageCreated") === false) {
			wire("session")->redirect($wire->config->urls->root."post/admin/close-modal.php");
		}
		$wire->session->set("newPageCreated",false);
	}

	public function beforePageAdd() {
		global $wire;
		$parid = $this->input->get->parent_id;
		$parpage = $wire->pages->get($parid);
		$newTemplate = "templatename";
		$wire->session->set("newPageCreated",true);
		if (in_array($partpl, $this->tplToTransfer)) {
			$p = new Page();
			$p->parent = $this->input->get->parent_id;
			$p->template = $newTemplate;
			$p->removeStatus(Page::statusUnpublished);
			$p->save();
			$wire->session->redirect("../edit/?id=$p->id");
		}
	}

 

Posted

I've found a way around this - by adding an extra GET variable to the pw-modal link that I use to create the new page then checking against it.  Still may not be the best way so if others have better solutions I'm happy to learn.

Posted

A small idea is to add a checkbox field - "created_by_module" - to the template and check the box when the page is created by the module. Then in the hook you can check if this field is checked or not.

 

3 hours ago, Loges said:

global $wire;

please have a look to this post :

 

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
  • Recently Browsing   0 members

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