Handling Page attribute changes in on_page_update

Hey there.

I’m trying to build an elastic search index from my pages. Certain pages (like the search results page) should not end up in the elastic index, for these pages, I created a page attribute “Do not index”.

When the page contents change, I need to update the elastic index, however I can’t find a way to detect whether the page that’s being updated has the “Do not index” attribute. To be more precise, I cannot get the most up to date value of said attribute.
Let’s say I have a page that currently is indexed (“Do not index” is false). If I check the box for “Do not index” and submit, in my event listener I still get false as the value of “Do not index”.
Here’s the code:

...
Events::addListener('on_page_update', function (PageEvent $event) use ($session) {
    $page = $event->getPageObject();
    dd($page->getAttribute('do_not_index')); // dumps false
});
...

Is there any way to get a set of all the changes for the page, or even just the new data? Any help is greatly appreciated!

You may be able to get round it by flushing the database entities in the entity manager.

1 Like

From what I can see in the core I think when an attribute is saved, the page update method that triggers that event is called before the attribute is actually saved. And I don’t think there’s an event that would allow you to do what you want.
Have you considered using a simple task to check the pages based on date of last update and index what needs to be indexed?

1 Like

Thank you for your help. I ended up writing an async command to handle the indexing. This approach works very well

1 Like