There are use cases where multiple drafts are desirable / necessary, and the work-around needed to trick webform into allowing them is ugly and brittle. #697146: Ability to save multiple drafts As requested, that issue is closed this one for a new api is now open.

This proposal adds a trivial alter function to the retrieved draft submission's sid, allowing a module developer to choose whether to allow multiple drafts, and if so when and how. Patch forthcoming.

function _webform_fetch_draft_sid($nid, $uid) {
  $sid = db_select('webform_submissions')
    ->fields('webform_submissions', array('sid'))
    ->condition('nid', $nid)
    ->condition('uid', $uid)
    ->condition('is_draft', 1)
    ->orderBy('submitted', 'DESC')
    ->execute()
    ->fetchField();
  if ($sid) {
    drupal_alter('webform_draft', $sid); 
  }
  return $sid;
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

DanChadwick’s picture

Status: Active » Needs review
FileSize
1.75 KB

Here's the patch. I hope quicksketch can review this, since I'm not sure anyone is watching the old multiple drafts issue from 2010.

I have tested this in my application, and it meets my needs nicely. I have read the original multiple drafts thread, and I believe this is sufficient to meet the needs of the users who posted there (assuming they are capable of making a trivial module.)

Also, webform now displays a list of previous submissions, which is very handy when presenting a new blank additional draft page. They can click to link to see their previous submissions, drafts included.

quicksketch’s picture

If all we're doing is modifying the result of a query, could we just get away with adding a tag to the query? Then you could use hook_query_alter() to modify it.

jomarocas’s picture

i try to apply the patch but nothing, dont work, i have webform 4.x, i have i form and later save but the form contain the same values that i write, ideas? thanks

quicksketch’s picture

@jomarocas: This patch doesn't do anything by itself, it's only an API addition that would enable different functionality in the future.

DanChadwick’s picture

If all we're doing is modifying the result of a query, could we just get away with adding a tag to the query? Then you could use hook_query_alter() to modify it.

The normal use case is to return NULL (or FALSE -- both equally good). I suppose you could add a tag as you suggest, and then modify the query to definitely fail (set the range to start at a gazillion, search for a negative sid, something). However, this seems less clean, no?

Let me know if you want me to do the tag approach. I'm happy to make a patch for that.

quicksketch’s picture

I suppose you could add a tag as you suggest, and then modify the query to definitely fail (set the range to start at a gazillion, search for a negative sid, something). However, this seems less clean, no?

If all you're wanting is to disable the normal draft functionality, I'd prefer that we use a variable rather than any kind of alter() hook. Can you think of any useful functionality for your hook or query alter other than returning FALSE?

DanChadwick’s picture

@quicksketch. Two part reply.

FIrst, I'm not sure what you mean by a variable. Do you mean some sort of UI checkbox in the webform settings? Maybe change Allow drafts to a select list with No, One at a time, Multiple? If so, then we need to make it clear in the #description that the user is responsible for managing the multiple drafts. For my use, this would be fine.

Second, I can imagine use cases for setting the sid to other than FALSE/NULL. For example, right now I generate links to the submission edit page for the user to complete their evaluations. I might instead put the sid into a URL query, then extract it in the alter or tag function, supplying the desired one. That said, if I had the UI option from above, I would probably continue to generate the existing URLs and be happy.

Alternatively, I can imagine someone wanting the multiple submissions responded to oldest-first (FIFO). Currently it is LIFO.

I'm not picky about the solution, and I'm happy to prepare a patch and test it. Let me know your preference.

DanChadwick’s picture

@quicksketch - which direction do you prefer? I'd like to finish this up.

quicksketch’s picture

FIrst, I'm not sure what you mean by a variable. Do you mean some sort of UI checkbox in the webform settings?

I mean a hidden variable that could be set by your module if it needed this functionality. i.e. variable_get('webform_draft_autoresume', 1). Your module could set this variable during requests by using $GLOBALS['conf']['webform_draft_autoresume'] = 0;

Overall, I'm not sure what to do about this patch. It sounds like you're making Webform do something it wasn't intended to do, so finding an adequate approach to the API is elusive, since the system wasn't intended to do what I think you're trying to do.

DanChadwick’s picture

A global variable won't work well as various webforms have various settings. Not all of mine allow drafts, much less multiple drafts.

We could put an optional variable in $node->webform which a custom module could set when the node is loaded.

I am confused about why you feel multiple drafts are outside webform's use cases. There seems to be ample demand. Webform does 95% of what I need. It and Drupals' APIs let me do the rest elegantly. Really the hack I do for multiple drafts it the only part the nauseates me a bit.
;)

EDIT (to elaborate):
If I had my druthers, I would just change the Allow drafts checkbox to be a select list. The site designer then is responsible for offering the links to finish the webforms. This is easily done with a view or maybe a simple custom block. But I accept your rejection of the UI approach. I am open to any technique that lets me enable multiple drafts for certain of my webforms.

DanChadwick’s picture

The UI approach, for your consideration.

quicksketch’s picture

Hm, I like the thinking in #11, but it's not typical Webform-style to require a user to do something manually in order to get a feature to fully work (in this case assembling links to edit the drafts). Getting multiple drafts fully supported would probably take a more significant effort, which I assume is exactly what you're doing in your project, but probably not in a way that is going to be reusable within Webform itself.

Given the choice between a UI option that doesn't provide full functionality and an API option that enables another module to attempt that functionality, I'd take the latter in this case. Sorry for all the dilly-dallying around here, but let's go back to the patch in #1. If you add the docs for webform.api.php (since we try to document all hooks) then we can add this in.

quicksketch’s picture

Status: Needs review » Reviewed & tested by the community

If you add the docs for webform.api.php (since we try to document all hooks) then we can add this in.

*Forehead smack*

The docs are already in the patch. I'll commit #1 next time I'm going through the queue.

quicksketch’s picture

I've given Dan commit access, so he can commit this himself now. :)

DanChadwick’s picture

Status: Reviewed & tested by the community » Fixed

Thanks. I'll try to be helpful.

Committed #1 (HOOK_webform_draft_alter approach) to 7.x-4.x.

Status: Fixed » Closed (fixed)

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

fenstrat’s picture

Version: 7.x-4.x-dev » 8.x-4.x-dev
Assigned: DanChadwick » fenstrat
Status: Closed (fixed) » Patch (to be ported)

Needs porting to 8.x-4.x.

fenstrat’s picture

Version: 8.x-4.x-dev » 7.x-4.x-dev
Assigned: fenstrat » DanChadwick
Status: Patch (to be ported) » Fixed

Committed and pushed 69d18da to 8.x-4.x. Thanks!

  • Commit 66e68dc on 8.x-4.x authored by DanChadwick, committed by fenstrat:
    Issue #2198489 by DanChadwick: Added multiple drafts API.
    

Status: Fixed » Closed (fixed)

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