8

I am wondering if there is any solution to store webforms in code. So that you can easily duplicate them on other sites and bundle them with your own modules. I am looking into something similair as the views API.

If it is not available how many people are interested in such functionality? I might develop a module which could handle storing of webforms in Code. And do you have any concerns in creating such a module?

Thanks, Jaap

3
  • You mean forms created with Webform module?
    – Mołot
    Nov 4, 2014 at 8:46
  • 1
    Yes I mean forms created with the webform module Nov 4, 2014 at 8:46
  • 1
    It's very easy actually, just have a look at how Webform share does it. (webform_share_export() and webform_share_node_insert() are the money functions). Can't say I approve of the use of eval(), but you could just as easily convert that to use a JSON object/serialized string instead. The only (small) difficulty you need to overcome is how/when your webform is applied to a new node, a node of course being required to attach the webform to.
    – Clive
    Nov 4, 2014 at 9:10

1 Answer 1

1

Not really, and there is no need for it

  1. If you need form available from code, Form API forms are not that hard to write from scratch. Contrary to Views, you can only theme Webforms bu their node ID, and that would change from site to site, so Webform forms bundled with module will not be convenient.

  2. If you want to bundle forms with your modules, and for any reason you can't use Form API, UUID Features Integration and Webform share provide ways to do it. It will not be a code in pure sense, but it should work.

  3. It's relatively easy to use hook_form_alter to obtain Form API representation of particular webform. Of course, you won't be able to change it easily in future, but again, contrary to views, it is good. Module is not hurt if some data is not displayed. Data not provided, or provided in a way module does not expect, may break things. So if module needs a form, it should not be easy to edit. Edits to form would require edits to module's code anyway, so Form API code makes things easier, not harder in the long run, in such situations.

6
  • 1
    While this is a good answer for the alternative, I think wanting to keep webforms in code is a pretty reasonable request (I don't agree that there's no need for it, or that it's not really possible). For example if you want to provide a base Contact form with a module which can then be extended by users through the UI, a webform would be ideal. Building that UI yourself would be a real pain. Since the webform object (or array?) sits on the node object anyway, it can be serialised and re-applied very easily
    – Clive
    Nov 4, 2014 at 9:30
  • @Clive But for basic contact, why would anyone require real code? Why wouldn't exported node (with UUID Features Integration you can export node to module) be enough?
    – Mołot
    Nov 4, 2014 at 9:32
  • Does that module synchronise the Webform object too?
    – Clive
    Nov 4, 2014 at 9:33
  • @Clive As far as I remember it did, with some issues but yes. Oh, and if custom code needs data from a form, wouldn't making it webform be dangerous? I know no way of making fields resistant to deletion in Webform (but I admit I haven't looked that hard).
    – Mołot
    Nov 4, 2014 at 9:35
  • 1
    Indeed, there's even a patch for webform to make integration work. I take it back :)
    – Clive
    Nov 4, 2014 at 9:37

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.