Jump to content

[SOLVED] Advanced Hooks with conditions


nurkka
 Share

Recommended Posts

I am trying to learn more about Advanced Hooks, and the following worked perfectly:

// this works perfectly:
$this->addHookAfter( 'Pages::saved(status<' . Page::statusTrash . ')', $this, 'hookPageSaved', [ 'priority' => 200 ] );

This adds the hook only if the saved page is not already in the trash, and executes the hook as late as possible.

Now, I would like to add the hook only, if a certain checkbox on the page is checked. I tried the following:

// this does not work:
$this->addHookAfter( 'Pages::saved(some_checkbox_field=1, status<' . Page::statusTrash . ')', $this, 'hookPageSaved', [ 'priority' => 200 ] );

But this did not work ...
Update: This didn't work, because I had a non-standard checkbox via the module CheckboxReversed, so I should have used the selector some_checkbox_field=0.
With a default checkbox, the code examples work perfectly.

Link to comment
Share on other sites

@nurkka It works for me at least:

$wire->addHookAfter('Pages::saved(toggle_checked=1, status<trash)', function($e) {
    $page = $e->arguments(0);
    $e->warning("Saved page $page->path"); 
}, [ 'priority' => 200 ]);

Check if your checkbox field is configured to have a non-default value?

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

  • nurkka changed the title to [SOLVED] Advanced Hooks with conditions

Additional info:
I just noticed that these two variants don't work the same:

// this works:
$this->addHookAfter( 'Pages::saved(status<' . Page::statusTrash . ')', $this, 'hookPageSaved', [ 'priority' => 200 ] );
  
// this does not work:
$this->addHookAfter( 'Pages::saved(status<trash)', $this, 'hookPageSaved', [ 'priority' => 200 ] );
  

In my case, I a have a module which writes the pages as JSON files to disk, after they are saved. As a page will be saved, when moving it to the trash, the hook will be called either. The selector in the first variant prevents this, while the selector in the second variant doesn't.

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