Jump to content

Upon saving the page, update the field on another template


Recommended Posts

Ehey, aloha everyone! I have two templates: post and blog. When publishing a first-time post, I must update the (same) field date_modified on its parent (a single) template blog. I am trying it via hook, but nothing happens. This checking for status unpublished is not working for some reason too. I appreciate any help!

$wire->addHookAfter("Pages::saveReady(template=post)", function($event) {
    $page = $event->arguments(0);
    $pages = $event->object;
    // if ($page->isNew() && $page->status->unpublished != true) {
    if ($page->isNew()) {

        $blog = wire('pages')->get('template=blog');
        $blog->date_modified = $page->date_modified;
        $blog->save($blog->date_modified); 
        // $blog->save();
    }
});
Link to comment
Share on other sites

Could be because $page->isNew() doesn't match the page as a page is only new right after it was created. So... the moment you publish that page it is already old.

$wire->addHookAfter("Pages::saveReady(template=basic-page)", function ($event) {

  $page = $event->arguments(0);
  $pages = $event->object;

  if ($page->isNew()) {
    wire()->log->save("debug", "page is new");
  } else {
    wire()->log->save("debug", "page is NOT new");
  }
});

$this->addHookAfter('Pages::published', function (HookEvent $event) {

  $pages = $event->object;
  $page = $event->arguments(0);

  // add conditionals here

  wire()->log->save("debug", "page was published");
});

Second example uses this hook: Pages::published

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Posted (edited)

@wbmnfktrthanks mate!!! I count beers I owe you.... So for anyone looking at this here is the solution:

$wire->addHookAfter("Pages::published(template=post)", function (HookEvent $event) {

    $pages = $event->object;
    $page = $event->arguments(0);

    $blog = wire('pages')->get('template=blog');
    $blog->date_modified = $page->date_modified;
    $blog->save();

  // wire()->log->save("debug", "page was published");
});

 

Edited by Leftfield
  • Like 1
Link to comment
Share on other sites

You could also simply get the parent by using the parent property. Of course you don't need to use setAndSave() but I feel it's cleaner in a situation like this.

I also like using $p rather than $p because even though the scope will prevent $page from overwriting the PW $page object, I still find it less confusing, but that's just a personal preference.

$wire->addHookAfter("Pages::published(template=post)", function (HookEvent $event) {
    $p = $event->arguments(0);
    $blog = $p->parent;
    $blog->setAndSave('date_modified', $p->date_modified);
});

Also, in case you don't know, there is a system "modified" property rather than adding a separate custom field for that. 

Another thought - if updating the modified date for the blog parent is just about using it to display on the website, perhaps you can just get the modified date of the most recently modified post, eg:

image.thumb.png.9d309ae5d7caf1cc86bba362720ed331.png

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

57 minutes ago, adrian said:

Another thought - if updating the modified date for the blog parent is just about using it to display on the website, perhaps you can just get the modified date of the most recently modified post, eg:

I thought exactly the same, but I think he wants to show the published date and not the modified date? So if anyone edited an already published blogpost the date would NOT change. At least that's what the hook does as far as I understand.

Maybe this shows that the naming of "$blog->date_modified = $page->date_modified;" might not be ideal 🙂 

  • Like 1
Link to comment
Share on other sites

1 minute ago, bernhard said:

but I think he wants to show the published date

Then we could use the system "published" property, no?

Regardless, even if there is a need for a custom date field of some sort on the post template, that could still drive that getRaw() example I posted. Not saying it's the right way to go - just highlighting some other options.

  • Like 1
Link to comment
Share on other sites

Posted (edited)

  

14 minutes ago, bernhard said:

but I think he wants to show the published date and not the modified date?

No, mate. PW (as you all know way better than me) doesn't have the option to change the default fields published and modified. Published is constant, and Modified is changing on every page save. It doesn't suit me, so I made my date field to set a date when publishing/changing so it reflects on the sitemap and tells search engines the page has been changed on that date. Not on every save.

Edited by Leftfield
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...