You are here

function phone_field in Phone 6

Same name and namespace in other branches
  1. 5 phone.module \phone_field()

Implementation of hook_field().

Define the behavior of a field type.

Parameters

$op: What kind of action is being performed. Possible values:

  • "load": The node is about to be loaded from the database. This hook should be used to load the field.
  • "validate": The user has just finished editing the node and is trying to preview or submit it. This hook can be used to check or even modify the node. Errors should be set with form_set_error().
  • "presave": The user has just finished editing the node and the node has passed validation. This hook can be used to modify the node.
  • "insert": The node is being created (inserted in the database).
  • "update": The node is being updated.
  • "delete": The node is being deleted.

&$node: The node the action is being performed on. This argument is passed by reference for performance only; do not modify it.

$field: The field the action is being performed on.

&$node_field: The contents of the field in this node. Changes to this variable will be saved back to the node object.

Return value

This varies depending on the operation.

  • The "load" operation should return an object containing extra values to be merged into the node object.
  • The "insert", "update", "delete", "validate", and "presave" operations have no return value.

In most cases, only "validate" operations is relevant ; the rest have default implementations in content_field() that usually suffice.

File

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

Code

function phone_field($op, &$node, $field, &$node_field, $teaser, $page) {
  switch ($op) {
    case 'validate':

      // corresponds to hook phone_widget validate in phone-5.x
      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'] == 'be_phone' && !valid_phone_number('be', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Belgian phone number<br>Belgian phone numbers should ...', 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'] == 'el_phone' && !valid_phone_number('el', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Greek phone number<br>Greek phone numbers should only ...', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'ch_phone' && !valid_phone_number('ch', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Swiss phone number<br>Swiss phone numbers should only contain numbers and spaces and be like 099 999 99 99', 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'] == 'cr_phone' && !valid_phone_number('cr', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Costa Rican phone number!<br>Costa Rican phone numbers should contain only numbers and spaces be like 99 99 99 99 with an optional prefix of "+506" or "00506".', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'pa_phone' && !valid_phone_number('pa', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Panamanian phone number!<br>Panamanian phone numbers should contain only numbers, spaces and dashes be like 9999-999, 9999 999 or 9999999 with an optional prefix of "+507" or "00507".', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'gb_phone' && !valid_phone_number('gb', $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>Russian Phone numbers should .... ', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'ua_phone' && !valid_phone_number('ua', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Ukrainian phone number<br>Ukrainian Phone numbers should .... ', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'es_phone' && !valid_phone_number('es', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Spanish phone number<br>Spanish phone numbers should only contains numbers and spaces and be like 999 999 999', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'au_phone' && !valid_phone_number('au', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Australian phone number<br>Australian phone numbers should contain only numbers with an optional prefix of "+61"', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'cs_phone' && !valid_phone_number('cs', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Czech phone number!<br>Czech phone numbers should contain only numbers and spaces be like 999 999 999 with an optional prefix of "+420" or "00420".', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'hu_phone' && !valid_phone_number('hu', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Hungarian phone number!<br>Hungarian phone numbers should contain only numbers and spaces be like 70 999 9999 with an optional prefix of "+36" or "06".', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'pl_phone' && !valid_phone_number('pl', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Polish phone number!<br>Polish phone numbers should ...', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'nl_phone' && !valid_phone_number('nl', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Dutch phone number!<br>Dutch phone numbers should contain only numbers and spaces and - and be like 099-9999999 with an optional prefix of "+31".', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'se_phone' && !valid_phone_number('se', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Swedish phone number!<br>Swedish phone numbers should ....', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'za_phone' && !valid_phone_number('za', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid South African phone number!<br>South African phone numbers should ....', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'il_phone' && !valid_phone_number('il', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Israeli phone number!<br>Israeli phone numbers should .....', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'nz_phone' && !valid_phone_number('nz', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid New Zealand phone number!<br>New Zealand phone numbers should contain only numbers, spaces, brackets and "-". They should look like 04 123 4567 or can optionally omit the leading 0 and have a prefix of "+64".', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'br_phone' && !valid_phone_number('br', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Brazilian phone number!<br>Brazilian phone numbers should contain only numbers and spaces and - and be like 099 9999-9999, 99 9999-9999 or 99 99999-9999 with an optional prefix of "+55".', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'cl_phone' && !valid_phone_number('cl', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Chilian phone number!<br>Chilean phone numbers should ...', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'cn_phone' && !valid_phone_number('cn', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Chinese phone number!<br>Chinese phone numbers should ...', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'hk_phone' && !valid_phone_number('hk', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Hong Kong phone number!<br>Hong Kong phone numbers should contain only numbers and spaces and be like 9999 9999 with an optional prefix of "+852" or "852".', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'mo_phone' && !valid_phone_number('mo', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Macau phone number!<br>Macau phone numbers should contain only numbers and spaces and be like 9999 9999 with an optional prefix of "+853" or "853".', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'ph_phone' && !valid_phone_number('ph', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Philippine phone number!<br>Example of valid Philippine phone numbers: +63 (2) 123-4567 or +63 (2) 123-4567 loc. 123	or mobile +63 (919) 123-4567', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'sg_phone' && !valid_phone_number('sg', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Singaporean phone number!<br>Singaporean phone numbers should ....', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'jo_phone' && !valid_phone_number('jo', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Jordanian phone number!<br>Please check the spaces and dashes in your number ...', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'eg_phone' && !valid_phone_number('eg', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Egyptian phone number!<br>Egyptian hone numbers should have the format +20 (#[#]) 1234567[8], where the digts between ( ) is the area or network code, without a leading zero; and digits between [ ] are optional', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'pk_phone' && !valid_phone_number('pk', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid Pakistanese phone number!<br>Pakistanese phone numbers should ...', array(
              '%value' => $item['value'],
            )));
          }
          if ($field['type'] == 'int_phone' && !valid_phone_number('int', $item['value'])) {
            form_set_error($field['field_name'], t('"%value" is not a valid international phone number<br>International phone numbers should contain a country code prefixed by a plus sign followed by the local number.', array(
              '%value' => $item['value'],
            )));
          }
        }
      }
      break;
    case 'presave':

      // corresponds to hook phone_widget 'process form values' in phone-5.x
      foreach ($node_field as $delta => $item) {

        //format the phone number
        if ($item['value'] != '') {
          if ($field['type'] == 'fr_phone') {
            $node_field[$delta]['value'] = format_phone_number('fr', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'be_phone') {
            $node_field[$delta]['value'] = format_phone_number('be', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'it_phone') {
            $node_field[$delta]['value'] = format_phone_number('it', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'el_phone') {
            $node_field[$delta]['value'] = format_phone_number('el', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'ch_phone') {
            $node_field[$delta]['value'] = format_phone_number('ch', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'ca_phone') {
            $node_field[$delta]['value'] = format_phone_number('ca', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'cr_phone') {
            $node_field[$delta]['value'] = format_phone_number('cr', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'pa_phone') {
            $node_field[$delta]['value'] = format_phone_number('pa', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'gb_phone') {
            $node_field[$delta]['value'] = format_phone_number('gb', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'ru_phone') {
            $node_field[$delta]['value'] = format_phone_number('ru', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'ua_phone') {
            $node_field[$delta]['value'] = format_phone_number('ua', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'es_phone') {
            $node_field[$delta]['value'] = format_phone_number('es', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'au_phone') {
            $node_field[$delta]['value'] = format_phone_number('au', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'cs_phone') {
            $node_field[$delta]['value'] = format_phone_number('cs', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'hu_phone') {
            $node_field[$delta]['value'] = format_phone_number('hu', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'pl_phone') {
            $node_field[$delta]['value'] = format_phone_number('hu', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'nl_phone') {
            $node_field[$delta]['value'] = format_phone_number('nl', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'se_phone') {
            $node_field[$delta]['value'] = format_phone_number('se', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'za_phone') {
            $node_field[$delta]['value'] = format_phone_number('za', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'il_phone') {
            $node_field[$delta]['value'] = format_phone_number('il', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'nz_phone') {
            $node_field[$delta]['value'] = format_phone_number('nz', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'br_phone') {
            $node_field[$delta]['value'] = format_phone_number('br', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'cl_phone') {
            $node_field[$delta]['value'] = format_phone_number('cl', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'cn_phone') {
            $node_field[$delta]['value'] = format_phone_number('cn', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'hk_phone') {
            $node_field[$delta]['value'] = format_phone_number('hk', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'mo_phone') {
            $node_field[$delta]['value'] = format_phone_number('mo', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'ph_phone') {
            $node_field[$delta]['value'] = format_phone_number('ph', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'sg_phone') {
            $node_field[$delta]['value'] = format_phone_number('sg', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'jo_phone') {
            $node_field[$delta]['value'] = format_phone_number('jo', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'eg_phone') {
            $node_field[$delta]['value'] = format_phone_number('eg', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'pk_phone') {
            $node_field[$delta]['value'] = format_phone_number('pk', $node_field[$delta]['value'], $field);
          }
          if ($field['type'] == 'int_phone') {
            $node_field[$delta]['value'] = format_phone_number('int', $node_field[$delta]['value'], $field);
          }
        }
      }
      break;
  }
}