Problem/Motivation

If a form widget contains an element that does not have a #name key (for example, a container), it triggers this notice:

Notice: Undefined index: #name
in ConditionalFieldsFormHelper.php line 192

Steps to reproduce

/**
 * @param $form
 */
function mymodule_form_alter(&$form) {
  $form['field_my_field']['widget'][0]['foo'] = [
    '#type' => 'container',
    'bar' => [],
  ];
}

Proposed resolution

ConditionalFieldsFormHelper::getState() should check if the #name key exists.

Remaining tasks

I will submit a patch.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

prudloff created an issue. See original summary.

prudloff’s picture

The attached patch fixes the notice.

andreastkdf’s picture

Had the same notice for a form widget containing an element that does not have a #name key.

I can confirm too that #2 fixes the notice

Thanks!

maddentim’s picture

Status: Needs review » Reviewed & tested by the community

I can also confirm the patch resolves the issue and applies cleanly to the 4.0.0-alpha3 release.

Grimreaper’s picture

Hi, Thanks for the patch!

Liam Morland’s picture

Version: 8.x-1.0-alpha9 » 4.x-dev
dqd’s picture

Status: Reviewed & tested by the community » Needs review

Thanks for the reports and all working on this here! +1

It may solve the issue of the thrown error, but I am not sure if we should leave out ELSE completely here. Now we have IF and ELSE IF and nothing if both are not TRUE. Can somebody confirm that it is harmless and that we do not need a silent exception error or anything else to notify? Or ion other words: do we need #name? If not, why we asking for it then?

Otherwise if somebody can clarify this for me I do not see reasons not to commit. I would commit/merge it asap after another review of my question.

Amstercad’s picture

In preparation for Drupal 11, please note the test failure of the current patch:

PHP 8.3 & MySQL 8, D10.2 Patch Failed to Apply

Drupal 11 requires PHP 8.3.