'Example element demo', 'page callback' => 'drupal_get_form', 'page arguments' => array('example_element_demo_form'), 'access arguments' => array('access content'), ); return $items; } /** * Implementation of hook_elements(). */ function example_element_elements() { $type['phonenumber'] = array( '#input' => TRUE, '#process' => array('example_element_phonenumber_expand'), '#element_validate' => array('example_element_phonenumber_validate'), '#default_value' => array('areacode' => '', 'number' => '', 'extension' => ''), ); return $type; } /** * Our process callback to expand the control. */ function example_element_phonenumber_expand($element) { $element['#tree'] = TRUE; if (!isset($element['#value'])) { $element['#value'] = array('areacode' => '', 'number' => '', 'extension' => ''); } $element['areacode'] = array( '#type' => 'textfield', '#size' => 3, '#maxlength' => 3, '#value' => $element['#value']['areacode'], '#prefix' => '(', '#suffix' => ')', ); $element['number'] = array( '#type' => 'textfield', '#size' => 8, '#maxlength' => 8, '#required' => TRUE, '#value' => $element['#value']['number'], ); $element['extension'] = array( '#type' => 'textfield', '#size' => 10, '#maxlength' => 10, '#prefix' => t('ext'), '#value' => $element['#value']['extension'], ); return $element; } /** * Our element's validation function. * * We check that: * - the area code is a three digit number * - the number is numeric, with an optional dash * * Any problems are attached to the form element using form_error(). */ function example_element_phonenumber_validate($form, &$form_state) { if (isset($form_state['values']['areacode'])) { if (0 == preg_match('/^\d{3}$/', $form_state['values']['areacode'])) { form_error($form['areacode'], t('The areacode is invalid.')); } } if (isset($form_state['values']['number'])) { if (0 == preg_match('/^\d{3}-?\d{4}$/', $form_state['values']['number'])) { form_error($form['number'], t('The number is invalid.')); } } return $form; } /** * Theme function to format the output. * * We use the container-inline class so that all three of the HTML elements * are placed next to each other, rather than on separate lines. */ function theme_phonenumber($element) { var_dump($element); return theme('form_element', $element, '
'. $element['#children'] .'
'); } /** * This is a simple form to demonstrate how to use the phonenumber form * element we defined. */ function example_element_demo_form() { $form['test'] = array( '#type' => 'item', '#value' => 'well this shows up at least...', ); $form['example_element_test_1'] = array( '#type' => 'phonenumber', '#title' => t('Phone number 1'), '#default_value' => variable_get('example_element_test_1', array('areacode' => '123', 'number' => '456-7890', 'extension' => '') ), '#description' => t('A phone number.'), ); $form['example_element_test_2'] = array( '#type' => 'phonenumber', '#title' => t('Phone number 2'), '#default_value' => variable_get('example_element_test_2', array('areacode' => '', 'number' => '456-7890', 'extension' => '23') ), '#description' => t('Another phone number, a fax perhaps?'), ); return system_settings_form($form); }