Custom Fields in Drupal Event Signups

Filed under: Uncategorized. Tags: , ,

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 FieldsTLC 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.

Comments

13 Responses to “Custom Fields in Drupal Event Signups”

  1. toemaz on December 12th, 2006 12:42 pm

    And can you tell us how the custom fields are being stored in the database? Serialized in a text db field?

  2. dnorman on December 12th, 2006 1:04 pm

    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:

    a:4:{s:4:"Name";s:15:"Testy Testerson";s:5:"Phone";
    s:3:"411";s:7:"Faculty";s:3:"TLC";s:6:"Status";s:5:"other";}

    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).

  3. toemaz on December 13th, 2006 3:34 am

    Thx for feedback and the nice article!

  4. Jim Manico on December 19th, 2006 2:22 pm

    Cool stuff - but what calls function uofc_thisisnow_signup_user_form() ?

  5. dnorman on December 19th, 2006 4:27 pm

    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()

  6. simon on January 9th, 2007 3:28 am

    Excellent article thanks saved me alot of time

  7. dnorman on January 9th, 2007 9:04 am

    Great! Glad to help out, Simon!

  8. Brent on April 30th, 2007 12:09 pm

    Thanks This was very helpful.

  9. Freeware Eugen on August 25th, 2007 6:59 pm

    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.

  10. ABC on October 9th, 2007 9:48 am

    Awesome, helped a lot! Wonderful you are man!

  11. Christina on October 14th, 2007 8:14 pm

    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?

  12. Tanya on January 11th, 2008 1:57 pm

    thank you so much, this was very helpful.

    what if i want to make some fields required, how would i do that?

  13. Stefanie on November 19th, 2008 9:14 am

    Thank you so much. I was looking to do something exactly like this and your solution came up. This was very helpful.

Leave a Reply

You must be logged in to post a comment.

Creative Commons License
This work is licensed under a Creative Commons Attribution 2.5 Canada License.