Webform is a great module in Drupal which help collecting user data. Recently i am working on a new website which needs a webform and one of the field is a selection list contains the node titles of a specific content type. I found a blog post about this dynamic select options feature in Drupal 6 by creating a custom module.
xebee – Drupal Webform : Add a dynamic select option list
Here is a similar approach for Drupal 7.
1. Create a new directory named as webform_options with the following 2 files inside.
webform_options.info
; $Id$ name = Webform options description = Preset options for webform field package = "Eureka" core = 7.x version = 7.x-1.0
webform_options.module
<?php /** * Reference: http://xebee.xebia.in/2011/06/14/drupal-webform-add-a-dynamic-select-option-list/ * The following piece of code is based on the blog post above written by Anubhav */ function webform_options_webform_select_options_info() { $items = array(); if (function_exists('_get_node_titles')) { $items['node_titles'] = array( 'title' => t('Node titles'), 'options callback' => '_get_node_titles', ); } return $items; } function _get_node_titles() { $options = array(); $sql = "SELECT nid, title FROM {node}"; $result = db_query($sql); foreach ($result as $row) { $options[$row->nid] = $row->title; } return $options; }
2. Enable the module and add a new webform field.
3. In the webform field setting page, pick Node titles for Load a pre-built option list. In addition, check the Listbox checkbox under Display as we want to have a selection list instead of radio button.
4. Save the form and you now have selection list which contains all node titles. If you want limit the options to a specific content type, just modify the SQL in webform_options.module.
Done =)
Reference: xebee – Drupal Webform : Add a dynamic select option list
Hi,
this tip works great but does not fit an additonal effort I have:
I have an event with multiple dates (date field, with multiple dates allowed).
Now I want to display each date with its eventname in options list.
E.g.: event 1 is on two dates, event 2 on 4.
23|event 1 – 1.1.2012
23|event 1 – 1.3.2012
24|event 2 – 1.1.2012
24|event 2 – 3.1.2012
24|event 2 – 15.1.2012
24|event 2 – 22.1.2012
By the way, can I change the safe_key to something else then the nodes id?
LikeLike
This can be done. But instead of querying the data by SQL, it is better to use the EntityFieldQuery to query the node data.
Here is an example
In you case, you need to add one more for loop for retrieving multiple date values for each event.
Hope this help.
LikeLike
Hi, this sound great. Just to be sure: if I use this way, will I get one Row for each event with all the dates in this single row, or is it possible to get one row for each pair of event and date? This is what I need.
Thanx again!
LikeLike
OK, got it 😉
In my case (as I just need the start date) I need something like
LikeLike
Good to know that you make it work. =)
LikeLike
Thanks a lot for this posting, really nice and understandable, but nonetheless I got stuck with a similar problem like Kaion:
I’ve got an event-node-type with date-field and want the dates to appear on the webform select list. But all I am able to get are the node-titles of the according nodes to be displayed on the webform-dynamic-select-list (and all other information, which are stored in the (mysql-)node-table like language, node creation-date etc.). I have been searching for a solution on the internet for hours, but can’t find any solution :(.
I guess I need to load the date-data from my (mysql-)_field_termim_datum-table for each node, but can’t figure out how. Do you know how? Thanks a lot in advantage!
LikeLike
Hi John, you dun’t need to load the data from the database. Just make use of the EntityFieldQuery and you retrieve all fields data of your selected nodes. In your case, the code should be sth like
Does it work for you?
And you can find more details about EntityFieldQuery in the following post
Drupal 7 – Get specific nodes using EntityFieldQuery
Hope this help. =)
LikeLike
@ykyuen thank you, that code did what I wanted to do. awesome!
LikeLike
Good to know that you have solved the problem~ =)
LikeLike
Has anyone had problems getting the list to display in the Load pre-built option list? After installing the custom module and following the steps, I still have not yet been able to display the custom pre-built list in the “Load pre-built option list” dropdown. Any suggestions?
LikeLike
Try to print some strings inside the _get_node_titles() function to check if the code has run or not.
LikeLike
Hey ykyuen,
No luck when printing a string inside the _get_node_titles() function. But the module is appearing and enabled in the modules list. Any idea why it would not be running?
LikeLike
can you paste the code here? please follow the Syntax Highlight to paste the code.
and what the the folder name of your custom module?
LikeLike
Hey ykyen,
My module is in the folder: webviewssel
which contains: webviewssel.info webviewssel.module
This is what I have for my .module file which is named webviewssel.module:
My webviewssel.info just has:
name = Webform Views Select
description = This module will be used to create selection options from a view.
package = “Webform”
core = 7.x
version = 7.x-1.0
LikeLike
Rename the function webform_options_webform_select_options_info() to webviewsse_webform_select_options_info()
Drupal makes use of the hook mechanism which is a kind of Inversion control. The above function is based on the hook_webform_select_options_info(). When you implement the hook, you have to replace the hook_xxx with <module name>_xxx.
LikeLike
Thanks ykyuen,
I should have known that, but I overlooked it!
LikeLike
Good to know that u have solved the problem. =D
LikeLike
Hi ykyuen, thanks for your explication.
i have done everything you listed.
i’ve created in (..sites/all/modules/) a new directory named as webform_options.
and i’ve created the 2 files inside, even with the same name,
but when i go to the admin->modules->
i cant find my new module to activate it, what im doing wrong?
thanks again.
LikeLike
Sorry just had to clean cache…
thx
LikeLike
Good to know u have figured it out.
Thanks for your comment. =)
LikeLike
Hi,
I am new with Drupal. I want to add pre-built timezone drop down in webform.
Please help me!!
Thanks
LikeLike
Replace the webform_options.module by the following one.
You could find the System timezones in the pre-built option list
LikeLike
Hi I’m not that savvy with code…could you let me know how to just select a specific content type. I know your mentioned that one would just modify the code in the module, but I just dont know what to ‘modify’!…thank you.
LikeLike
Replace the <content type machine name> with your content type machine name.
See if it works for u.
LikeLike
Thank you for the tutorial! The instructions for how to do this in the official Webform documentation failed me, but this worked great!
LikeLike
Good to know that i could help. =)
LikeLike
Thank you so much for the tutorial! Very helpfull!! Now I already managed to have a dropdown of the titles of a certain content type. But now i’m struggling with the multilanguages… Is there a way to show only the node titles of the language that is active for the user (and hide the titles of the other 2 languages)
I already used the code you suggested to Collin and works great! I’m trying to add a line with something like global $language; or $lang_name = $language->language; but I don’t seem to manage to get it right…
All suggestions / help are very welcome!
Thanks in advance!
LikeLike
OK tried to make it work but no succes untill now. The following code gives me the least errors for now, but still pretty massiv… testsite crashed…
I’m pretty new to coding like this. Anybody an idea what is going wrong?
LikeLike
Forgot to mention: I have a content type ‘seminaries’ and a multilingual site (3 languages)… What I’m trying to do is generate a list of the node titles, but only thos from the user’s active language must be shown, the rest will be hidden (whereas now everything was shown)
LikeLike
Allright!! I’m very sorry for all the spamming, but finally found the way. Here’s the code that did the trick for me
Thanks again ykyuen for the code!
LikeLike
Good to know that you could solve it by yourself. =D
LikeLike
This has been immensely helpful after many hours of painful attempts at getting webforms to work for my needs. That said, I’m still stuck. Perhaps you can help?
Here’s my situation:
I have three webforms for three class sign-ups. At this point I’ve given up on using a single view output to work for all three, so I have three class content types (one for each class). I need to add all three types to my webform options as a select list in each form.
This code works wonderfully for adding one type of node to the select list options, but when I try to duplicate the initial module to use for the second two lists, it breaks.
I’m extremely ignorant with regards to PHP so I’ve no idea how to change the code to work for me. Does any of this make sense?
LikeLike
Can you follow the syntax highlight and post your code here?
Can you you give me more details on the logic you want to have?
Is that you want to have 3 webforms and each on the want to have a preset select option list of different values?
LikeLike
hi, it is possible if there are 2 date field, once the first date field are selected, the second date field will auto select a date which is 2 years/month later of the first date field, thx.
LikeLike
It should have nothing to do with the topic of this post.
In your case, i think you need to work on Javascript to achieve this feature.
LikeLike
Hi, great post, very helpful. I’ve got an immediate problem needing a solution, sorry off topic a bit from your original, wondering if you could give a heads up.
I have a webform with a picklist that specifies dept. Rather than the user having to set which dept this form originates, i’d like that set via a passed parameter on the menu URL. So for example, in Dept X webpage, the menu URL for the webform has a parameter that is used to set the value of the dept pick list on the form (ie from Dept X). That way i can hide the list and each form has its orginating dept set based on where on the website it originates.
LikeLike
you can refer to the following post.
Working with Default Values
LikeLike
Hi, I’m really grateful to find this article. I’m wondering if you could help me set default values for the node titles from the URL. somewhat similar to nodereference.
thank you
LikeLike
Parabéns, muito bom!
LikeLike
Obrigado por seu comentário. =)
LikeLike
I am trying to add configurable options to this module. The API for hook_webform_select_options_info() says that you can provide an “options arguments: Any additional arguments to send to the callback.”
How do you set values of options arguments – as an array or a string? Also, whichever way I set the arguments, I am not able to access them in the callback function. How should the arguments be accessed inside the callback function?
Thanks for the gr8 post.
LikeLike
The options argument could be set in this way.
And your call back function should look like this
LikeLike
I tried that out. I set up ‘options arguments’ with array(‘hello’, ‘world’) and then I write the _get_node_titles() as:
Now I get the error “Warning: Illegal offset type in _get_node_titles()”.
Trying to print $arg1 in _get_no_titles() returns something like: Array ( [nid] => 2 [cid] => 1 [pid] => 0 [form_key] => select [name] => select [type] => select [value] => [extra] => Array ( [items] => 2|Get a Quote 3|test 1|Welcome to Base D7 [options_source] => node_titles [multiple] => 0 [title_display] => before [private] => 0 [aslist] => 0 [optrand] => 0 [conditional_operator] => = [other_option] => [other_text] => Other… [description] => [custom_keys] => [conditional_component] => [conditional_values] => ) [mandatory] => 0 [weight] => 0 [page_num] => 1 )
And trying to print $arg2 returns 1.
I am running on Drupal 7.22 & webform 7.x-3.19.
Could that be a bug with webform?
Regards.
LikeLike
you can try this example, does it work?
Support callback arguments for hook_webform_select_options_info
LikeLike
I tried it out. Doesn’t seem to be working. This is what I did – in hook_webform_select_options_info() I pass the arguments with the key ‘callback arguments’ (& not ‘options arguments’) and then set it to array(‘hello’ => ‘world’). In the callback function, I add four arguments to the signature (as per the post you mentioned) and the last one should contain the passed arguments. But, the fourth argument is nothing but an empty array. Also, nothing is returned when I set the key in the hook to ‘options arguments’, not even an empty array. The first argument returns the webform component information only, the second & third arguments return 1.
However, I got a work around which is working. Since, in hook_webform_select_options_info() the ‘options callback’ can be set to the name of the callback function (as string) only and the ‘options arguments’ is not working as desired, create as many items as you require and then for each of the ‘options callback’ pass a lambda function using the create_function, which essentially returns the name of the function created and executes when called.
My code looks something like this:
LikeLike
Portions of my code above got removed. Here is the complete code – http://pastebin.com/4qZyPsX9.
Thanx yk yuen!!!
LikeLike
Hi KN,
I have fixed your code snippet in the above comment. good to know that you have solved the problem and thanks for your code. =D
LikeLike
How could I modify this to work via an auto complete text field instead of a select option field?
LikeLike
Webform Autocomplete may help. but i am not very sure how to make it dynamic. It might provide u some hooks which you could implement your own autocomplete set.
LikeLike
Thanks. Works like a charm.
LikeLike
thanks for your comment~ =D
LikeLike
If I want to appear directly in the webform each page, without requiring the user to specify option list
How to write code
LikeLike
Could u elaborate more? not sure about what u mean.
LikeLike
I want use to different webform nid, load a different Dynamic select options
-How get to different webform nid
-How use to?
LikeLike
You can add different select options list in the return $items inside the hook_webform_select_options_info() function. Then you can edit different webforms and bind different select options list.
LikeLike
The tutorial is fantastic! but i have a problem that doesn’t get solve…I need that VALUE is the title and the KEY is the email
I need help please!! 😦
LikeLike
You could write your own function in the “options callback” which returns the key value pair.
LikeLike
Can we add dependent drop-down?
Thanks in advance. Awsome blog!!!
LikeLike
what do u mean by dependent drop down? you mean 2 drop down lists and the available options of the 2nd one depends on the 1st drop down?
LikeLike
Hi,
I have added this code and module and have it working as designed. However, the content type I am trying to display contains items that need to be displayed based on permissions. That is, I would like my “Programs” select list to only include items that the current user has permissions to view. Right now, every “program” is shown to all users. For my case, I am designing a registration form and I’d only like Anonymous users to be able to sign up to Programs that they have been given permission to sign up for.
My current code is this:
LikeLike
you can use user_access to check if the user has the corresponding permission. Add this checking in the _get_node_titles() function.
LikeLike
This works so well.. however I need my ‘safe_key’ to be a different field from the node and the VALUE to be the node title… any ideas?
LikeLike
The safe_key should be the key in the $options hash. So in my example, the safe_key is the node id.
LikeLike
Awesome.. I will give that a go… will the list be auto updated when new nodes are added?
LikeLike
Yes. i think so.
LikeLike
Cant get it too work. I have a field called ‘field_off’. I need that field to be my safe key is this possible?
LikeLike
In the example, the options callback will run a plain sql which only return the nid and title.
In your case, you could try to get the nodes by using EntityFieldQuery and node_load_multiple(). The returned array should contains all node with all the fields.
Drupal 7 – Get mulitple nodes using EntityFieldQuery and node_load_multiple()
LikeLike
ooof.. might be a bit beyond me 🙂
LikeLike
just follow and see what you get.
you can always print the object/array to the screen by
LikeLike
I have had a look but cant get my head around it! Don’t suppose you do freelance work of this kind?
LikeLike
I have sent you an email. Check your inbox. =)
LikeLike
Thanks so much for this. Great tutorial.
I have a situation where I want to add multiple select options from different databses.
For example – I want to add 1 database with all car models. I create the module as per your instructions and all works well.
I then want to add ANOTHER select option for motorcycles. I create a NEW module – rename accordingly and make the respective changes but only my first select option appears in webform.
Any idea what I am doing wrong?
This is my module for CARS:
And her is my module for MOTORCYCLES:
Thanks in advance for any help!
LikeLike
use another name for the items array key
LikeLike
Nice trick !
It’s really useful since there is not this feature in the webform core module…
But what if you want to display a field value ? We all know that it’s more easy for a client to edit a node than a webform field (and less risky ^^).
For exemple : the webform is placed as a block in a content type. This content type (say : Meetings) possesses a simple text field with multiple values where we write informations about the meeting (place, dates, …).
How do you get these values from the field ?
LikeLike
I answer to myself. ^^
I did something to get field value (based on a content type).
The webform is placed as a block on each node and the field values (simple texte with multiple value) are differents on each node, so the list in the webform is filled with different values on each node. 🙂
Sorry for my bad english, I invite you to test it to see by yourself 😉
LikeLike
You can use EntityFieldQuery to select the node fields in the ‘options callback’.
The following posts should help.
LikeLike
This is the best tutorial for doing this! Quick question, do you know a way to auto reload the list items? seems like if you add new nodes, it will not update the select field in webform for some reason.
“Use a pre-built list of options rather than entering options manually. Options will not be editable if using pre-built list.”
So if I add a new node, then I do to the form, that new node is Not on the list. Any work around for that? Thank you so much!!
LikeLike
Excellent Tutorial !! I am having a bit of a problem, I followed the instructions and everything seems working fine BUT if I add a new Node, that does not show in the select field. Select field is not being updated!!
“Use a pre-built list of options rather than entering options manually. Options will not be editable if using pre-built list.”
Webform 4.4 – Any work around for this? I really appreciate it
LikeLike
You know what !! My Bad, it is working , clean cache…
Anyways, how could I get taxonomy vocabulary term list instead of nodes?
Thank you!
LikeLike
EntityFieldQuery could help you to select the specific terms.
LikeLike
Thanks for the info about creating dynamic webform options. The only problem I have is that I want to be able to have all the options in the pre-built option checked off by default when someone first accesses the webform. I haven’t found a way to do this. Any suggestions?
LikeLike
You could try using the hook_form_alter() to to set the default values for your webform. You can find more details below.
Forms API – Modify Forms with hook_form_alter()
LikeLike
Thanks. I ended up having to do that. I was hoping for an easier solution.
LikeLike
Seems no straight forward way but form alter. Let me know if you find a simpler solution. =)
LikeLike
Hi,
do you know how implement a option field related to another with ajax?
LikeLike
i think u may need to implement by urself.
see if this help
Drupal 7 – Simple Ajax implementation @ 1
LikeLike
Hi, I did it with hook_form_alter method.
Thanks
LikeLike
glad to know that. =)
LikeLike
Hi ,
Thank you for this post.
I am looking for a editable Webform select options. Like Time picker where user can add time manually or he can select from drop down.
Can we do that?.
I am very new to Drupal and PHP.
Thank you in advance.
LikeLike
Something like this? https://www.drupal.org/project/date_popup_timepicker
LikeLike
Excelente, Muchas Gracias
LikeLike