Jump to content

Hooking into Inline CKEditor Saving


GKM490
 Share

Recommended Posts

Hi there,

I am trying to integrate Algolia into our website, and I'm wanting to hook into the saving of an inline CKEditor Content field on a page, so that when you double click, edit the content, and hit save, it updates the Search Index.

Currently, I have this hook in ready.php:

$this->addHookAfter('Pages::savedPageOrField', function(HookEvent $event) {
    $page = $event->arguments(0);
    wire('log')->save('debug', 'Saving Field');
});

Just to try and confirm that the event is executing, but when I save the content inline on a page, nothing gets added to my log. I have also tried with the Pages::savedField event and it also doesn't work. Is there a different hook I should be using?

Thanks!

Link to comment
Share on other sites

  • 3 weeks later...

I've had a look and found the issue: ready.php is never triggered on frontend editing. You can attach your hook in /site/init.php for example and then it should work.

The hooks are the same as for regular page saves: You can hook into Pages::saveReady or Pages::saved for example.

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

2 hours ago, bernhard said:

I've had a look and found the issue: ready.php is never triggered on frontend editing. You can attach your hook in /site/init.php for example and then it should work.

This worked perfectly! I would never have guessed to put it in init.php. The commented line at the top:

When this file is called, the current $page has not yet been determined.

Made me think that I wouldn't be able to use $page = $event->arguments(0); to grab the necessary data about the page to save in the search index.

Thank you so much for the response. Much appreciated!

Link to comment
Share on other sites

Glad it helped.

$page is not available OUTSIDE of your hook in init.php, but INSIDE the hook it is available since the inner part of the hook is executed at a different time (after/before Pages::saveReady or Pages::saved).

In your hook you get the saved page from the first argument of your HookEvent. That's why you do $page = $event->arguments(0)

See also https://processwire.com/talk/topic/18037-2-date-fields-how-to-ensure-the-second-date-is-higher/?do=findComment&comment=158164

Link to comment
Share on other sites

Ohh I had been treating ready.php as init.php then. I only have hooks in ready.php and didn't realize that $page existed in that file outside the context of a hook. So that's what the comments at the top were referring to, haha.

Thanks for the clarification! That helps me understand the distinction. 🙂

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