I was going to include my patch to the phone.module file too, but it would be far better if the module included the code submitted here: http://drupal.org/node/175899 What would be nicer is if the phone.module file just looked at what was in the local directory and automatically created the sections for each include file.

CommentFileSizeAuthor
#7 phone.uk_.inc_.txt4.8 KBUlti
phone.uk_.inc_.txt4.72 KBUlti

Comments

thierry_gd’s picture

Status: Needs review » Fixed

Patch taken into account

Zoologico’s picture

I think I got UK phones working with this file and this new version of the phone module I modified:

<?php
// $Id: phone.module,v 1.7 2007/09/17 23:21:19 thierrygd Exp $

// Copyright 2007 Thierry GUEGAN http://www.arvoriad.com

/**
* @file
* Defines phone number fields for CCK.
* Provide some verifications on the phone numbers
*/

/**
* Implementation of hook_field_info().
*/
function phone_field_info() {
return array(
'uk_phone' => array('label' => t('UK Phone Numbers')),
'fr_phone' => array('label' => t('French Phone Numbers')),
'it_phone' => array('label' => t('Italian Phone Numbers')),
'ca_phone' => array('label' => t('US & Canadian Phone Numbers')),
);
}

/**
* Implementation of hook_field_settings().
*/
function phone_field_settings($op, $field) {
switch ($op) {
case 'form':
$form = array();
$form['phone_country_code'] = array(
'#type' => 'checkbox',
'#title' => t('Add the country code if not filled by the user'),
'#default_value' => isset($field['phone_country_code']) ? $field['phone_country_code'] : '',
);
return $form;

case 'save':
return array('phone_country_code');

case 'database columns':
if ($field['type'] == 'fr_phone'
|| $field['type'] == 'it_phone'
|| $field['type'] == 'ca_phone'
|| $field['type'] == 'uk_phone'){
$columns = array(
'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE),
);
}
return $columns;
}
}

/**
* Implementation of hook_field().
*/
function phone_field($op, &$node, $field, &$node_field, $teaser, $page) {
switch ($op) {
case 'view':
foreach ($node_field as $delta => $item) {
$node_field[$delta]['view'] = content_format($field, $item, 'default', $node);
}
return theme('field', $node, $field, $node_field, $teaser, $page);
}
}

/**
* Implementation of hook_field_view_item().
*
*/
/*
function phone_field_view_item($field, $node_field_item) {
$phonenumber = check_plain($node_field_item['value']);
return $phonenumber;
}
*/

/**
*Implementation of hook_field_formatter_info
*/
function phone_field_formatter_info() {
return array(
'default' => array(
'label' => 'Default',
'field types' => array('uk_phone', 'fr_phone', 'it_phone', 'ca_phone'),
),
);
}

/**
* Implementation of hook_field_formatter().
**/
function phone_field_formatter($field, $item, $formatter, $node) {
if (!isset($item['value'])) {
return '';
}
if ($field['text_processing']) {
$text = check_markup($item['value'], $item['format'], is_null($node) || isset($node->in_preview));
}
else {
$text = check_plain($item['value']);
}
// iPhone Support
if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== FALSE) {
$text = '' . $text . '';
}
return $text;
}

/**
* Implementation of hook_widget_info().
*/
function phone_widget_info() {
return array(
'phone' => array(
'label' => t('Textfield'),
'field types' => array('uk_phone', 'fr_phone', 'it_phone', 'ca_phone'),
),
);
}

/**
* Implementation of hook_widget_settings().
*/
function phone_widget_settings($op, $widget) {
switch ($op) {
case 'form':

case 'validate':
break; //do nothing

case 'save':
return array();
}
}

/**
* Implementation of hook_widget().
*/
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'] == 'uk_phone') {
$node_field[0]['value'] = format_phone_number('uk', $node_field[0]['value'], $field);
}
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);
}
}
}
}
break;

case 'validate':
if (is_array($node_field)) {
foreach ($node_field as $delta => $item) {

if ($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 UK phone number
', array('%value' => $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
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
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
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'])));
}
}
}
}
break;
}
}

