Jump to content

Error when using sanitizer in hook


Hans0L0
 Share

Recommended Posts

Hey guys,

I'm trying to generate a page name depending on another input field (checkbox) as a combination of title and an extra text field. So far, I've found this topic with an explanation how to hook after page save:

 

So what I was trying is the following:

/* ready.php */
$pages->addHookAfter('saveReady', function(HookEvent $event) {
    $page = $event->arguments(0);
    if($page->template->name !== 'my_template_name') return;
    if($page->my_checkbox_name) {
        $page->name = $sanitizer->pageName($page->title, Sanitizer::translate);
    } else {
		$page->name = $sanitizer->pageName("$page->my_extra_field $page->title", Sanitizer::translate);
	}
});

Unfortunately I get this compilation error message:

Error: Uncaught Error: Call to a member function pageName() on null

Trying to use wire()->$sanitizer leads to another processwire error:

Method ProcessWire::pageName does not exist or is not callable in this context

Even this simple test script won't work for me:

$pages->addHookAfter('saveReady', function(HookEvent $event) {
    $page = $event->arguments(0);
    if($page->template->name !== 'my_template_name') return;
    $page->name = wire()->$sanitizer->pageName("test");
});

I don't see any further reason why/how this would work or not... ?

Link to comment
Share on other sites

10 minutes ago, Hans0L0 said:

Hi flydev, I've tried that one too, but it's the same result as with 'wire()'..

Correction: I've tried that one too, but it's the same result as with 'wire()'..  that got it working!

My mistake was writing '->$sanitizer' instead of '->sanitizer', also in the version with 'wire()'

Thanks! ?

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