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
- Have a Drupal installation with the phone_international module installed on a version older than 4.0.0.
- Ensure there is an existing phone field configured on a content type or form.
- Update the phone_international module to version 4.0.0 using Composer.
- Navigate to a page that displays the phone field (e.g., a node add form or edit form).
- Observe the "Undefined array key" warning.
Proposed resolution
Solve mismatch from "'#dial-code'" to "'#dial_code'" on PhoneInternationalElement.
Issue fork phone_international-3543263
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:
- 3543263-undefined-array-key
changes, plain diff MR !25
Comments
Comment #2
eduardo morales albertiComment #4
eduardo morales albertiReady to review
Comment #5
eduardo morales albertiComment #6
saidatomThank you for catching that detail.
Comment #8
saidatom