/**
* Verification for Phone Numbers.
*
* @param string $countrycode
* @param string $phonenumber
* @return boolean Returns boolean FALSE if the phone number is not valid.
*/
function valid_phone_number($countrycode, $phonenumber) {

$countrycode = trim($countrycode);
$phonenumber = trim($phonenumber);

if ( $countrycode == 'uk'
|| $countrycode == 'fr'
|| $countrycode == 'it'
|| $countrycode == 'ca') {

//drupal_set_message('langue = ' . $countrycode, 'error');

$valid_phone_function = 'valid_'. $countrycode . '_phone_number';
include_once('./'. drupal_get_path('module', 'phone') . '/phone.'. $countrycode . '.inc');

if (function_exists($valid_phone_function)) {
return $valid_phone_function($phonenumber);
}
else {
return false;
}
}
else {
//Country not taken into account yet
return false;
}
}

/**
* Verification for Phone Numbers.
*
* @param string $countrycode
* @param string $phonenumber
* @return boolean Returns boolean FALSE if the phone number is not valid.
*/
function format_phone_number($countrycode, $phonenumber, $field) {

$countrycode = trim($countrycode);
$phonenumber = trim($phonenumber);

if ( $countrycode == 'uk'
|| $countrycode == 'fr'
|| $countrycode == 'it'
|| $countrycode == 'ca') {

//drupal_set_message('langue = ' . $countrycode, 'error');

$format_phone_function = 'format_'. $countrycode . '_phone_number';
include_once('./'. drupal_get_path('module', 'phone') . '/phone.'. $countrycode . '.inc');

if (function_exists($format_phone_function)) {
return $format_phone_function($phonenumber, $field);
}
else {
return false;
}
}
else {
//Country not taken into account yet
return false;
}
}

thierry_gd’s picture

Could you explain the modifications you have done !

Thanks

Zoologico’s picture

I basically went through you .module file and everywhere you had a listing for fr, it, or ca, I added one for uk.

For example:

/**
* Implementation of hook_field_info().
*/
function phone_field_info() {
return array(
'fr_phone' => array('label' => t('French Phone Numbers')),
'it_phone' => array('label' => t('Italian Phone Numbers')),
'ca_phone' => array('label' => t('US & Canadian Phone Numbers')),
);
}

Turned into:

/**
* Implementation of hook_field_info().
*/
function phone_field_info() {
return array(
'uk_phone' => array('label' => t('UK Phone Numbers')),
'fr_phone' => array('label' => t('French Phone Numbers')),
'it_phone' => array('label' => t('Italian Phone Numbers')),
'ca_phone' => array('label' => t('US & Canadian Phone Numbers')),
);
}

I looked for "fr" and "fr_phone" to pinpoint where they should be added.

Finally, since I did not know what the feedback should be (I don't know the UK number formatting) when a user types the wrong number, I left the "%value is not a valid UK number" portion of the message, but removed the specific feedback see example below.

From this:

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
French phone numbers should only contain numbers and spaces and be like 99 99 99 99 99', array('%value' => $item['value'])));

To this:

if ($field['type'] == 'uk_phone' && !valid_phone_number('uk', $item['value'])) {
form_set_error($field['field_name'],t('"%value" is not a valid UK phone number
', array('%value' => $item['value'])));

thierry_gd’s picture

OK, I was thinking you had find problem in the code I uploaded.
I already had modified that file

Zoologico’s picture

Ack. Sorry, had I known I wouldn't have duplicated efforts.
Thanks for your efforts on this module.

Ulti’s picture

StatusFileSize
new4.8 KB

Sorry I left in a debug statement, you will want to remove line #107 "var_export($matches);". I've included the update file.

Many thanks.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

Ulti’s picture

Version: 5.x-1.5 » 5.x-2.9
Category: feature » bug
Status: Closed (fixed) » Active

Hi,

For some reason in the current stable version of this module the uk.inc file has

function valid_fr_phone_number($phonenumber)

as well as the submitted version:

function valid_uk_phone_number($phonenumber) {}

BTW a bit of attribution in the README.txt would be nice for the people who have submitted country includes ;P

thierry_gd’s picture

Status: Active » Fixed

Taken into account in 5.2.11 version

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.