Provide possibility to add custom attributes to phone international element

Created on 7 March 2025, 2 months ago

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.

Feature request
Status

Active

Version

3.3

Component

Code

Created by

🇫🇷France gidel

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024