Jump to content


Photo

Custom Template save validation


  • Please log in to reply
3 replies to this topic

#1 adamkiss

adamkiss

    Master of the universe

  • Moderators
  • 1,078 posts
  • 289

Posted 06 March 2012 - 09:08 AM

Hey, is there anyway I can hook onto page::save() process, do custom validation and either accept changes or reject changes?

#2 ryan

ryan

    Hero Member

  • Administrators
  • 5,780 posts
  • 3125

  • LocationAtlanta, GA

Posted 06 March 2012 - 10:47 AM

You'd have to hook into Pages::save ($pages->save), not Page::save. If you use a 'before' hook, you could throw an exception if you want to abort the save. This is what I recommend doing. But if throwing an exception doesn't meet your needs, you can also use a 'replace' hook:
http://processwire.c...ndpost__p__7435

If you use a 'replace' hook, $pages->save will never be called on it's own, so you'll have to take over the call. In order to avoid a recursive loop when you call it from your hook, you'll want to call $pages->___save() instead (which bypasses any hooks).

#3 adamkiss

adamkiss

    Master of the universe

  • Moderators
  • 1,078 posts
  • 289

Posted 07 March 2012 - 10:36 AM

Hey Ryan,

thanks. Just to illustrate (I don't want exception :)), I'd do it like this?:

  $this->addHookBefore('Pages::save', $this, 'savePageValidation');

  function savePageValidation(){
    if ($everythingIsOk === true) {
      $pages->___save();
    }
  }

?

Also how about that exception - I understand I'd probably need to try catch something, but I'm not too certain.

#4 ryan

ryan

    Hero Member

  • Administrators
  • 5,780 posts
  • 3125

  • LocationAtlanta, GA

Posted 08 March 2012 - 09:47 AM

Adam, sorry I linked to the wrong message in that thread before. Here's the instructions for the 'replace' hook:
http://processwire.c...ndpost__p__7435

You don't want to call $pages->___save() unless you are using a 'replace' hook. For all other hooks, the original method that you hooked is still called (either before or after your hook). So a 'replace' hook is different in that it tells PW you are completely taking over the implementation. These have not been used a lot, and there is potential for conflicts with other modules hooking the same function. So that's why I say it's probably better to throw an exception where possible. In that case, your hook would look like this:

function savePageValidation($event) {
	$page = $event->arguments[0];

	$everythingIsOkay = true;

	if($page->other != $page->that) {  // pseudocode
		$everthingIsOkay = false;
	}

	if(!$everythingIsOkay) throw new WireException("Aborted save because other != that");
}

A replace hook would look like this:

function savePageValidation($event) {
	$event->replace = true;
	$page = $event->arguments[0];
	$options = $event->arguments[1];

	$everythingIsOkay = true;
	if($page->other != $page->that) { // pseudocode
		$everythingIsOkay = false;
	}

	if($everythingIsOkay) {
		$event->return = wire('pages')->___save($page, $options);
	} else {
		$event->return = false;
		$this->error('Page was not saved because other != that');
	}
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users