How to create a webform with code (programmatically)
I was hoping to add a webform to a recipe, but can not do this with config files - since webforms are nodes and are not currently stored in config files.
I went ahead and created a recipe for a Contact Us form that uses the Webform module instead of the core Contact module. Because, I was not able to use config files, I used PHP in the .install file of the module to create the form. Here is the code I used:
You posted this as I was typing my answer. Have you actually tried what you posted? I don't think creating a node and passing the components to it will work... but maybe I'm wrong.
@argiepiano - You are so very fast. I figured out the answer before posting the question, but wanted to add it to the forum for future reference (next time I need to do this and forgot I did it before). I really thought I would get my answer up before anyone else did.
It's still interesting, because you provide a different way of doing it that may or may not be better.
Yes, my way works. I've already tested and released a recipe that creates a webform in the manner I proposed. That doesn't mean it's the best way to do it.
If my works, do you still think that might be a reason that your way is better?
Take a look at webform_node_insert(), which is a hook implementation that's triggered when a node is saved. If the node is of the webform type, then the rest of the code there is executed. To create a webform programmatically, you could simply create a node of that type programmatically. When you save the node, all of the code in webform_node_insert() will run, resulting on a ready-to-use webform node.
Of course, that will not add fields to the webform. In order to do that, you have to call webform_component_insert() and pass it a component. My guess is that the component will be an array with specific keys. To figure those keys out, you can manually create a webform, add components, and then use devel's dpm() to inspect the node. For example, this is a textfield component:
I've started to read up on creating modules, but I think this is waaay out of my league at the moment due to what I perceive as the complexity of my issue.
find all files with...
@NumerousHats How about using Bee from the command line to upgrade core and modules.
I have some scripts using a modified version of bee in my repos on github if they are any help.
I...
Posted14 hours 22 min ago by Francis Greaves (themetman) on:
We are looking for the similar functionality. I have noticed that Drupal 10 added https://www.drupal.org/project/media_download_tracker . It provides the following information for each...
Hello @NumerousHats,
I agree with your assessment. I am looking into this situation right now.
The root of this problem is that Backdrop CMS is implemented on top of a "stack" of...
Posted2 days 22 hours ago by Graham Leach (Graham Leach) on:
I just installed a Backdrop CMS site with version 1.27.0. I then used the user interface to download and update Backdrop CMS to 1.30.0.
After starting the update process, and on the...
Comments
I found an open issue to allow the storage of webform components in config files: https://github.com/backdrop-contrib/webform/issues/192
Until that happens, creating a webform programmatically (in code) is pretty much the same as creating any other type of node in code. https://forum.backdropcms.org/forum/how-does-one-create-node-programmati...
I went ahead and created a recipe for a Contact Us form that uses the Webform module instead of the core Contact module. Because, I was not able to use config files, I used PHP in the .install file of the module to create the form. Here is the code I used:
https://github.com/backdrop-contrib/contact_us_webform_recipe/blob/1.x-1...
(Use link to recipe. I tried to paste code here, but it's a mess. Will open an issue.)
You posted this as I was typing my answer. Have you actually tried what you posted? I don't think creating a node and passing the components to it will work... but maybe I'm wrong.
@argiepiano - You are so very fast. I figured out the answer before posting the question, but wanted to add it to the forum for future reference (next time I need to do this and forgot I did it before). I really thought I would get my answer up before anyone else did.
It's still interesting, because you provide a different way of doing it that may or may not be better.
Yes, my way works. I've already tested and released a recipe that creates a webform in the manner I proposed. That doesn't mean it's the best way to do it.
If my works, do you still think that might be a reason that your way is better?
Take a look at
webform_node_insert()
, which is a hook implementation that's triggered when a node is saved. If the node is of thewebform
type, then the rest of the code there is executed. To create a webform programmatically, you could simply create a node of that type programmatically. When you save the node, all of the code inwebform_node_insert()
will run, resulting on a ready-to-use webform node.Of course, that will not add fields to the webform. In order to do that, you have to call
webform_component_insert()
and pass it a component. My guess is that the component will be an array with specific keys. To figure those keys out, you can manually create a webform, add components, and then use devel's dpm() to inspect the node. For example, this is a textfield component: