Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This is a real problem. We should not be modifying data on input. All formatting should only happen on output to preserve the integrity of submitted data. As you can see at issue #8436095 (see related issues), this is resulting in Swedish phone numbers being destroyed on input. Can anyone explain the rationale for having done this in phone_field_presave()? If there's not a really good reason for that, we should just strip that function out in its entirety and do it in hook_field_prepare_view() instead. I'll take a whack at this later.
Comment | File | Size | Author |
---|---|---|---|
#9 | 2186331-9-phone_field_prepare_view.patch | 3.86 KB | G2 |
#7 | 2186331-7-phone_field_prepare_view.patch | 3.58 KB | G2 |
#6 | 2186331-6-phone_field_prepare_view.patch | 3.61 KB | G2 |
#3 | 2186331-3-phone_field_prepare_view.patch | 1.04 KB | roderik |
#2 | 2186331-phone_field_prepare_view.patch | 1.1 KB | seanr |
Comments
Comment #1
seanrComment #2
seanrHere's the patch for this.
Comment #3
roderikOh yes, definitely!
But you're getting your $ids and field deltas confused.
Also, format_phone_number() should get the instance settings passed, not the field settings.
Comment #4
roderikActually, it's strange to have formatting of the phone number depend on field instance settings.
1)
It would be more logical if there was a field formatter with the 'phone_country_code' ("Add the country code if not filled by the user") setting, and that setting was deleted from the instance itself.
(This would also open up the way for other formatter options, like for instance: "use spaces instead of dashes".)
2)
...'unfortunately', we also have the 'phone_default_country_code' setting for fields with settings['country'] == 'int'. Right now, I am divided if we should keep using this setting on presave.
Opinions?
Comment #5
roderikThinking about my comment #2 more: I guess we really should not changing the data before saving it. Also when settings['country'] == 'int' and the input phone number has no country extension, we should not add one.
Why? Because the source for the country code could be external, like a country dropdown. (It isn't at the moment, but it could be... ;) ) If that is the case, we don't want to save the local input number with added country code. Because what should happen then, at the moment the user changes the country dropdown?
So:
Comment #6
G2 CreditAttribution: G2 commentedThis patch does exactly that. Phone_country_code is now a field_formatter_setting, phone_field_presave function is not necessary as format_phone_number is called from hook_field_formatter_view(). Formatter settings are passed to the format function instead of instance settings.
phone_default_country_code is still an instance setting and is used in phone.int.inc, so for now I merged this array element to $display['settings']. There should be a more concise solution for this though.
Comment #7
G2 CreditAttribution: G2 commentedSame patch as in #6, but without unnecessary whitespaces :)
Comment #8
roderikWeehee, it's my FIRST EVER code review using DREditor!
Not in your patch::
function phone_field_info() {
line should be deleted.
Should be 0 by default. It's also the default for the now-deleted instance setting. And I think 0 is the most widely-used setting:
* many people who have country-specific phone fields will not want the country code
* for 'int' fields, the country code is usually already present, so having 0 is fine.
Unnecessary code. (2x)
$instance, not $field, no?
On my install, it gives:
Notice: Undefined index: phone_default_country_code in phone_field_formatter_view().
Pro tip: when developing modules, set error_reporting = E_ALL in your PHP settings. (If you don't want to do that for some of your own sites, I won't blame you ;) but it's necessary for public code.)
Further: I thought about making an update function to move the current instance setting into display settings (for all view modes!)... but don't think it's strictly necessary, if the default setting value is made 0. Up to you.
Comment #9
G2 CreditAttribution: G2 commentedRrright, thank you for your feedback! An update function could be useful as well, but I don't really see how could we modify this kind of data there. Here is the corrected patch until then.
Comment #10
roderikRead the instance data, update it (move the setting from field instance settings to formatter settings; they must both be in the same data structure somewhere) and update the data.
I see there's
field_read_instance()
andfield_update_instance()
for that.I won't test that because I don't need it myself, and upgrade paths are not officially required yet because the module is still in beta.
I will act like you set this to 'needs review' and put the issue to RTBC now, since I tested your changes. If you want to write the update process, you can set this back to Needs Review when uploading your patch.
Comment #11
DamienMcKennaI'm not sure this really fixes the problem? With a field configured to allow an international number it accepts just about any string that has numbers and a few control characters, country code or not. It also doesn't add the plus symbol. Is this what is expected?