Jump to content

[solved] How to do additional logic with input data after module save


Can
 Share

Recommended Posts

Hey guys, I'm building a module from https://processwire.com/talk/topic/13471-better-ckeditor-image-insertion-at-least-for-me/

it's declared as "class HappyImage extends WireData implements Module, ConfigurableModule"

everything works so far, but I don't know how to process input send with module config submits

The module is showing all available CKEditor fields and if my plugin is enabled for it or not with a checkbox, this checkbox gets properly submitted (I inspected with chrome dev tools)

but I don't know where to I receive those inputs? I put some test logic just to see what it spits out but no matter if I place in module's construct, init, ready or even in getModuleConfigInputfields, no input at all.. :/

Link to comment
Share on other sites

I am not sure you can do that (without hooking into something). Module configs are are processed internally by ProcessWire. It will correctly save radio values, checkboxes, etc. The module config form has the ID: ModuleEditForm. Is there any reason you want to also process them? If, on the other hand, you need access to the saved configs, you get those using:

$happyImageConfigs = $modules->getModuleConfigData('HappyImage');// assoc. array

 

Link to comment
Share on other sites

Thanks kongondo, that's what I'm afraid of, maybe I can rearrange the markup, but right now those checkboxes are not really part of the module config, they're just sitting in theire

they're automatically checked if the inputfield has the plugin already enabled, if not you could just mark all you want to enable and save the module
then I need to know for which fields you want to enable the ckeditor plugin to do so ;-)

Link to comment
Share on other sites

You should be able to do your 'automatic checks' in getModuleConfigInputfields(). If the checkboxes are on that screen then I see no harm in letting them be part of the config. See how we do it in Blog. If Blog is installed, we show a different form, if not, we show the pre-install form. Search the forums for that method for simpler examples. It's a static method, mind you, so you will be 'wiring' a lot :)

Link to comment
Share on other sites

I'm diving thru the module code (and I had dinner^^) and I like it, but sorry I still don't get how it'll help my need?

only idea came to mind would be to store it in module config and e.g. in ready function I check them and if I need to change field settings or not..
I hoped I could do all this on config save..

EDIT: thing is I wanted to line all available fields nicely in a MarkupAdminDataTable and that seems to interfere with saving to module settings..

Link to comment
Share on other sites

I think you want to have a method in your module that hooks Modules::saveModuleConfigData. At the top of that method you check if it is your module that was saved and return if not.

$module_name = $event->arguments('className');
if(is_object($module_name)) $module_name = $module_name->className();
if($module_name !== $this->className()) return;

Then do whatever based on the saved config data.

$cfg = $event->arguments('configData');

I think you can also get submitted config values from $input->post within your getModuleConfigInputfields() method but that seems less desirable.

 

Link to comment
Share on other sites

that's a great idea, had the thought about some hook but missed to ask..

unfortunately I can't get it working

the checkboxes are not part of the module config fields, they're just sitting within a markup field in a markupAdminDataTable

and as mentioned before, I can see their data being sent with the form in dev tools but am not able to catch it, always seems to late

so configData won't work as it's not part of it and $input->post is always empty when I catch it :/

Uh and got the hook only running from construct function of module..init and ready wouldn't do anything

Link to comment
Share on other sites

21 minutes ago, Can said:

$input->post is always empty when I catch it

Just tested this and it works for me - I can also see checked checkboxes contained inside InputfieldMarkup (but not unchecked ones).

public function getModuleConfigInputfields() {
        
    $post = $this->input->post;
    bd($post, 'post');
    
    $inputfields = new InputfieldWrapper();

    $f_name = 'addnew_text';
    $f = $this->modules->get('InputfieldText');
    $f->name = $f_name;
    $f->label = 'A text field';
    $f->value = $this->$f_name ?: 'Add New';
    $inputfields->add($f);

    $f = $this->modules->get('InputfieldMarkup');
    $f->name = 'my_markup';
    $f->label = 'My markup';
    $f->value = "
    <label>
    <input type='checkbox' name='my_checkbox'>
    My checkbox
    </label>
    ";
    $inputfields->add($f);

    return $inputfields;
}

 

22 hours ago, Can said:

Thanks kongondo, that's what I'm afraid of, maybe I can rearrange the markup, but right now those checkboxes are not really part of the module config, they're just sitting in theire

they're automatically checked if the inputfield has the plugin already enabled, if not you could just mark all you want to enable and save the module
then I need to know for which fields you want to enable the ckeditor plugin to do so ;-)

25 minutes ago, Can said:

the checkboxes are not part of the module config fields, they're just sitting within a markup field in a markupAdminDataTable

This approach seems strange to me. Normally your module config is what defines your settings - so you check the module config values in your module methods and do things accordingly. You seem to be doing it back-to-front, like you are looking for settings defined elsewhere and then putting fields into your module config.

Wouldn't it be better to just use actual checkbox fields in your module config to define the settings?

  • Like 1
Link to comment
Share on other sites

oookay, (of cause) my fault^^
should've used tracydebugger or just place the propper code..it just works now in getModuleConfigInputfields()

thing is, it's actually a setting of InputfieldCKEditor, my module is symlinking a CKEditor plugin and this needs to be enabled, and I liked the ability to do all this from the same page, so installing this module would become easier. makes sense?

Link to comment
Share on other sites

1 minute ago, Can said:

thing is, it's actually a setting of InputfieldCKEditor, my module is symlinking a CKEditor plugin and this needs to be enabled, and I liked the ability to do all this from the same page, so installing this module would become easier. makes sense?

I just private messaged you with some stuff, but for anyone else that is following the discussion, check out how Hanna Code Helper handles the loading of a CKEditor plugin:

https://github.com/teppokoivula/HannaCodeHelper/blob/c26b3b7eeafa17dd8500dc1b89cbb88218ceca9c/HannaCodeHelper.module#L55-L97

  • Like 2
Link to comment
Share on other sites

Thanks for sharing @Robin S so, I would then save the checkbox to my modules config, and inject some similar js into page edit for enabled CKEditor fields instead of my current approach, symlinking the plugin into site/InputfieldCKEditor/plugins and then messing with field settings..

I like! :) 

Link to comment
Share on other sites

  • 2 weeks later...

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