Hello

I'm quite new to drupal so bare with me. In Sweden we have one area code with only "08" all the others are 3-4 digits, but "Phone" thinks that the swedish 08 should be removed. How can I change the code do allow 2-digit area codes (or just the 08)

example: if I specify a number like this +46 8 #### #### Phone just removes the 8.

I guess i should do something in phone.se.inc?

Comments

morelight’s picture

example 2: If i have specified +46 as default and someone types their phonenumber like: 08-123 123 it gets converted to +46 123 123

mgifford’s picture

How is this issue is different from #1605128: Austrian and Romanian phone number missing other than that this is a different country? Any reason not to mark this a duplicate? Figure out how to fix it and roll it out on all regions?

seanr’s picture

Title: Swedish area code "08" gets striped totally » format_int_phone_number() fails to use the appropriate country formatter and strips bits out of valid Swedish phone numbers
Component: Miscellaneous » Code
Category: Support request » Bug report
Issue summary: View changes
Priority: Normal » Major

mgifford, that issue is quite clearly unrelated if you read the two threads. That one is rejecting numbers outright, this one is literally stripping out part of the number while still accepting the submission.

At any rate, what is happening here is if you set the field settings to accept any international number, the module doesn't use the detected country code of the number in phone_field_presave() but instead uses in, and then formats the number before saving (WHY? we should ALWAYS preserve input date and format only on output!). That formats using int instead of the actual country, hitting format_int_phone_number() which does this:

  // $bits[0] is the country code WITH a plus sign.
  if (isset($bits[1])) {
    // This is the first non-CC segment, so it could have the NDN.
    switch ($bits[1][0]) {
      case 0:
        $bits[1] = substr($bits[1], 1); // This blows away our parentheses
        break;
    }
    switch ($bits[1]) {
      case 0:
      case 7:
      case 8:
        array_splice($bits, 1, 1); // This blows away the 8
        break;
    }
  }
  return implode(' ', $bits);
}

This strikes me as very arbitrary and a bad idea. First off, let's preserve input data and only format on output. Second, why aren't we letting the individual country formatters handle the formatting? If it's a question of showing vs not showing the country codes, I get that, but then let's pull off the country code in format_int_phone_number(), then pass the rest of the number to its correct formatter, then re-attach and send to the browser.

So there are two issues here obviously:

  1. Formatting on input rather than output
  2. Not using the correct country's formatter and doing weird stuff with int numbers to detect an NDN

#1 can go in a separate issue, I suppose (which I will do), but #2 is definitely the root cause of the specific bug in this thread and we should address that here. I'll work on a patch today.

Marking as Major since this is violating data integrity (due in part to #1).

mgifford’s picture

Thanks for the clarification @seanr

seanr’s picture

Status: Active » Needs review
StatusFileSize
new935 bytes

This patch adds a check to see if the country code is supported and if so, runs it through the appropriate formatter function.

roderik’s picture

So... the existing code just strips area codes 00 AND 7 / 8 / 07 / 08 at a whim, eh?

Judging this http://www.allareacodes.com/international_dialing_codes.htm, this is only for countries inside the '7' dialing area (ru), which will never execute that code - because there's a .ru.inc file.

So, shall we get rid of the arbitrary stuff at the same time?

roderik’s picture

One note, though: you must not apply this patch unless you already applied #2186331: phone_field_presave() calls format_phone_number(), breaking input data integrity. Otherwise your phone numbers may be saved without a country code! (Depending on your field-instance settings.)

klokie’s picture

The regular expression wasn't working for any Swedish phone numbers that I tried, so I simplified and replaced it. Comments welcome.