$page->save() in plugin does not do anything, no errors either

In my Simple Events plugin, I would like to set the unpublish date of a new Event page programmatically. To this end, I am getting a list of all pages with the Event type on a cache rebuild, checking each one’s unpublish date, and if it’s not set, I would like to set it and then save that in the page’s frontmatter. Most of that is working well so far. Here is my code, annotated:

if ($this->checkUnpublishedDates) {
  // set unpublish datetime to header.start/header.end plus time set in options.
  $pages = $this->grav['pages'];
  $events = $pages->all()->ofType('event');

  foreach($events as $e) {
    if (!$e->unpublishDate()) {
      $time = " 0:00";
      if (!empty($this->grav['config']->plugins['simple-events']['unpublish_time'])) {
        $time = " ".$this->grav['config']->plugins['simple-events']['unpublish_time'];
      $datetime = $e->header()->start.$time;
      // dump($e->unpublishDate()) at this point gives me the new timestamp correctly
      $e->save(); // no errors, but nothing gets saved

From what I gather from the Grav API and other plugins, this should actually work. So why doesn’t it actually save anything? :thinking:

I’m never sure it’s useful to suggest things when I don’t know the answer but nothing else has been offered …

I looked up the source for the Page->save() method seems it doesn’t handle anything unexpected at all. I wonder if the if ($file) predicate is returning true. Seems like the method will do (almost) nothing if it’s false and the docs say “Save page if there’s a file assigned to it.” (my emphasis). So I think you should put some debugging in there and see if that predicate is true, and then probably look at finding out why not.

Hope that helps you track it down.

Hello hughbris, and thank you for your reply!

Yes, I did stumble over that little if clause as well, but wasn’t sure what to do about it. I did not quite realise I could easily check for this by putting dump($e->file()); in my code though… :laughing: I have now tried this, and while I actually get an output for that, it is the wrong file. So that probably means there’s no file assigned to pages when I get them as a collection.

Now… does anybody know how to assign the respective files to the pages of a collection, perhaps? :smile: