<?php
function example_element_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'example_element',
'title' => t('Example element demo'),
'access' => user_access('access content'),
'type' => MENU_NORMAL_ITEM,
'callback' => 'drupal_get_form',
'callback arguments' => array('example_element_demo'),
);
}
return $items;
}
function example_element_elements() {
$type['phonenumber'] = array(
'#input' => TRUE,
'#process' => array('example_element_expand' => array()),
'#validate' => array('example_element_validate' => array()),
'#default_value' => array('areacode' => '', 'number' => '', 'extension' => ''),
);
return $type;
}
function example_element_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;
}
function example_element_validate($form) {
if (isset($form['#value']['areacode'])) {
if (0 == preg_match('/^\d{3}$/', $form['#value']['areacode'])) {
form_error($form['areacode'], t('The areacode is invalid.'));
}
}
if (isset($form['#value']['number'])) {
if (0 == preg_match('/^\d{3}-?\d{4}$/', $form['#value']['number'])) {
form_error($form['number'], t('The number is invalid.'));
}
}
return $form;
}
function theme_phonenumber($element) {
return theme('form_element', $element, '<div class="container-inline">'. $element['#children'] .'</div>');
}
function example_element_demo() {
$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);
}