Problem/Motivation

In some situations, the #parents key may be empty in a form element, resulting in an error (possibly only in combination with inline form errors). This will now result in a TypeError:

TypeError: implode(): Argument #1 ($array) must be of type array, string given in implode() (line 26 of core/lib/Drupal/Core/Form/FormElementHelper.php).

Steps to reproduce

At least one situation where this happens can be triggered using config overrides:

  1. Enable inline form errors (unsure if this is required at this point)
  2. Override a setting from system.site in settings.php, e.g. $config['system.site']['name'] = 'Overridden site name';
  3. Visit /admin/config/system/site-information
  4. Notice the overridden message at the top, informing you of the overridden value
  5. Make a change to the form and save
  6. Notice the error

Proposed resolution

TBD.

Remaining tasks

  • Debug
  • Propose resolution
  • Fix

User interface changes

None.

API changes

None.

Data model changes

None.

Original report

Seeing this Notice when saving a node edit form:

Notice: Undefined index: #parents in /app/core/lib/Drupal/Core/Form/FormElementHelper.php.

Comments

brooke_heaton created an issue. See original summary.

brooke_heaton’s picture

Title: Notice: Undefined index: #parents in a Form/FormElementHelper.php o » Notice: Undefined index: #parents in a Form/FormElementHelper.php
brooke_heaton’s picture

Attached patch adds isset check on #parents.

cilefen’s picture

Status: Active » Needs review

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative, +Needs steps to reproduce

This will require an issue summary update.

I have not seen this error when saying a node so need steps to reproduce.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

s_leu’s picture

Status: Postponed (maintainer needs more info) » Needs review

The problem this patch is addressing was throwing only notices on PHP 7.4 but is throwing errors on PHP 8+, so the patch is valid I think. Steps to reproduce this using the focal_point module:

On Vanilla Drupal 9.5+

  • Enable the modules media, media_library and focal_point
  • Add an image style with a focal_point image effect
  • Configure the media_library form display of image media to use the Image (Focal point) field widget
  • Add a media field to the article content type
  • Configure the form display of the article content type to use the media_library widget
  • Add an article and add a media to the article by uploading a new image
  • After uploading the image, do not enter an alt text and click save

There will be an ajax error originating from the location in code the patch here changes.

smustgrave’s picture

Status: Needs review » Needs work

If valid it will need a test case to show the issue.

Though not sure that’s a fix that would get committed. This may be covering a symptom of a bigger issue. Like why is that value not set when it gets to this point?

But either way tests will 100% be needed

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

hargurpreet’s picture

Updated patch #3 to fix a notice in core/lib/Drupal/Core/Form/FormState.php on line 1174

pixlkat’s picture

I also was seeing this error, but with different form elements. We have a taxonomy entity reference field with unlimited cardinality, and we are using inline entity form to add an existing term. We are also using Inline Form Errors to display error messages inline.

When submitting the IEF form with an error condition, it appears that the `actions` element was the form element causing the error. This issue was not present when using IEF to add a new entity.

Applying the patch in #14 fixed the error in our case.

dahousecat’s picture

I was getting this error on /admin/config/development/performance with $key config_override_status_messages which comes from Drupal\Core\Form\ConfigFormBase.

Patch #14 applied and resolved the issue for me.

devdits’s picture

We did have create a small patch to fix the problem:

diff --git a/core/lib/Drupal/Core/Form/FormElementHelper.php b/core/lib/Drupal/Core/Form/FormElementHelper.php
--- a/core/lib/Drupal/Core/Form/FormElementHelper.php
+++ b/core/lib/Drupal/Core/Form/FormElementHelper.php
@@ -23,7 +23,7 @@
    */
   public static function getElementByName($name, array $form) {
     foreach (Element::children($form) as $key) {
-      if (implode('][', $form[$key]['#parents']) === $name) {
+      if (implode('][', $form[$key]['#parents'] ?? []) === $name) {
         return $form[$key];
       }
       elseif ($element = static::getElementByName($name, $form[$key])) {

But it's doing almost the same as #14.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

driskell’s picture

I had this same problem with config overrides key as well on people account settings area.
So not sure if possible there's a reproduction case there with config overrides applying. Specifically for me it was relaying to symfony_mailer module I think that brings in some overrides there, and then when saving that form it now crashes in 11.1

eelkeblok’s picture

Title: Notice: Undefined index: #parents in a Form/FormElementHelper.php » Empty #parents in a Form/FormElementHelper.php
Priority: Normal » Major
Issue summary: View changes

Looks like this may be happening in different situations, so the risk is that we'll be solving one reason #parents is not set, and there may be others. Also, it looks like this is now an error in later PHP versions and will be reported like this:

[Sorry, apparently forgot to paste the error, or maybe it got filtered out somehow. Either way, it has now been lost to the depths of time...].

eelkeblok’s picture

Title: Empty #parents in a Form/FormElementHelper.php » Empty #parents in Form/FormElementHelper.php
dabbor’s picture

We ran into the same problem while using Paragraphs and Media which always gives us following Warning and Error in the exact order:
- Warning: Undefined array key "#parents"
- TypeError: implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given in implode()

Here are our replication steps:

  1. Create or edit content with paragraphs field.
  2. Add paragraph that contains media field.
  3. Click “Add media” (or so) to add media on the media field/
  4. In the media popup click on the “Choose file” (under the Add file).
  5. Choose a file (image).
  6. Leave the “Alternative text” (required field) empty and click “Save”.
  7. The validation error is not displayed and you can see 500 error on the AJAX request.
  8. The warning and error appears in Recent Log messages.