Peter Troeger Posted December 12, 2018 Share Posted December 12, 2018 (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 December 13, 2018 by Peter Troeger * Solved! :) Link to comment Share on other sites More sharing options...
flydev Posted December 12, 2018 Share Posted December 12, 2018 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 1 Link to comment Share on other sites More sharing options...
Peter Troeger Posted December 13, 2018 Author Share Posted December 13, 2018 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 More sharing options...
Peter Troeger Posted December 13, 2018 Author Share Posted December 13, 2018 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 More sharing options...
flydev Posted December 13, 2018 Share Posted December 13, 2018 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')); }); 1 Link to comment Share on other sites More sharing options...
Peter Troeger Posted December 13, 2018 Author Share Posted December 13, 2018 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? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now