Jump to content

addHook saveReady question


joe_g
 Share

Recommended Posts

Hi there, I have the following hook that loops through and overloads a date. It's a template type book page. This page also has the field image.

My problem is, with this code I need to save the book page twice for an image thumbnail to be generated - even there is no mentioning of the image field in the code. (I need to press publish, then save). If I only press "publish" once, the 'book' page will not get an image thumbnail. In some way the code below cancels the image generation for this page, but I can't figure out why - or how it's connected..?

 

$pages->addHookAfter('saveReady', function($event) {
    $p = $event->arguments(0);
    if($p->template->name == 'book') {
        $lastcreated = $parent->lastcreated;
        if($p->created > $lastcreated) {
            $p->parent->lastcreated = $p->created;
        }
        $p->parent->save();
    }
});

 

Link to comment
Share on other sites

If that's the exact code, you have an error. It should read "$p->parent->lastcreated", not "$parent->lastcreated". Perhaps that's the whole cause. I'd put the code in a "saved" (or even better "added", since the created property isn't changed after the first save) hook though, so that updating the parent only happens after successfully saving the book page.

<?php

$pages->addHookAfter('added', function($event) {
    $p = $event->arguments(0);
    if($p->template->name == 'book') {
      
        $parent = $p->parent;
        $lastcreated = $parent->lastcreated;
      
        if($p->created > $lastcreated) {
            $parent->lastcreated = $p->created;
        }
      
        // Save just the lastcreated field and leave modified user and time alone
        $parent->save("lastcreated", ["quiet" => true]);
    }
});

 

  • Like 4
Link to comment
Share on other sites

thanks! yes indeed — very sharp-eyed — that seemed to be causing the problem.

not sure i understand fully what you mean with created and saved vs. added. But the created date is used on purpose so that it only happens once.

Link to comment
Share on other sites

Glad to hear that helped.

What I meant regarding saved/added: the saveReady and saved hooks are executed every time you save a book page, even if it is just to amend some information. The created timestamp won't change anymore after the first save when it was added. The "added" hook will only execute once for each book page at the time it is created, so you can avoid running your code each time.

On saved/added vs. saveReady: there might be an (unlikely, but not impossible) error where something prevents saving your book page. If that happens and you use saveReady, your parent page's lastcreated field will have been updated nonetheless, which will lead to an inconsistency until the next book page is added and correctly saved so it overwrites its lastcreated field again.

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...