Adding dynamic fields to Signups on Drupal

In my day job at SkySQL I work with Drupal as our content management system.  One thing we often need to do is provide a way for people to sign up for events and the like.  One such event is the upcoming SkySQL and MariaDB: Solutions Day for the MySQL® Database and unlike other events we needed to take into account the dietary requirements of those wishing to attend.

For events registration we use the Signup module and use a theme template function to provide a set of standard fields.  The code looks something like this:

function ourtheme_signup_user_form($node) {
$form = array();
// If this function is providing any extra fields at all, the following
// line is required for form form to work -- DO NOT EDIT OR REMOVE.
$form['signup_form_data']['#tree'] = TRUE;

$form['signup_form_data']['FirstName'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#size' => 40, '#maxlength' => 64,
'#required' => TRUE,
);
$form['signup_form_data']['LastName'] = array(
'#type' => 'textfield',
'#title' => t('Last Name'),
'#size' => 40, '#maxlength' => 64,
'#required' => TRUE,
);

And so on, building up the elements and then returning the form.  This is great because it allows us to have a standard set of fields for all signup pages, making life a lot simpler when creating content that requires registration.  But the Solutions Day event required an extra field.  I could have done this a number of ways, including putting logic in the template file to check for that particular node and only display the field then, or perhaps some other hack specific to this node.  I, however, don't like specifics and tend to look for a generic solution, as the exception invariably becomes the rule.

For this exercise I wanted to be able to have a way of specifying for a particular node any extra fields that are available for this form.  So I now have in the template.php file the following code:

// If there is a special field required for this, check and display
if (!empty($node->field_signup_extra) && !empty($node->field_signup_extra[0]['value'])) {
$extras = explode("\n", $node->field_signup_extra[0]['value']);
foreach ($extras as $field_def) {
$field_def = trim($field_def);
if (empty($field_def)) {
continue;
}
$elems = explode('|', $field_def);
$field_name = array_unshift($elems);

$form['signup_form_data'][$field_name] = array();
foreach ($elems as $field_element) {
list($key, $val) = explode('=',$field_element);
if ($key == 'options') {
$val = explode(',', $val);
}
$form['signup_form_data'][$field_name]['#' . $key] = $val;
}
}
}

Now all I need to do is create a field that is non-displayable but contains information to build extra fields.  For example the content that describes the Dietary Requirements field is:

dietary_requirements|title=Dietary Requirements|size=40|type=textfield

The production version does a little more analysis of the input to ensure there are no possible attack vectors, but I've left that out for clarity sake.

Now, if I have an event (or other content type) that needs extra signup fields, I ensure that the content type has the new Signup Extras field and fill it on the new content with a simple field definition that Signup can use.

No feedback yet


Form is loading...