Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hello,
I'm currently using the ctools form wizard. I'd like to be able to programmatically modify the order of the form steps (or remove steps) via the submit handlers. The tutorial suggests that this is possible:
Note that submit callbacks can override the order so that branching logic can be used.
However, modifying the $form_state['form_info'] values in a child-form submit function seems to have no effect on the order or presence of steps.
Is there another place that these values should be modified?
Thanks!
Comment | File | Size | Author |
---|---|---|---|
#8 | 7.x-ctools-multistep-update-steps-in-submit-1732904-8.patch | 583 bytes | IRuslan |
Comments
Comment #1
merlinofchaos CreditAttribution: merlinofchaos commentedI will usually store data needed in the object cache, and actually modify the form order based upon that data in the initial call to the wizard. The reason for this is that changes to the form info won't be preserved from page to page, unless you actually cache the form info itself in your object cache (which is totally a possibility if you want to do it that way), and then pass that to the wizard from your object cache, if that's how to do it.
Comment #2
grasmash CreditAttribution: grasmash commentedThanks for such a quick response. I've taken your suggestion and changed my application so that the form is built from the object cache. That part works.
However, I'm having difficultly determining exactly how the form_info should be modified in order to skip or remove steps. I saw #1570628: Documentation: How to skip one step in wizard programatically?, which suggests that modifying $form_state['clicked_button']['#next'] would correctly set the redirect for the next step, but it doesn't seem to work for me. A few other variables look tempting:
Anyway, the best way to communicate this may be to simply show you the code. No one likes looking through huge code blocks... but these should be fairly straightforward implementations of the form wizard.
Build form using data from object cache:
In my custom submit handler for step 1 (user_register_form), modify the form_info in the object cache:
A few utility functions to handle the object cache:
Unfortunately, the 'location' step is still set as step 2 after this modification. Unsetting the 'order' key merely removes the step from the form trail, and removing the 'forms' key will result in a blank page. Regardless, I am still redirected to the original step 2 url.
Thanks in advance!
Comment #3
grasmash CreditAttribution: grasmash commentedManaged to figure this out! The issue was that the form had multiple submit handlers, and they weren't being called in the right order. After defining the correct order, everything seems to work.
Comment #5
AritoMelo CreditAttribution: AritoMelo commentedHi guys!
I need to change the step order for BACK button as u guys did for NEXTso far I can't get it working.
I've already did for NEXT btt, and it works.
Any idea?
Comment #6
medienverbinder CreditAttribution: medienverbinder commentedHi aritomelo!
You could use the next callback function (The function to call when the next button has been clicked) and fiddle "$form_state['redirect']"
Example:
Comment #7
IRuslan CreditAttribution: IRuslan as a volunteer and at DrupalJedi commentedComment #8
IRuslan CreditAttribution: IRuslan as a volunteer and at DrupalJedi commentedI have a multistep custom form.
Based on a value of radio button on the first step, I need to turn on or off step 3.
If I made it in submit, this values will be lost.
Therefore I find out it could be good to make possible to modify $form_info within one of the form_submits and pass it to further steps.
Currently it's not possible because in ctools_wizard_multistep_form() we have separate variable $form_info, and changes in $form_state['form_info'] will be reflected for internal call of ctools_wizard_multistep_form().
That's why I propose to update $form_info values from $form_state['form_info'] after:
$output = drupal_build_form($info['form id'], $form_state);
string.
With this correction, it's possible to do stuff like that:
Comparing to the current approach, it's easy to control a whole state of form_info, instead of manipulating with next or previous steps.
See patch in attachment.
Comment #9
Chris Matthews CreditAttribution: Chris Matthews as a volunteer commentedThe 2 year old patch in #8 to wizard.inc applied cleanly to the latest ctools 7.x-1.x-dev, but still needs to be reviewed and tested.
Comment #10
MustangGB CreditAttribution: MustangGB commented