Problem/Motivation

The annotation/instructions in the PhoneInternationalElement class for using a field in a custom form are incorrect.

 * @code
 * $form['phone'] = [
 *   '#type' => 'phone_international',
 *   '#title' => $this->t('International Phone'),
 *   '#attributes' => [
 *      'data-country' => 'PT',
 *      'data-geo' => 0, // 0(Disable) or 1(Enable)
 *      'data-exclude' => [],
 *      'data-only' => [],
 *      'data-preferred' => ['PT']
 *   ],
 * ];
 * @endcode

The settings for the field can not be added as attributes because they get overwritten in the processInternationalPhone method

Proposed resolution

Better document how to use the field with form api in a custom field, which is to use the array keys #countries, #exclude_countries, #country i.e.

* @code
 * $form['phone'] = [
 *   '#type' => 'phone_international',
 *   '#title' => $this->t('International Phone'),
 *   '#countries' => 'all',
 *   '#country' => 'uk'
 * ];
 * @endcode
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Joe Huggans created an issue. See original summary.

niharika.s’s picture

Status: Active » Needs review
StatusFileSize
new1.5 KB

I have applied the changes please review it .

joe huggans’s picture

@nikarika.s - I don't think this works, because the JS in the module looks for these data attributes, so it would break the field to remove them.

The only thing needed here is to update the annotation.

tim_dj’s picture

@niharika.s you are right the instructions are wrong but you should not change the code but the instructions in the annotations on line 31.

These should not include "data-" and for preferred country it should be: #preferred_countries

joe huggans’s picture

I actually opened this issue myself @tim_dj, I've provided an MR already

tim_dj’s picture

@joe huggans

Looks good but missing some properties. Looking at code you will have to add the following as well:
#countries
#exclude_countries
#geolocation
#preferred_countries

tim_dj’s picture

Status: Needs review » Needs work
joe huggans’s picture

@tim_dj

Thanks for the reply, maybe we should add those properties in a separate code example because above the one in question it states "If you want a default country you need to do this:".

But as far as I am aware, the following are not required in order to set a default_country?

#countries
#exclude_countries
#geolocation
#preferred_countries

joe huggans’s picture

I've made another edit to the annotation, let me know if I can make any improvements. Thanks

tim_dj’s picture

@joe huggans

Looks good I noticed one more thing though. I think by default geolocation is disabled. See PhoneInternationalElement.php:101

joe huggans’s picture

Status: Needs work » Needs review

@tim_dj

good call, I've made a change and also fixed some phpcs issues.

tim_dj’s picture

Comment still says by default geolocation is enabled.
See PhoneInternationalElement.php:15

tim_dj’s picture

Status: Needs review » Needs work
joe huggans’s picture

Status: Needs work » Needs review
tim_dj’s picture

Status: Needs review » Reviewed & tested by the community

Looks good

saidatom’s picture

Version: 3.3.1 » 4.x-dev
Status: Reviewed & tested by the community » Fixed

saidatom’s picture

saidatom’s picture

Status: Fixed » Closed (fixed)

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