Jump to content

Recommended Posts

Posted

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.

Posted

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?

Posted

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. 

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
×
×
  • Create New...