Custom Fields in Drupal Event Signups
We’re using the Event module to list our workshops at the Teaching & Learning Centre, and the Signup module to let people register to attend workshops (or other events). It’s working really quite well, but we needed to add some extra fields to the registration form so we could track Faculties, Status, etc…
“Sure,” I said, “Drupal’s open source, so we should be able to add any fields we want. Worst case scenario? We’d have to fork Signup.module and maintain our own version with our custom fields in it.”
I then proceeded to drag my feet, not looking forward to having to maintain a module for something as simple as adding some custom fields. Maybe I could use the FormsAPI and insert the fields through some custom code?
So, I poked through the signup.module source code to see what would be involved. I’d braced for some rather convoluted and involved hackery. I blocked my schedule for the day so I’d have time to dedicate to the task.
Then, I saw that the module developers had already done the work for me. They implemented the signup form’s fields as a themable method, letting me override it on a per-theme basis. Without having to touch the code for the module itself. Brilliant. Absofrakking brilliant. So, I added this code to our theme’s template.php file (the theme is called “uofc_thisisnow”):
function uofc_thisisnow_signup_user_form() {
$form['signup_form_data']['#tree'] = TRUE;
$form['signup_form_data']['Name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#size' => 40,
'#maxlength' => 64
);
$form['signup_form_data']['Phone'] = array(
'#type' => 'textfield',
'#title' => t('Phone'),
'#size' => 40,
'#maxlength' => 64
);
$form['signup_form_data']['Faculty'] = array(
'#type' => 'textfield',
'#title' => t('Faculty or Department'),
'#size' => 40,
'#maxlength' => 64
);
$form['signup_form_data']['Status'] = array(
'#type' => 'select',
'#title' => t('Status'),
'#default_value' => t('Faculty Member'),
'#options' => array(
'faculty' => t('Faculty Member'),
'staff' => t('Staff'),
'student' => t('Student'),
'other' => t('Other')
)
);
return $form;
}
That results in a signup form that looks like this:
TLC Workshop Signup with Custom Fields
The beauty of this, since it exposes the full FormsAPI, we can add select menus, radio boxes, default values, etc… Without having to touch the code of the Signup module itself. Very cool stuff.
hi, big thanks for helpfull info!
PS: Link to http://api.drupal.org/api/HEAD/file/developer/topics/forms_api_reference.html is dead
i can not open this page.
Awesome, helped a lot! Wonderful you are man!
I am needing to do the exactly the same thing, but you lost me on where you placed the code for the edits. it sounded like in the theme template (in any particular place). More specifically how can I create several different signup forms using the same theme? Possible?
thank you so much, this was very helpful.
what if i want to make some fields required, how would i do that?
Thank you so much. I was looking to do something exactly like this and your solution came up. This was very helpful.
How can you make certain fields required…?
I think its good article! But not full, and may be difficult to implement for beginners (as i am:-))
I should make all clear: when you add a code with custom field definiton, posted in article to your template.php file you MUST add the same code to the your signup_form.inc file (locatet in …sites\all\modules\signup\theme). if you dont add you will see blank drupal page or no your field showed up.
So you see you must touch yhe code of the module. I spent 1 day trying to implement all this
Actually you shouldn’t add the code to signup/theme/signup_form.inc
You should follow the direction in singup_form.inc: Copy the entire function theme_signup_user_form from signup_form.inc. Paste it into template.php Change the name of the function in template.php to yourthemename_signup_user_form Make the changes to this function as this tutorial explains. Then clear your the theme registry for the changes to take effect. To clear the theme registry, do one of the following things: 1. On the “Administer > Site configuration > Performance” page, click on the “Clear cached data” button. 2. With Devel block enabled (comes with devel module), click the “Empty cache” link. 3. With Admin Menu module enabled go to admin_menu/flush-cache/theme
Actually, I just did as you told, but no luck. I couldn’t get it to work…
This was still useful to me today. I used pasted the code from the signup_form.inc file into my template.php for my zen subtheme. Since my site used events for other purposes and did not want the extra fields in all events, I create a new event type called ‘parade’ and used an if statement to control if the extra fields showed up like this:
if (!empty($node) && $node->node-type == t('special_node_type_name_here')) { $form['signup_form_data']['Faculty'] = array( '#type' => 'textfield', '#title' => t('Faculty or Department'), '#size' => 40, '#maxlength' => 64 ); $form['signup_form_data']['Status'] = array( '#type' => 'select', '#title' => t('Status'), '#default_value' => t('Faculty Member'), '#options' => array( 'faculty' => t('Faculty Member'), 'staff' => t('Staff'), 'student' => t('Student'), 'other' => t('Other') ) ); }I successfully used the procedure from this blog post. However, how do I alter the signups list, so I can view the fields I have added?
Hi,
Your Tutorial was very helpful but is there in any way to enable a text area only when the user select the “Other” option
with regards, Don Asok
What if i want to validate the entries? For instance, if i had an Email and Confirm Email field and i wanted to compare them to make sure that they were the same, how would i do that? I got this to work by creating a signup_alter module of my own, but the nice part about using the template.php file is that it takes care of all the forms in one shot (the edit, the admin, etc). Any ideas how to add a validation function to this?
Hey there, great post saved me a heap of time. Is there an easy way to make the fields required like the first email box?
Hi this post was very helpful. However, i wanted to combine this with the ubercart. Would this be able to work the same way and go to the cart? I’m a Drupal newbie.
And can you tell us how the custom fields are being stored in the database? Serialized in a text db field?
Ah, yes. I forgot that part. Yeah. It basically just serializes an array of values into the signup_log table, using the form_data field, like this:
That gets turned back into a hashtable in PHP so it’s easy enough to call via arrayname['valuename'] convention to get the values back out (but it takes care of doing that automatically if you don’t want to do anything custom for display).
Thx for feedback and the nice article!
Cool stuff – but what calls function uofc_thisisnow_signup_user_form() ?
Jim – that gets automagically called by Drupal’s theming engine, triggered by a request by signup.module. It looks in template.php for THEMENAME_signup_user_form()
Excellent article thanks saved me alot of time
Great! Glad to help out, Simon!
Thanks This was very helpful.