Webform Number Component

Drupal Webform Module

I have a requirement in my current project that requires to create a number of fields using  Drupal Webform Module. I have searched a lot for this at drupal.org but didn’t found anything close of my need. So I have decided to make my own module for creating number fields using Drupal Webform Module. It was a great experience to work with Drupal Weform APIs, so you will find below some notes about the development, a guide and the installation process of the module.

Development Guide:

1.)     Just make a file webform_number_component.module and define your component in the list of webform components.

function webform_number_component_webform_component_info() {

return array(

'number' => array(

'label' => t('Number'),

'description' => t('Basic textfield type for integer & float numbers.'),

'file' => 'components/number.inc',

'features' => array(

'email_name' => TRUE,

'spam_analysis' => TRUE,

),

),

);

}

2.)    Make the component file number.inc  and its intraction with Webform default component.

function _webform_defaults_number() {

return array(

'name' => '',

'form_key' => NULL,

'pid' => 0,

'weight' => 0,

'value' => '',

'mandatory' => 0,

'extra' => array(

'width' => '',

'maxlength' => '',

'field_prefix' => '',

'field_suffix' => '',

'disabled' => 0,

'unique' => 0,

'title_display' => 0,

'description' => '',

'attributes' => array(),

),

);

}

3.)    Define theme function for the Webform number component.

function _webform_theme_number() {

return array(

'webform_display_number' => array(

'arguments' => array('component' => NULL, 'value' => NULL, 'format' => 'plain'),

),

);

}

4.)    That is the rendering the Webform number component field.

function _webform_render_number($component, $value = NULL, $filter = TRUE) {

global $user;

$element = array(

'#type' => 'textfield',

'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],

'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',

'#default_value' => _webform_filter_values($component['value']),

'#required' => $component['mandatory'],

'#weight' => $component['weight'],

'#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],

'#attributes' => $component['extra']['attributes'],

'#element_validate'  => array('_webform_validate_number'),

'#theme_wrappers' => array('webform_element_wrapper'),

'#pre_render' => array('webform_element_title_display'),

'#post_render' => array('webform_element_wrapper'),

'#webform_component' => $component,

);

// Change the 'width' option to the correct 'size' option.

if ($component['extra']['width'] > 0) {

$element['#size'] = $component['extra']['width'];

}

if (isset($value)) {

$element['#default_value'] = $value[0];

}

return $element;

}

5.)    Validation of Webform number component field.

function _webform_validate_number($form_element, &$form_state) {

$component = $form_element['#webform_component'];

if (!empty($form_element['#value']) && !is_numeric($form_element['#value'])) {

form_error($form_element, t("%value is not a valid number.", array('%value' => $form_element['#value'])));

return;

}

if (isset($component['extra']['type']) && $component['extra']['type'] == 'int' && !ctype_digit($form_element['#value']) && $form_element['#value'] > 0) {

form_error($form_element, t("%value is not an integer.", array('%value' => $form_element['#value'])));

}

if (isset($component['extra']['min']) && strlen($component['extra']['min']) && $component['extra']['min'] > $form_element['#value'] && $form_element['#value'] > 0) {

form_error($form_element, t("%value is below the minimum allowed value.", array('%value' => $form_element['#value'])));

}

if (isset($component['extra']['max']) &amp;&amp; strlen($component['extra']['max']) &amp;&amp; $component['extra']['max'] < $form_element['#value'] &amp;&amp; $form_element['#value'] > 0) {

form_error($form_element, t("%value is above the maximum allowed value.", array('%value' => $form_element['#value'])));

}

}

6.)    Display data of the Webform number component field after the submission of the form.

function _webform_display_number($component, $value, $format = 'html') {

return array(

'#title' => $component['name'],

'#weight' => $component['weight'],

'#theme' => 'webform_display_number',

'#theme_wrappers' => $format == 'html' ? array('webform_element', 'webform_element_wrapper') : array('webform_element_text'),

'#post_render' => array('webform_element_wrapper'),

'#format' => $format,

'#value' => isset($value[0]) ? $value[0] : '',

'#webform_component' => $component,

);

}

function theme_webform_display_number($element) {

$element['#value'] = empty($element['#value']) ? ' ' : $element['#value'];

return $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];

}

Installation Guide:

1.)    Put the module folder in sites/all/modules.

Go to http://yoursitename/admin/build/modules and enable the Webform Number Component module.

Enable Webform Drupal Module

Now you are done with the installation process and you are able to create number field in your webform.

Create number of fields Drupal Webform

In the process of adding number fields there are two options that are integer & float. So always keep in mind to choose the integer and float option while adding the fields in your Webform.

Integer & Float Drupal Webform

This is the last part of the installation process. Enjoy this Free Drupal tool. Thank you for using and commenting it!

Abhishek
Drupal programmer at E-nova Technologies


Related Posts:

Tags: , ,

3 Responses to “Webform Number Component”

  1. vidhatanand says:

    Great work Abhishek. Can we have a module with install file and posted on the blog.

  2. Marco says:

    Joli travail. Très bonne fonctionnalité avancée de Drupal

  3. Abhishek says:

    Thanks guys. Sure ! Vidhatanand, i am working on some modules let them complete. I will definitely post on the blog.

Leave a Reply

CommentLuv Enabled

Notify me of followup comments via e-mail. You can also subscribe without commenting.