webform file document field generates php TypeError caused by protected forms module

Created on 10 January 2025, 3 months ago

Problem/Motivation

A file document field (file upload) in Webform 6 generated an internal server error caused by the Protected Forms module:
Exception TypeError: "implode(): Argument #1 ($array) must be of type array, string given"

Using the server error log I traced the error to:
/modules/contrib/protected_forms/protected_forms.module line 69

which is:
$user_input .= ' ' . is_array($value) ? implode(' ', $value) : $value;

Steps to reproduce

Using Drupal 10.4.1, Webform 6.2.9 and Protected Forms 2.0.10.
Created a form using a file document field to upload a document.

Proposed resolution

I fixed the issue by replacing line 69 with:
$user_input .= ' ' . (is_array($value) ? implode(' ', $value) : (string)$value);

Remaining tasks

πŸ“Œ Task
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡³πŸ‡±Netherlands mweirath

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

Merge Requests

Comments & Activities

  • Issue created by @mweirath
  • Merge request !26created mr β†’ (Open) created by Unnamed author
  • Pipeline finished with Success
    3 months ago
    Total: 152s
    #394523
  • πŸ‡§πŸ‡ͺBelgium BramDriesen Belgium πŸ‡§πŸ‡ͺ

    @deepali sardana Your MR introduces code sniff issues. It's not recommended to just blindly copy over what might fix a given issue from the issue summary. Also I don't see any need to wrap it in round brackets. I think the only thing relevant here is the string conversion, which might actually crash if the value is NULL. This might need a fix higher up.

    But given your track record, you probably will not look again at this issue and just continue on the next one.

    Fixing some meta data as I go.

  • πŸ‡¨πŸ‡¦Canada teknocat

    @bramdriesen I just ran into this and reviewed the fix provided by @deepali.

    Firstly, I the only code sniff issue is with the lack of a space between (string) and $value, but that's just a coding standards issue and quick to fix. There's also a sniff error on the elseif, which is meant to be on the next line per Drupal coding standards, but that wasn't a result of the patch.

    Wrapping the turnery in parentheses is required to fix the issue, though I'm not exactly sure why that makes a difference. If there's a concern about $value being null when trying to type it as a string, then the second part of the ternary could have a second turnery instead, to be safe, as such:

    $user_input .= ' ' . (is_array($value) ? implode(' ', $value) : ($value ?: ''));

    So either that, or @deepali's, change works fine to resolve the error.

  • πŸ‡¨πŸ‡¦Canada teknocat

    By the way, casting $value as a string will just result in an empty string if it's null, so that's not an issue.

  • πŸ‡¨πŸ‡¦Canada teknocat

    Ok, so the patch derived from either of the merge requests here won't apply to the current release because it includes changes from other patches that haven't all been merged and added to a release yet.

    So using the 2.0.x-dev branch with the patch from either of these merge requests should do the trick for now and cover you on the other issues that haven't been included in a new release version yet (I think).

    Looking forward to a new release soon so we can do away with the patches.

  • Status changed to Needs work 9 days ago
  • πŸ‡ΊπŸ‡ΈUnited States sclsweb

    I'm posting a patch for 2.0.10 for anyone else who may need it. I didn't want to switch to the dev branch right now.

  • πŸ‡­πŸ‡ΊHungary nagy.balint

    I had the same issue, patch #10 fixed it for me on 2.0.10

Production build 0.71.5 2024