Skip to content

Custom Fields in Drupal Event Signups

2006 December 12
by dnorman

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.

Similar Posts:

  • None Found
23 Responses
  1. August 25, 2007

    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.

  2. ABC permalink
    October 9, 2007

    Awesome, helped a lot! Wonderful you are man!

  3. October 14, 2007

    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?

  4. Tanya permalink
    January 11, 2008

    thank you so much, this was very helpful.

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

  5. November 19, 2008

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

  6. January 5, 2009

    How can you make certain fields required…?

  7. February 10, 2009

    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

    • Art permalink
      May 20, 2009

      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

      • Kourosh permalink
        February 21, 2011

        Actually, I just did as you told, but no luck. I couldn’t get it to work…

  8. June 22, 2009

    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') ) ); }

  9. June 25, 2009

    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?

  10. Don Asok permalink
    August 31, 2009

    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

  11. Brian permalink
    January 8, 2010

    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?

  12. June 15, 2010

    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?

  13. Gretch permalink
    January 13, 2011

    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.

  14. toemaz permalink
    December 12, 2006

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

  15. dnorman permalink
    December 12, 2006

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

  16. toemaz permalink
    December 13, 2006

    Thx for feedback and the nice article!

  17. December 19, 2006

    Cool stuff – but what calls function uofc_thisisnow_signup_user_form() ?

  18. dnorman permalink
    December 19, 2006

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

  19. simon permalink
    January 9, 2007

    Excellent article thanks saved me alot of time

  20. dnorman permalink
    January 9, 2007

    Great! Glad to help out, Simon!

  21. April 30, 2007

    Thanks This was very helpful.