Problem/Motivation

After updating the phone_international module to version 4.0.0, a "Warning: Undefined array key "#dial_code"" error is thrown on forms that use the phone field.

The full error trace is:

Warning: Undefined array key "#dial_code" in Drupal\phone_international\Element\PhoneInternationalElement::preRenderPhoneInternational() (line 125 of modules/contrib/phone_international/src/Element/PhoneInternationalElement.php).
Drupal\phone_international\Element\PhoneInternationalElement::preRenderPhoneInternational()
call_user_func_array() (Line: 113)
Drupal\Core\Render\Renderer->doTrustedCallback() (Line: 917)
Drupal\Core\Render\Renderer->doCallback() (Line: 441)
Drupal\Core\Render\Renderer->doRender() (Line: 530)
Drupal\Core\Render\Renderer->doRender() (Line: 257)
Drupal\Core\Render\Renderer->render() (Line: 238)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent{closure}() (Line: 668)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 231)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare() (Line: 128)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse() (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray()
call_user_func() (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (Line: 186)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 53)
Asm89\Stack\Cors->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 741)
Drupal\Core\DrupalKernel->handle() (Line: 19)

This regression appears to be a side effect of the new dial-code functionality introduced in the 3bbc571a2e604e52153856e1871e6237186fc58f commit, which was part of issue 3460732.

The issue seems to come from the PhoneInternationalElement class:

1. Line 91: The default setting is defined as '#dial-code' (with hyphen)
2. Line 125: The code tries to access $element['#dial_code'] (with underscore)

This mismatch causes the "Undefined array key" error. The field configuration doesn't include a dial_code setting, so it defaults to the value from getInfo(), but due to the key mismatch, it's not found.

The solution is to fix the inconsistent key naming.

Steps to reproduce

  1. Have a Drupal installation with the phone_international module installed on a version older than 4.0.0.
  2. Ensure there is an existing phone field configured on a content type or form.
  3. Update the phone_international module to version 4.0.0 using Composer.
  4. Navigate to a page that displays the phone field (e.g., a node add form or edit form).
  5. Observe the "Undefined array key" warning.

Proposed resolution

Solve mismatch from "'#dial-code'" to "'#dial_code'" on PhoneInternationalElement.

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

eduardo morales alberti’s picture

Issue summary: View changes

eduardo morales alberti’s picture

Status: Active » Needs review

Ready to review

eduardo morales alberti’s picture

Issue summary: View changes
saidatom’s picture

Status: Needs review » Reviewed & tested by the community

Thank you for catching that detail.

  • saidatom committed 4243ee9f on 4.0.x
    feat: #3543263 Undefined array key "#dial_code"  By: eduardo morales...
saidatom’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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