Jump to content

Hooking buildFormContent() - set field parent_id to InputfieldPageAutocomplete


martind
 Share

Recommended Posts

It should be possible. You can use the methods of InputfieldWrapper on the return value of buildFormContent, retrieve the field by name, create the new Inputfield and insert that in the place of the original one with insertBefore()/insertAfter() and remove().

//...
$wrap = $event->return;
$origFld = $wrap->get('parent_id');

$newFld = wire('modules')->get('InputfieldPageAutocomplete');
$newFld->attr('name', 'parent_id');
$newFld->attr('value', $origFld->attr('value'));
// do any other instantiation work, like setting/copying title, label etc.

$origFld->parent()->insertAfter($newFld, $origFld)->remove($origFld);
// done

 

  • Like 3
Link to comment
Share on other sites

thank you. the problem i see is, that

$origFld = $wrap->get('parent_id');

is NULL. parent_id doesn't seem to be in $event->return
maybe this has to do with my module the hook lives in?

    public function ready() {
        $this->addHookAfter('ProcessPageEdit::buildFormContent', $this, 'hookAfter_ProcessPageEdit_buildFormContent');

		....

    public function hookAfter_ProcessPageEdit_buildFormContent($event) {
        $p = $event->object->getPage();
        $form = $event->return;

		....

		$origFld = $form->get('parent_id'); // and here the NULL

 

Link to comment
Share on other sites

7 hours ago, martind said:

parent_id doesn't seem to be in $event->return

Try hooking after ProcessPageEdit::buildForm. ProcessPageEdit::buildFormContent only contains the page template fields and not the fields from the Children, Settings, etc tabs.

Another hurdle is that the value of InputfieldPageAutocomplete is always an array, and ProcessPageEdit expects the value of the parent_id field to be an integer.

This seems to be working:

$wire->addHookAfter('ProcessPageEdit::buildForm', function(HookEvent $event) {
	/* @var InputfieldWrapper $form */
	$form = $event->return;
	$orig_pi_field = $form->getChildByName('parent_id');
	$attrs = [
		'collapsed',
		'required',
		'label',
		'icon',
		'id',
		'name',
		'value',
	];
	$new_pi_field = $event->wire('modules')->get('InputfieldPageAutocomplete');
	$form->insertAfter($new_pi_field, $orig_pi_field);
	$form->remove($orig_pi_field);
	foreach($attrs as $attr) {
		$new_pi_field->$attr = $orig_pi_field->$attr;
	}
	$new_pi_field->maxSelectedItems = 1;
});

$wire->addHookAfter('ProcessPageEdit::processInput', function(HookEvent $event) {
	/* @var InputfieldWrapper $form */
	$form = $event->arguments(0);
	if($form->id !== 'ProcessPageEditSettings') return;
	$pi_field = $form->getChildByName('parent_id');
	if(!$pi_field || !$pi_field->isChanged()) return;
	$parent_id = $pi_field->value;
	$parent_id = (int) reset($parent_id);
	/* @var Page $page */
	$page = $event->object->getPage();
	$page->parent_id = $parent_id;
});

 

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...

Hey @martind, just a heads-up that I've moved this topic to the General Support area of the forum. Modules/Plugins area is only intended for module-specific dedicated support threads (for third party modules), and this question is about a core feature ?

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...