How to create webforms programatically

Submitted on Sun, 06/10/2012 - 20:25

Well, not really. I mean, you can create webforms programatically pretty easily. This tutorial will show you how easy. Or you could just use the rules module if you just want the node created. But also, I want to share the things that got me scratching my head, like creating the description of each webform component programatically.

I am sure you have had this scenario as well. You enabled the webform module and tought the site admins how to create them and add components. But each time they want to create a new webform, they send an email to you asking for help, and you practically end up creating the nodes for them, since it has already been 3 weeks since last time you told them how it was done. Ok, so let's make it dead simple for them. Create a node with a title, and put all components in the body - one per line. Ah, no more emails, and code nerds can go back to their terminal and away from clicking with a mouse.

Use case:

So I have this site where you can sign up for parts of an order. Like a co-op. Let's say we are ordering kittens. So an admin puts out the news that he is shipping out a new order, and these are the kittens that are up for grabs.

  • Grey kitten
  • Black kitten
  • Killer kitten
  • Clown kitten

We want to create a webform to find out how many kittens of each type the users of the site would want in this order.

So instead of me telling the admins to create a webform and add numeric components for each kitten, I just tell them to go ahead and click the big button that says “create order” (visible for admins only). In the title field, they give the order a name (like the name of the supplier) and in the body field they list all kittens available, one kitten per line. Optionally they can also add an URL to an animated gif of the kitten, if included on the same line in a parentheses. So much easier for them, so they can concentrate on kitten distribution instead. I also added a description field so they can use that for a closer description of the order. Ok, so this is the code (Drupal 7 obviously): eiriksm/c9e779b999b3da82158e

Ok. This is all pretty straight forward, eh? So the thing that had me going nuts a while, was adding the links as the description of each component. Looking at a webform node object, one would think it would go in the “extra” array of each component. This is how a webform node with a component with a description looks like:

$node->webform['components'][1]['extra']['description'] = 'A description';

But after repeating different takes on adding it to the array (even trying to modify $n after it is saved, and saving it again), nothing seemed to do the trick. Luckily, webform has its own hooks. Enter hook_webform_component_presave(). Or “Modify a Webform component before it is saved to the database.” as it says in the documentation. Perfect! Let's go ahead and add a link to the animated gif as a description:


Bottom line? Webform is awesome for these kind of user submissions, and now with programatically creating them just like we want, things just got real easy for the non-techies on this site. Now let's see how many killer kittens I can afford. Sorry for the large GIF this time, but I could just not help myself. Killer kitten to follow (5.7MB):