thetuningspoon Posted March 24, 2015 Share Posted March 24, 2015 I'm trying to add a configuration option to InputfieldPage using a hook. I am able to create the new field (a checkbox) by hooking into InputfieldPage::getConfigInputfields so it shows up on the field's settings page, but when I try saving the field with the checkbox checked off, the setting does not save. (it remains unchecked) public function hookAfterGetConfigInputFields(HookEvent $event) { if($event->hasFieldtype !== false) { $field = $this->modules->get('InputfieldCheckbox'); $field->attr('name', 'editable'); $field->attr('value', 1); $field->label = $event->_('Use page edit link?'); $field->description = $event->_('If checked, pages selected with this field may be edited in a modal window.'); if($event->editable) $field->attr('checked', 'checked'); $event->return->append($field); } } Link to comment Share on other sites More sharing options...
adrian Posted March 24, 2015 Share Posted March 24, 2015 Have you checked the value of $event->editable ? I am not sure in your situation, but you might try this. Of course my ternary approach is not relevant, but you might need to get the value of editable like this: $field->attr('checked', $this->fields->get($event->object->name)->editable ? 'checked' : '' ); 1 Link to comment Share on other sites More sharing options...
thetuningspoon Posted March 24, 2015 Author Share Posted March 24, 2015 Have you checked the value of $event->editable ? I am not sure in your situation, but you might try this. Of course my ternary approach is not relevant, but you might need to get the value of editable like this: $field->attr('checked', $this->fields->get($event->object->name)->editable ? 'checked' : '' ); Thanks Adrian, that did it! After posting I checked and discovered that the value of editable was being saved in the database, but just wasn't reflected in the GUI because $event->editable was returning nothing. I am a little confused how the $event variable works. What does "object" represent here? Link to comment Share on other sites More sharing options...
thetuningspoon Posted March 24, 2015 Author Share Posted March 24, 2015 Adrian, I just played around with this some more and discovered that the api call was unnecessary. The only thing I was missing was the "object" property. So $event->object->editable returns what I'm looking for. Here is my modified hook: public function hookAfterGetConfigInputFields(HookEvent $event) { if($event->object->hasFieldtype !== false) { $field = $this->modules->get('InputfieldCheckbox'); $field->attr('name', 'editable'); $field->attr('value', 1); $field->label = $this->_('Use page edit link?'); $field->description = $this->_('If checked, pages selected with this field may be edited in a modal window.'); if($event->object->editable) $field->attr('checked', 'checked'); $event->return->append($field); } } 1 Link to comment Share on other sites More sharing options...
adrian Posted March 24, 2015 Share Posted March 24, 2015 Nice - that does make more sense - I grabbed that line from an old module of mine - not sure why I thought that extended version was needed. EDIT: Actually, in my scenario, what I had is needed Link to comment Share on other sites More sharing options...
Soma Posted March 24, 2015 Share Posted March 24, 2015 $event->object refers to the module / class you hook into. @Adrian config fields are not fields. Only Inputfields for a module config. Link to comment Share on other sites More sharing options...
adrian Posted March 24, 2015 Share Posted March 24, 2015 @Adrian config fields are not fields. Only Inputfields for a module config. Understood, although I am not totally sure on the difference between what @thetuningspoon (the developer previously known as @everfreecreative) was doing and what I needed. I needed the following: $f->attr('checked', $this->fields->get($event->object->name)->allow_overwrite ? 'checked' : '' ); here: https://github.com/adrianbj/TableCsvImportExport/blob/master/TableCsvImportExport.module#L55 If I switch to simply $event->object->allow_overwrite, it doesn't work. What I have makes sense to me because it is the getting the allow_overwrite value for the field name that I am getting with $event->object->name, although I would have thought it would also work with just $event->object->allow_overwrite. What am I missing? 2 Link to comment Share on other sites More sharing options...
Soma Posted March 24, 2015 Share Posted March 24, 2015 Ah sorry, I read that too quick (on mobile). Now at the laptop, I see that you're getting the config value from the field. I did that here too https://gist.github.com/somatonic/4252958 Since the property isn't set in construct, it's not automatically set to the field from the config data. 1 Link to comment Share on other sites More sharing options...
Soma Posted March 24, 2015 Share Posted March 24, 2015 What I just thought about is: What if you add the property to the Inputfield using a hook. And it works you can do $this->addHookProperty("InputfieldText::overwrite", $this, "addProperty"); Then public function addProperty(HookEvent $event) { $event->return = 0; } Then this works $this->addHookAfter("InputfieldText::getConfigInputfields", $this, "hookConfig"); public function hookConfig(HookEvent $event){ $fields = $event->return; $field = $event->object; $modules = wire("modules"); $f = $modules->InputfieldCheckbox; $f->attr("name", "overwrite"); $f->attr("checked", $field->overwrite ? "checked" : ""); $fields->add($f); } 2 Link to comment Share on other sites More sharing options...
thetuningspoon Posted March 24, 2015 Author Share Posted March 24, 2015 Soma, I wasn't aware of the addHookProperty() function. Not sure how I missed that in the documentation before. I'm surprised that my code is working without explicitly adding the property... Link to comment Share on other sites More sharing options...
Soma Posted March 24, 2015 Share Posted March 24, 2015 Soma, I wasn't aware of the addHookProperty() function. Not sure how I missed that in the documentation before. I'm surprised that my code is working without explicitly adding the property... I'm not sure why at all. it shouldn't work. But then I only see partial code. Well, then you haven't read the HelloWorld.module yet!?. The documentation along with examples is right where you start learning it. Link to comment Share on other sites More sharing options...
adrian Posted March 24, 2015 Share Posted March 24, 2015 soma - I have certainly used addHookProperty before, but it seems like an unnecessary use for it in this case, right? Don't you think it is cleaner and would have less overhead to simply get the property via the call to the field like in my example and your TextareaCounter? I assume your example for adding the property in this case is just an example of how it could be done, rather than a recommendation? I don't understand why @thetuningspoon's code is working as is either Link to comment Share on other sites More sharing options...
Soma Posted March 25, 2015 Share Posted March 25, 2015 In my opinion it doesnt matter. It was just an alternative and don't think it add more overhead than loading the field from db or adding it via hook. Link to comment Share on other sites More sharing options...
Macrura Posted April 12, 2016 Share Posted April 12, 2016 I'm having trouble adding config fields to an inputfieldText, actually the config fields are added an function, and they even save to the database, but the $event->object does not read the data back into the object at this init() for some reason – this is based on the AdminPageFieldEditLinks module, which i tried to copy virtually exactly, but my data array never contains the extra values from the data column in the database... $this->addHookAfter("InputfieldText::getConfigInputfields", function($event) { $that = $event->object; if($that->hasFieldtype !== false) { $field = $this->modules->get('InputfieldCheckbox'); $field->attr('name', 'selectizeThis'); $field->attr('value', 1); $field->label = $this->_('Enable Selectized input for this field?'); $field->description = $this->_('If checked, you may setup selectable options for this field. (Options may be selected and then edited).'); if($that->selectizeThis) $field->attr('checked', 'checked'); else $field->collapsed = Inputfield::collapsedYes; $event->return->append($field); // list of selectable text strings $field = $this->modules->get('InputfieldTextarea'); $field->attr('name', 'userStringsList'); $field->label = $this->_('List of text strings to show as options.'); $description = $this->_('List each string, 1 per line.'); $field->description = $description; $notes = 'Example: Default News Title Format'; $field->notes = $notes; $field->value = trim($that->userStringsList); $field->showIf = 'selectizeThis>0'; $event->return->append($field); } }); Link to comment Share on other sites More sharing options...
thetuningspoon Posted April 12, 2016 Author Share Posted April 12, 2016 I wonder if the difference has something to do with working with an Inputfield vs. a Fieldtype. In what context are you wanting the additional config options to be shown? 1 Link to comment Share on other sites More sharing options...
Macrura Posted April 12, 2016 Share Posted April 12, 2016 @thetuningspoon - thanks for reply... holy smokes, figured it out, and soma's post was staring at me the whole time... you need to add hook properties for this type of inputfield... so adding the below to the init() $this->addHookProperty("InputfieldText::selectizeThis", $this, "addProperty"); $this->addHookProperty("InputfieldText::userStringsList", $this, "addProperty"); and adding this method to the module: public function addProperty(HookEvent $event) { $event->return = 0; } makes it work... so for future reference...this is how to add custom attributes/settings to a text field... https://processwire.com/talk/topic/9462-adding-a-configuration-setting-to-an-existing-module-with-a-hook/?p=91149 4 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