Jump to content

Recommended Posts

Posted (edited)

Hi ?

I'm trying my hands on hooks and was hoping to figure out how to set the default value of a page-select-dropdown of a form, created with formBuilder, via a hook.

I want it to be dynamic, so I don't want to set it fixed in the formBuilder Menu.

I tried something like this, but I don't know what to set as method and whether I should return a page or an id:

$wire->addHookAfter('InputfieldPage::processInput', function($event) {
  if($event->object->name == 'job_waehlen') {
    $event->return = '1036';
  }
});

Any hints and help are greatly appreciated ?
Thanks!

- Peter

Edited by Peter Troeger
* Solved! :)
Posted

Hi,

you have to assign a page.

$wire->addHookAfter('InputfieldPage::processInput', function($event) {
    if($event->object->name == 'job_waehlen') {
        bd($event->return->value); // return 1019 (my test id page)
        $event->return->value = $this->pages->get(1); // will set you parent tree
        bd($event->return->value); // return 1 (root page)
    }
});

 

PS:  bd() call come from TracyDebugger

 

 

  • Thanks 1
Posted

Hi flydev,

thanks for your Feedback!

It's not working for me. In my dropdown I have two pages. One has as a value '1033', the other '1036'.
The Dropdowns name is 'job_waehlen'.

Even when I put $event->return->value = $this->pages->get(1033); The Dropdown doesn't preselect anything.<

Is my method ('InputfieldPage::processInput') possibly the wrong one for a dropdown input field?

- Peter

Posted

I tried something different. And while it doen't work either, at least something happens ?

$wire->addHookBefore('InputfieldPage::render', function($event) {
  if($event->object->name == 'job_waehlen') {
	$event->object->set('defaultValue', '1033');
  }
});

I changed 'addHookAfter' to 'addHookBefore' and the method to 'InputfieldPage::render'. Now the Hook actually seems to trigger and so something.
So then I found out that there's a value called 'defaultValue', which I was able to change, but it doesn't seem to have an effect on my Dropdown. It still doesn't preselect anything. ?

Posted
1 hour ago, Peter Troeger said:

The Dropdown doesn't preselect anything

I understand now what you are trying to achieve.

try that :

$mypage= $pages->get(1036);

$wire->addHookBefore('InputfieldPage::render', function($event) use(&$mypage) {
    if($event->object->name !== 'job_waehlen') return;
    $inputfield = $event->object;
    $selectfield = $inputfield->getInputField();
    $selectfield->addOption($mypage->id, $mypage->title, array('selected' => 'selected'));
});

 

  • Thanks 1
Posted

Awesome! That was it ? Thank you very much! ?

So I'm basically selecting the field, and then adding an Option to it. If it exists it just overwrites the existing option and if it doesn't, it just adds it to the dropdown. Right?

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...