You are here

function phone_widget in Phone 5

Same name and namespace in other branches
  1. 6 phone.module \phone_widget()

Implementation of hook_widget().

File

./phone.module, line 142
Defines phone number fields for CCK. Provide some verifications on the phone numbers

Code

function phone_widget($op, &$node, $field, &$node_field) {
  switch ($op) {
    case 'form':
      $form = array();
      $form[$field['field_name']] = array(
        '#tree' => TRUE,
      );
      if ($field['multiple']) {
        $form[$field['field_name']]['#type'] = 'fieldset';
        $form[$field['field_name']]['#title'] = t($field['widget']['label']);
        foreach (range(0, 2) as $delta) {
          $form[$field['field_name']][$delta]['value'] = array(
            '#type' => 'textfield',
            '#title' => '',
            '#default_value' => isset($node_field[$delta]['value']) ? $node_field[$delta]['value'] : '',
            '#required' => $field['required'] ? $field['required'] : FALSE,
            '#maxlength' => 255,
            '#weight' => $field['widget']['weight'],
            '#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 20,
            '#description' => $field['widget']['description'],
          );
        }
      }
      else {
        $form[$field['field_name']][0]['value'] = array(
          '#type' => 'textfield',
          '#title' => $field['widget']['label'],
          '#default_value' => isset($node_field[0]['value']) ? $node_field[0]['value'] : '',
          '#required' => $field['required'] ? $field['required'] : FALSE,
          '#maxlength' => 255,
          '#weight' => $field['widget']['weight'],
          '#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 20,
          '#description' => $field['widget']['description'],
        );
      }
      return $form;
    case 'process form values':
      if (is_array($node_field)) {
        foreach ($node_field as $delta => $item) {

          //format the phone number
          if ($item['value'] != '') {
            if ($field['type'] == 'fr_phone') {
              $node_field[0]['value'] = format_phone_number('fr', $node_field[0]['value'], $field);
            }
            if ($field['type'] == 'it_phone') {
              $node_field[0]['value'] = format_phone_number('it', $node_field[0]['value'], $field);
            }
            if ($field['type'] == 'ca_phone') {
              $node_field[0]['value'] = format_phone_number('ca', $node_field[0]['value'], $field);
            }
            if ($field['type'] == 'uk_phone') {
              $node_field[0]['value'] = format_phone_number('uk', $node_field[0]['value'], $field);
            }
            if ($field['type'] == 'ru_phone') {
              $node_field[0]['value'] = format_phone_number('ru', $node_field[0]['value'], $field);
            }
          }
        }
      }
      break;
    case 'validate':
      if (is_array($node_field)) {
        foreach ($node_field as $delta => $item) {
          if ($item['value'] != '') {
            if ($field['type'] == 'fr_phone' && !valid_phone_number('fr', $item['value'])) {
              form_set_error($field['field_name'], t('"%value" is not a valid French phone number<br>French phone numbers should only contain numbers and spaces and be like 99 99 99 99 99', array(
                '%value' => $item['value'],
              )));
            }
            if ($field['type'] == 'it_phone' && !valid_phone_number('it', $item['value'])) {
              form_set_error($field['field_name'], t('"%value" is not a valid Italian phone number<br>Italian phone numbers should only ...', array(
                '%value' => $item['value'],
              )));
            }
            if ($field['type'] == 'ca_phone' && !valid_phone_number('ca', $item['value'])) {
              form_set_error($field['field_name'], t('"%value" is not a valid North American phone number<br>North American Phone numbers should only contain numbers and + and - and ( and ) and spaces and be like 999-999-9999. Please enter a valid ten-digit phone number with optional extension.', array(
                '%value' => $item['value'],
              )));
            }
            if ($field['type'] == 'uk_phone' && !valid_phone_number('uk', $item['value'])) {
              form_set_error($field['field_name'], t('"%value" is not a valid British phone number<br>British Phone numbers should .... ', array(
                '%value' => $item['value'],
              )));
            }
            if ($field['type'] == 'ru_phone' && !valid_phone_number('ru', $item['value'])) {
              form_set_error($field['field_name'], t('"%value" is not a valid Russian phone number<br>British Phone numbers should .... ', array(
                '%value' => $item['value'],
              )));
            }
          }
        }
      }
      break;
  }
}