I've created a new webform component that introduces the "sort" field type.
The module uses drupal's build in drag'n'drop feature for selecting elements order (like in menu).

This is particularly helpful if you would like to have a field like this: "Please tell us which features of software X are most important for you".
Thanks to my component users can just rearrange the order of features the way they want.

All works great except for one small thing:
When component is rendered all it's items have default order (randomized or not - it doesn't matter). Users can rearrange items but each time they re-enter the page with "sort list", the previously selected order is lost and items are displayed in default order. This is very annoying because when a webform page has some other fields and validation of one of them fails, users have to rearrange items again. The real problem is that they may not notice that items they already rearranged are messed up again.

I've been trying to work around this but run out of ideas. Debugging whole webform module is what I would really like to avoid, so a little help from developers of webform module would be very appreciated :)

One ugly idea was to use $_SESSION var to store user selected order of items when my component is validated. Then use the data in render function to rearrange items the way user did earlier. Unfortunately _webform_render_component($component, $value = NULL) is invoked before component validation function :(

CommentFileSizeAuthor
#8 webform_sort.zip9.15 KBSiliconMind
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

SiliconMind’s picture

Category: feature » support
quicksketch’s picture

Isn't $value already given to you each time the component is rendered? You should be able to check the value there and adjust your component to match the order of $value.

SiliconMind’s picture

I thought of that but the $value is always empty. The same is with $component['value'].

quicksketch’s picture

Priority: Major » Normal

Hm, I think you're right. $value is only populated if you're editing an existing submission. It looks to me like the current value is only available AFTER the element has been built. This means that you need to use a #process, #pre_render, or #after_build to modify the element once its been built.

SiliconMind’s picture

Wired thing though is that according to this Forms API diagram http://drupal.org/node/165104 "validate" should occur before "render" but it does not in case of webforms :/

quicksketch’s picture

Webform does includes special validation that allows it to skip it if it's not necessary (such as going "Back" or using the "Save Draft" button), but it's still done before render. Otherwise required fields wouldn't be highlighted with the "form-error" class.

SiliconMind’s picture

Webform does includes special validation that allows it to skip it if it's not necessary (such as going "Back" or using the "Save Draft" button), but it's still done before render.

Before actual form render - YES
Before call to _webform_render_component() function - NO

Anyway, I managed to solve the problem. I store last user selected items order during validation, and restore it on #pre_render. Works like a charm, but from design point of view - well its damn ugly :)

The only thing left is to figure out how to request user confirmation on form submit if default items order is unchanged. A JS message box perhaps? But how can I hook to the "submit/next page" button to cancel form submission?

SiliconMind’s picture

Assigned: Unassigned » SiliconMind
Status: Active » Needs review
FileSize
9.15 KB

I think I managed to resolve all the issues. New field type seems to be ready. Anyone would like to test it?

By the way: naming convention for hook _webform_render_component is a bit misleading. The actual rendering occurs later in drupal's Forms API but this hook is called a lot earlier, when webform node is prepared. Some kind of diagram similar to this would be very helpful for component developers.

quicksketch’s picture

Status: Needs review » Fixed

Marking this support request as fixed since it seems like the issues are resolved. While I appreciate your sharing, the Webform issue queue isn't the best place to have add-ons reviewed, especially as part of a support request on a different topic.

SiliconMind’s picture

I agree. Just wanted to share here since I don't want to start new drupal project for now.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.