Jump to content
monollonom

[SOLVED-ish] Saving the homepage from guest session removes image references

Recommended Posts

Posted (edited)

(I edited the title to reflect my findings down below, I'm leaving the original post as is though)

Hi,

This is the first time I'm using LazyCron so maybe I did something wrong but basically when the hook function is triggered and does its thing to the homepage, all images within that page disappear from the database, while remaining in the file system. When I try to re-upload the same image it tells me "Refused file X because it is already on the file system and owned by a different field" (couldn't find that other field though). Has anybody encountered something like this before ?

Here is my hook, from ready.php:

$wire->addHook("LazyCron::everyHour", function() {
	$home = wire("pages")->get(1);
	$home->of(false);
	$nextMonth = strtotime("next month");
	$newEvents = wire("pages")->find("template=event, end_date>=today, start_date<$nextMonth, limit=10, sort=start_date, sort=end_date, sort=title");
	foreach($home->events as $event) {
		if (!$newEvents->has($event)) {
			$home->events->remove($event);
		}
	}
	foreach($newEvents as $new) {
		if (!$home->events->has($new)) {
			$home->events->add($new);
		}
	}
	$home->save();
});

$home->events is a Page Reference field. The idea is to update the field with the ten closest events and allow the editor to sort them in the admin.

Thank you !

Edited by monollonom
Changed title of the topic

Share this post


Link to post
Share on other sites

After you get the home page you should set its output formatting state to false because you are about to manipulate and save it.

$home = wire("pages")->get(1);
$home->of(false);

 

Share this post


Link to post
Share on other sites

Yes at first I did set the output formatting to "false" but wanted to try and see how it goes without it, with no luck. I'll edit my post though, thanks.

(also for completeness, I'll try again with of(false) just to be 100% sure)

Share this post


Link to post
Share on other sites
Posted (edited)

After some more testing, it looks like it's not because of the hook or the LazyCron but because of the code being executed by a guest.

Edit: it's actually just the fact that I save the homepage (from a guest session) that makes the images disappear.

I have access control toggled on for the homepage template but I thought API calls would be respected, am I wrong ?

Edited by monollonom

Share this post


Link to post
Share on other sites

In the end it seems to work if I specifically save the Page Reference field:

$home->save("events");

I'm still not sure though why saving the page from a guest session would break the link between images and their field...

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...