1

I am building a module that will have a bunch of checkboxes to enable the addition of metatags like HandheldFriendly, MobileOptimized, etc.

I want to have the first checkbox only displayed. Upon checking that item, other options should appear. (if itemA == checked, then show itemB) I actually have that working. The problem is that if you check itemA and then check itemB and save, but then uncheck itemA, when you return to the configuration page itemB will still have a value.

Here is a simple timeline showing the issue

Snapshot #1: itemA: unchecked itemB: unchecked (invisible)

Snapshot #2: itemA: checked itemB: unchecked (visible)

Snapshot #3: itemA: checked itemB: checked (visible)

Snapshot #4: itemA: unchecked itemB: checked (invisible) // PROBLEM

Between each snapshot, the form was saved. How can I automatically hide AND uncheck the value for itemB when clicking itemA so that when I re-enable itemA, itemB is still unchecked?

If I need to provide further clarification, I can.

module code for poetic_toolkit.admin.inc file

1 Answer 1

0

Add a secondary submit handler to run before the main one:

array_unshift($form['#submit'], 'poetic_toolkit_admin_submit');

Then in that:

function poetic_toolkit_admin_submit($form, &$form_state) {

    // check if field A is checked
    if(!$form_state['values']['poetic_toolkit_mobile_viewport_setting']) {
        // if it is not then set field B value to 0
        $form_state['values']['poetic_toolkit_enable_webapp_setting'] = 0;
    }

}

Then after this the main submit handler will fire and save our updated values.

This will only update the values once the form is saved.

Additionally, you don't want the unchecked condition on the depending item poetic_toolkit_enable_webapp_setting, because then it becomes checked whenever the dependent field poetic_toolkit_mobile_viewport_setting is checked (double negative). You also want to make sure you have the #default_value on the second item set, so that the user see's what is currently set (which will always be unchecked when the first is unchecked but could still be unchecked if the first is checked).

If you want B to become unchecked as soon as A is unchecked then you will need a jQuery solution.

7
  • This is excellent information. I'm a little unsure how to implement the array_unshift. Should it go in my module_admin_settings function, just before the return statement?
    – mattcoker
    Apr 2, 2014 at 17:30
  • Put it on the line before return system_settings_form($form);
    – Felix Eve
    Apr 3, 2014 at 7:56
  • I have the line in there, but I don't see any change in behavior even after clearing all caches. When A and B are both checked, and I uncheck A, B is hidden. Upon saving and rechecking A, the value for B is still checked. Updated code
    – mattcoker
    Apr 3, 2014 at 18:09
  • You need to put some debugging code in to check why it is not working. Inspect the form array to make sure the additional submit handler is in the correct place, and then inspect the $form_state in the submit handler to make sure the values have been updated correctly. I've updated the code with my suggested dpm calls here
    – Felix Eve
    Apr 4, 2014 at 7:57
  • I made the change that you listed, but the information is never printed out where I can see it. Not when the form reloads after saving, and not anywhere else on the site. Devel is turned on. Any other way I can check that value?
    – mattcoker
    Apr 4, 2014 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.