Jump to content

How to manipulate buildEditForm in ProcessField?


Oliver
 Share

Recommended Posts

Is there any way to manipulate the form created by ProcessField's not hookable method buildEditForm? It's used by the hookable executeEdit method. But by adding a hook after executeEdit, I just can access the rendered HTML code executeEdit returns. Changing the markup is definitely not the way to go here. Adding a hook before is also useless, as the buildEditForm is called after the hook would have been executed.

The only idea I got is to set the form property through a hook before, as it would be used by executeEdit if it already is set.

public function ___executeEdit() {
 if(is_null($this->form)) $this->buildEditForm();
 ...

But as I can't access the protected buildEditForm method from outside the class, it seems there is just the way to copy buildEditForm completely, change it and use it to set ProcessField's form property before executeEdit instead of just accessing the form to perform minor changes or additions. Anyone a better idea? Or would it make sense to make the form generation more accessible for hooks?

Link to comment
Share on other sites

If you say properties of the form, if you just want to change/add attributes of the InputfieldForm rendered you could do it like this with an auto load module:

<?php

class Helloworld extends WireData implements Module {
 public static function getModuleInfo() {
return array(
  'title' => 'Hello World',
  'version' => 100,
  'summary' => 'An example module.',
  'singular' => true,
  'autoload' => true,
);
 }
 public function init() {
$this->addHookBefore('InputfieldForm::render', $this, 'formHook');
 }
 public function formHook(HookEvent $event) {
// restrict to edit field form only
if( $this->process == 'ProcessField' && strpos($_SERVER['REQUEST_URI'], 'field/edit?') !== false ){
  $form = $event->object;
  $form->attr('class', 'MyCSSClass');
}
 }
}
Link to comment
Share on other sites

That's a good idea by Soma. But I don't see any reason why these functions shouldn't be hookable in ProcessField, so I've just made them hookable. The following are now hookable:

buildEditForm

buildEditFormBasics

buildEditFormCustom

buildEditFormAdvanced

buildEditFormDelete

buildEditFormInfo

I've also added a function to ProcessField:

getField() -- returns the current field being edited (as set by buildEditForm)

So if you make a hook into one of the above, you'd retrieve the field like this:

public function yourHookFunction(HookEvent $event) {
 $process = $event->object;
 $field = $process->getField(); 
 // ...
}
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...