Jump to content

[SOLVED] add Hook to set default Dropdown-Select in form


Peter Troeger
 Share

Recommended Posts

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! :)
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

  • Recently Browsing   0 members

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