Jump to content

Page status invalid when using saveReady hook


adamspruijt
 Share

Recommended Posts

Hope the title is at least a little bit descriptive. I will try my best to explain what seems like an incredibly strange and probably very rare issue.

I have the follow code in the init.php file (using latest PW dev). The code below basically covert a date string like say "first Monday of March 2016" into a usable date while making sure the date is in the future. 

$pages->addHookBefore('save', function($event) {

	$page = $event->arguments[0];
	if ($page->template != "holiday") return;
	if (!$page->dev->date_string) return;

	$year = date("Y");
	$date = strtotime("{$page->dev->date_string} {$year}");
	if ($date < (int) date("U")) {
		$year++;
		$date = strtotime("{$page->dev->date_string} $year");
	}

	
	$this->message("Date set to " . date("Y-m-d",$date));
	$page->date = $date;
	

});

It works just fine as written above, but originally I was using this

$pages->addHookAfter('saveReady', function($event) {

	$page = $event->arguments[0];
	if ($page->template != "holiday") return;
	if (!$page->dev->date_string) return;

	$year = date("Y");
	$date = strtotime("{$page->dev->date_string} {$year}");
	if ($date < (int) date("U")) {
		$year++;
		$date = strtotime("{$page->dev->date_string} $year");
	}

	
	$this->message("Date set to " . date("Y-m-d",$date));
	$page->date = "{$page->dev->date_string} $year";

});

The only difference being. using "after saveReady" vs "before save". Either setup works fine on my local dev environment for those wondering. But on the live website, when a page using this code get saved it sorta gets "corrupted". The page becomes unviewable even if it is published and not hidden and the images and dev tab sets disappear. When the page is set back to unpublished thew fields are visible again.

I got looking in my database after a great deal of frustration with these "corrupted" pages and saw that the trouble pages had strange status values. (17562, 141313, etc). Not sure why the status is being affected...

For now I have obviously left the working code in place, but thought I should share this strange issue, maybe its a processwire bug? Maybe a hosting issue (I also find I have to access images differently on my dev machine vs my live server, $image vs $image->first, maybe related)?

Hope this can help in some capacity is anyone else has had a similar issue or if its a PW bug. Not sure when I'll be back to take a look at this thread, but thanks ahead of time for taking a look.  :)

  • Like 1
Link to comment
Share on other sites

I experienced some wierd issues with saveReady before and after and am still waiting for the mystery to be solved :) Mentioned it somewhere but can't remember where.

Also happened to have corrupt status in db. I think I reported those but couldnt tell why it happened, neither Ryan.

On mobile so too lazy to look it up.

  • Like 2
Link to comment
Share on other sites

Ok, thanks Soma, I guess it good to know that I'm not alone with the issue. Hopefully better chance we can find a solution. I kinda wish the error was happening on my dev environment and not on the live site, would be a lot easy to try and help find the bug then.

Link to comment
Share on other sites

  • 1 year later...

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