I want to add a phone number to my CCK content type, so naturally, I downloaded the Phone (CCK) module. However, this seems to always format the number with a leading one (in the United States) which I don't want. E.g.:

1 555-123-4567

I would prefer the number to be formatted like so:

(555) 123-4567

Should I not use the Phone module and use something else instead (a text field perhaps?)? How can I do this?

Comments

David Stosik’s picture

You could use CCK computed field if you know a little bit of PHP and regular expressions. ;)

userofdrupal’s picture

Is using a computed field how most people do it? How do you do it? In this case I'm the one entering the phone numbers so should I just use a text field?

David Stosik’s picture

I don't need a phone number input...
I just thought using CCK Computed Field could allow you to parse the users' input using some PHP code...
The value is not really computed in this case.

Oghnia’s picture

open phone.ca.inc in your phone module...

at the end of the page you will see the following code

  // construct ten-digit phone number
  $phonenumber = $matches[1] . '-' . $matches[2] . '-' . $matches[3];
  
  if ($matches[1] != "1") {
  	$phonenumber = "1" . " " . $phonenumber; 
  }
  return $phonenumber;
}

replace it by this

  // construct ten-digit phone number
  $phonenumber = '(' . $matches[1] . ') ' . $matches[2] . '-' . $matches[3];
  
// comment this section not needed anymore
/* if ($matches[1] != "1") {
  	$phonenumber = "1" . " " . $phonenumber; 
  }*/
  return $phonenumber;
}

______________________________
next project convert this site to drupal -> http://www.oghnia.com

userofdrupal’s picture

Thanks, I appreciate it. I'll look into it.