Jump to content

How to define the page variable in hooks


Juergen
 Share

Recommended Posts

Hello @ all,

I am struggeling with a problem to run a hook under a certain condition.

I have used a code from Soma which removes the add button from a pagetable field.

    public function init() {
        $this->buttonHook = null;
        $this->addHookBefore("InputfieldPageTable::render", $this, "renderPageTable");
        $this->addHookAfter("InputfieldPageTable::render", $this, "renderPageTableAfter");
    }

    public function renderPageTable(HookEvent $event){
         // get the table field
        $table = $event->object;

        // make sure this is our field
        if($table->name !== "mypagetable") return;

        // if there's 2+ rows add another hook to remove returned markup string
        // rendered by InputfieldButton::render
        if(count($table->attr("value")) > 1) {
            $this->buttonHook = $this->addHookAfter("InputfieldButton::render", null, function(HookEvent $event){
                // overwrite/remove button markup
                $event->return = '';
            });
        }
    }

This works so far, but I need to run the code only under a certain condition. In my case:

if(($page->template == "events") AND ($page->eventkindchooser == "1"))

The problem in this case is how to define the page variable ($page) because the event object is used as

$table = $event->object;

I have tried to define the page variable as

$page = $event->arguments[0];

like in other cases but this does not work.

How can I define the page variable in this scenario so I am able to use the if condition?

Best regards

Link to comment
Share on other sites

$event->object is the object the hooked function lives on, in this case: A InputfieldPageTable object.

$event->arguments does also have nothing, which garantees you to return a page object. This does hold the parameters, which are passed into the hooked function. Can be a page, but can be all sorts of other things. In your case there aren't any parameters to the render function. 

@tpr Mentioned another incorrect option, because getPage() is just a convenience function to retrieve the page if you're hooking ProcessPageEdit. Other hooked objects most often do not implement such a function. 

In the case of InputfieldPageTable you might be out of luck, because it's best practice, that inputfields do not need to know anything about the page they live on. They just need to know the value to work with. Only the fieldtype does need to be aware of the page itself.

  • Like 2
Link to comment
Share on other sites

37 minutes ago, LostKobrakai said:

In the case of InputfieldPageTable you might be out of luck

Never say die, where there's a will, there's a way! :)

public function renderPageTable(HookEvent $event){
	$page_id = $this->input->get('id');
	$page = $this->pages->get($page_id);
	// use $page...
}

 

  • Like 2
Link to comment
Share on other sites

Using the get ID can be problematic / dangerous - at least make it safe by sanitizing it with:

$page_id = (int) $this->input->get('id');

You might want to take a look at how it's done in the field itself:

https://github.com/processwire/processwire/blob/master/wire/modules/Inputfield/InputfieldPageTable/InputfieldPageTable.module#L137-L138

Note the sanitized get id check with a fallback to getPage() of wire->process

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