Juergen Posted September 26, 2016 Share Posted September 26, 2016 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 More sharing options...
tpr Posted September 26, 2016 Share Posted September 26, 2016 You can also try this (untested with this hook): $page = $event->object->getPage(); Link to comment Share on other sites More sharing options...
Juergen Posted September 26, 2016 Author Share Posted September 26, 2016 Unfortunately this doesnt work. Method InputfieldPageTable::getPage does not exist or is not callable in this context 21 secs Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 26, 2016 Share Posted September 26, 2016 $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. 2 Link to comment Share on other sites More sharing options...
Juergen Posted September 26, 2016 Author Share Posted September 26, 2016 Ok thanks! I will hide it with jquery. Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 26, 2016 Share Posted September 26, 2016 You could try to add your current hook from a hook to ProcessPageEdit. This way it'll only get added for the page you want the button to be hidden. Link to comment Share on other sites More sharing options...
Robin S Posted September 26, 2016 Share Posted September 26, 2016 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... } 2 Link to comment Share on other sites More sharing options...
Juergen Posted September 26, 2016 Author Share Posted September 26, 2016 @Robin S, many thanks. That works as expected! Great!!!!!!!! Link to comment Share on other sites More sharing options...
adrian Posted September 26, 2016 Share Posted September 26, 2016 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 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now