Jump to content

Dependent select in module config


Robin S
 Share

Recommended Posts

Again I'm on the hunt for example modules to learn from.

This time I'm wondering if anyone has released a module that uses dependent selects in the config - in other words, load select options for select inputfield A depending on the value of select inputfield B. Currently the module I'm working on requires a save after changing inputfield A, but it would be much cooler if I could update the select options for inputfield B via AJAX when the value of A changes.

Link to comment
Share on other sites

You need to call your modules config url via AJAX,

check in the init or ready if it is an AJAX request, do the needed settings, (modify / save config?)

and return back the resulting list B, or what ever you like / need to do. :)

 

It is completly the same as you would do with any other HTML-page. There is nothing special for modules here. Only difference maybe the places where you check for AJAX requests and posted keys, and that your returns maybe organized in dedicated methods in your module. (Like it is done automatically with urlSegments in Processmodules ___executeMySpecialRequest())

public function init() { // or ready()
    $input = $this->input;
    if($input->post->mySpecialRequest) {
        $this->mySpecialRequestMethod();
    } elseif($input->post->anotherSpecialRequest) {
        $this->anotherSpecialRequest();
    } else {
        // all stuff for a manual and fully displaying of the config page
        ...
    }
}

 

Link to comment
Share on other sites

Thanks @horst.

I have my dependent selects working acceptably well now. But there are a couple of things that could perhaps be improved if anyone has suggestions...

1. How can I add options to a select inputfield from outside getModuleConfigInputfields()? Any options used in the select must be added to field $f with $f->addOption() or else they don't pass validation and are not successfully saved on form submit. I need to add these options in my AJAX method according to what has been selected in the first select. I thought I would be able to do this...

$fields = $this->modules->getModuleConfigInputfields($this->className);
$f = $fields->get('my_select_field');
$f->addOption('my_value', 'my_label);

But options added here do not persist, i.e. they are not included the the array of allowed options at the time the form is submitted and processed.

Options added within the getModuleConfigInputfields() method in the module work normally. Currently my workaround is to set all possible options within getModuleConfigInputfields() and then filter out the 'wrong' ones with jQuery, but it would be nice to set just the 'right' options in my AJAX method.

 

2. The second one relates to what you say here...

14 hours ago, horst said:

} else { // all stuff for a manual and fully displaying of the config page ... }

When accessing the module config page with an AJAX request we don't want all the normal form markup returned. I'm not sure what you mean about setting this manually with an if/else condition in init(). As I understand it, the form markup comes automatically from ProcessModule and doesn't depend on anything done manually in init().

The only modules that I know of that use AJAX in the config are the ones @adrian referred me to:
http://modules.processwire.com/modules/process-email-to-page/
http://modules.processwire.com/modules/process-custom-upload-names/

These two avoid the normal form markup by doing exit() at the end of the AJAX method, but I think this is a bit ugly (and Ryan has said that it's best if PW can control its shutdown). So I am using a replace hook on ProcessModule::executeEdit. I'm interested if there are different or better ways to handle an AJAX request to the config page.

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