Problem/Motivation

When the module process the international phone to create the int_phone form element, all attributes are defined by the module and it's impossible to use options or attributes provided by the 'phone_international' field type.

Steps to reproduce

For example, if I want to set an attribute data-number-type to my field :

$form['phone'] = [
  '#type' => 'phone_international',
  '#title' => $this->t('International Phone'),
  '#exclude_countries' => ['GB', 'BH', 'US'],
  '#preferred_countries' => ['FR', 'IT'],
  '#geolocation' => 1,
  '#attributes' => [
    'data-number-type' => 'fixed_line',
  ],
];

My attribute data-number-type will be overwritten by the processInternationalPhone() method that set #attributes with predefined values.

    $element['int_phone'] = [
      '#type' => 'tel',
      '#default_value' => $element['#default_value'],
      '#attributes' => [
        'placeholder' => $element['#placeholder'] ?? '',
        'class' => ['phone_international-number'],
        'data-country' => (int) $element['#geolocation'] === 1 ? 'auto' : $element['#country'],
        'data-geo' => (int) $element['#geolocation'],
        'data-preferred' => $element['#preferred_countries'] ? implode("-", $element['#preferred_countries']) : [],
      ],
      '#theme_wrappers' => [],
      '#size' => 30,
      '#maxlength' => 128,
    ];

Proposed resolution

When creating the int_phone form element, add provided attributes by the original form element in the #attributes array.

CommentFileSizeAuthor
#3 3511592-add-custom-attributes-3.patch1.7 KBgidel
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

gidel created an issue. See original summary.

gidel’s picture

Status: Active » Needs review
StatusFileSize
new1.7 KB

Here a patch file from the MR for easier Composer usage.

  • saidatom committed 69f428b0 on 4.x
    Issue #3511592: Provide possibility to add custom attributes to phone...
saidatom’s picture

Version: 3.3.1 » 4.x-dev
Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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