TypeError: implode(): Argument #1 ($array) must be of type array, string given when FormElementHelper::getElementByName() called with string

Created on 21 August 2025, 20 days ago

Problem/Motivation

In Drupal 10, FormElementHelper::getElementByName() expects an array of parent keys as its first parameter.
The current implementation of _protected_forms_validate() calls this method with a plain string:

$form_element = FormElementHelper::getElementByName($key, $form);

When $key is a string (e.g., submitted, captcha_sid), this triggers a fatal error:

TypeError: implode(): Argument #1 ($array) must be of type array, string given
in FormElementHelper.php on line 26

This issue occurs on Webforms and other forms with nested values (e.g., $values['submitted']['first_name']).
As a result, anonymous submissions protected by this module can break completely.

Steps to reproduce

  1. Enable Protected Forms.
  2. Create a basic Webform with textfields and a CAPTCHA element.
  3. Embed the Webform on a content page.
  4. Submit the form anonymously (so validation runs).
  5. Observe in the PHP error log:
TypeError: implode(): Argument #1 ($array) must be of type array, string given
in Drupal\Core\Form\FormElementHelper::getElementByName()

Proposed resolution

Update _protected_forms_validate() to always pass arrays of parents to FormElementHelper::getElementByName().

Example:

$form_element = FormElementHelper::getElementByName([$key], $form);

And for Webform submissions, use:

$form_element = FormElementHelper::getElementByName(['submitted', $child_key], $form);

See also: https://www.drupal.org/project/protected_forms/issues/3498791 ๐Ÿ“Œ webform file document field generates php TypeError caused by protected forms module Active

๐Ÿ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany marcusx

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

Merge Requests

Comments & Activities

  • Issue created by @marcusx
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany marcusx

    Linking related issue...

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia kulpratap2002

    Working on it.

  • Merge request !31Issue #3542381: Fix error. โ†’ (Open) created by Unnamed author
  • Pipeline finished with Success
    14 days ago
    Total: 267s
    #582693
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia kulpratap2002

    I have solved the error and generated MR.

    The reason $form_element = FormElementHelper::getElementByName([$key], $form); or $form_element = FormElementHelper::getElementByName(['submitted', $child_key], $form); does not work is because getElementByName() in Drupal core only accepts a string as its first argument, not an array.

    Core internally compares this string against the elementโ€™s #parents array, joined with implode('][', โ€ฆ). Passing an array directly causes a PHP type error:
    TypeError: Argument 1 ($name) must be of type string, array given

    The correct approach is to convert any array of parent keys into a string before passing it, like this:

    $form_element = FormElementHelper::getElementByName(implode('][', ['submitted', $child_key]), $form);
    This ensures compatibility with both flat and nested form elements (Webforms, nodes, user profiles, etc.) without touching core.

    Also, I have fixed this Issue ๐Ÿ“Œ webform file document field generates php TypeError caused by protected forms module Active .

    Please review.

    Thanks

Production build 0.71.5 2024