Jump to content

AddHookAfter("save"...) on Page template ==("")


Recommended Posts

Greetings to all processwire fans,

I am new in processwire and I have used it since 3 weeks. Probably it's a really silly question but does someone now how I can execute a function in my own module, which is triggered when I save a pages, which contains a user-defined template. I tried to to this with an basic if/else condition to trigger the hook when the page has theTemplateName== "". Unfortunately it doesn't work. So I try this which might be the wrong Syntax. Perhaps someone can give me a hint ;) THANKS a lot! 

 

    public function init() {

        // add a hook after the $pages->save, to issue a notice every time a page is saved
   
        $this->page->template == "templateName"->addHookAfter('save', $this, 'example1');
 
    }

 

Link to comment
Share on other sites

Hi, welcome to PW forums.

Looks like you have not the right PHP syntax. Try this:

public function init() {
    // you need a condition that will result to true
    if($this->page->template->name == 'templateName') {
		$this->addHookAfter('save', $this, 'example1');
    }
}

 

  • Like 1
Link to comment
Share on other sites

12 minutes ago, horst said:

Hi, welcome to PW forums.

Looks like you have not the right PHP syntax. Try this:


public function init() {
    // you need a condition that will result to true
    if($this->page->template->name == 'templateName') {
		$this->addHookAfter('save', $this, 'example1');
    }
}

 

Thanks for your help! Seems to be the right syntax now but unfortunately the function will not be fired. I also tried different templates...:'(

Link to comment
Share on other sites

6 minutes ago, adrian said:

The page object is not available in init(). You need to use ready()

public function ready() {
        if($this->page->template->name == 'TemplateName') {
            $this->addHookAfter('save', $this, 'syncMobileDE');
        }
    }

Also tried this. No function execution :( 

Link to comment
Share on other sites

Needs the Pages class in there somewhere. Either:

public function ready() {
    if($this->page->template->name == 'TemplateName') {
        $this->pages->addHookAfter('save', $this, 'syncMobileDE');
    }
}

Or:

public function ready() {
    if($this->page->template->name == 'TemplateName') {
        $this->addHookAfter('Pages::save', $this, 'syncMobileDE');
    }
}

 

  • Like 2
Link to comment
Share on other sites

1 hour ago, Robin S said:

Needs the Pages class in there somewhere. Either:


public function ready() {
    if($this->page->template->name == 'TemplateName') {
        $this->pages->addHookAfter('save', $this, 'syncMobileDE');
    }
}

Or:


public function ready() {
    if($this->page->template->name == 'TemplateName') {
        $this->addHookAfter('Pages::save', $this, 'syncMobileDE');
    }
}

 

Unfortunately not! :-[ It just works if I save any of the pages – regardless which template the page is using. 

 public function init() {
        $this->pages->addHookAfter('Pages::save', $this, 'syncMobileDE');
}

 

Link to comment
Share on other sites

9 hours ago, Mathroth said:

Unfortunately not! :-[ It just works if I save any of the pages – regardless which template the page is using. 


 public function init() {
        $this->pages->addHookAfter('Pages::save', $this, 'syncMobileDE');
}

You have to perform the check for the template name inside your hook function. $this->page or wire('page') returns whatever page in which the module code is executed (admin page editor, custom page with the code...). Pages::save on the other hand is called with the page object being saved, which is what you're looking for.

public function init() {
        $this->pages->addHookAfter('Pages::save', $this, 'syncMobileDE');
}

protected function syncMobileDE(HookEvent $event) {
	$page = $event->arguments(0);
	if($page->template->name == 'TemplateName') {
		// Run your code
	}
}

 

  • Like 7
Link to comment
Share on other sites

On 3.4.2017 at 8:50 AM, BitPoet said:

You have to perform the check for the template name inside your hook function. $this->page or wire('page') returns whatever page in which the module code is executed (admin page editor, custom page with the code...). Pages::save on the other hand is called with the page object being saved, which is what you're looking for.


public function init() {
        $this->pages->addHookAfter('Pages::save', $this, 'syncMobileDE');
}

protected function syncMobileDE(HookEvent $event) {
	$page = $event->arguments(0);
	if($page->template->name == 'TemplateName') {
		// Run your code
	}
}

 

You made my day, now it works. Looks very logical and works quite well. Thanks to all for your advises. Have a nice weekend! 

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

×
×
  • Create New...