Jump to content

Replace render method of InputfieldCheckboxes - class context


gebeer
 Share

Recommended Posts

Hello,

I need to add some markup to checkbox inputs of page fields in the admin forms for which I created an autoload module with

$this->addHookBefore('InputfieldCheckboxes::render', $this, 'renderCheckboxes');

The renderCheckboxes method aims to replicate, extend and then replace the original render method of InputfieldCheckboxes.

I'm having trouble finding the right class context from within my renderCheckboxes method.

In the original render method there is code like $this->checkDefaultValue() where $this represents the context of ProcessWire\InputfieldCheckboxes and checkDefaultValue() is a method defined in the parent class InputfieldSelect.

In my renderCheckboxes method, I need to be able to access the same class context in order to replicate the original render method.
 

public function renderCheckboxes($event) {

  $inputfield = $event->object;
  if($inputfield->name != 'certifications') return;
  var_dump($inputfield);
  $inputfield->checkDefaultValue();

}

The var_dump tells me that $inputfield is an object instance of ProcessWire\InputfieldCheckboxes, just like $this in the original render method.
But when I try to do $inputfield->checkDefaultValue(), I get an error: Method InputfieldCheckboxes::checkDefaultValue does not exist or is not callable in this context

This tells me that the context for the $event->object is not the same as for $this in the original render method.

Obviously I don't know enough about OOP and PW module development to understand what is going on. But I would like to be able to replace that render method with my own.
Any pointers on how to achieve this would be very much appreciated.

Link to comment
Share on other sites

$event->object and $this refer to the same object. The difference is that hooks are not "inside" the class, so you can only access public methods/properties, while the core class can access all of it's own methods/properties. 

http://php.net/manual/en/language.oop5.visibility.php

http://php.net/manual/en/language.oop5.inheritance.php

  • Like 2
Link to comment
Share on other sites

Thank you for the explanation.

Is it possible then to instantiate the InputfieldCheckboxes class in the original context from within my module so that I have access to all methods? Or any other way to "step inside" the original class context?

Link to comment
Share on other sites

Yes and no. Not having access to those is what those visibility rules are for. If you really need to access blocked class contents you can use the Reflection class in php to inspect further than what's public, but this is mostly meant for code inspection. 

Link to comment
Share on other sites

you could also copy the module into you site/modules/ directory, edit to your needs and then in processwire module settings of this inputfield select the one from your site/modules, just refresh module cash and it'll note about two possible files for this module anyway ;-)

more about this in the blog https://processwire.com/blog/posts/processwire-core-updates-2.5.14/#multiple-copies-of-the-same-module 

  • 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

×
×
  • Create New...