Problem/Motivation

After migration from PHP 7.4 to PHP 8.0, and trying to write a Comment, we get this error:
TypeError: implode(): Argument #1 ($pieces) must be of type array, string given in implode() (line 1104 of /DRUPAL_FOLDER/modules/field/field.module).

Steps to reproduce

Just try to create a comment.

Proposed resolution

Change from this:
$variables['classes'] = implode(' ', $variables['classes_array']);
to this:
if(is_array($variables['classes_array']))
{
$variables['classes'] = implode(' ', $variables['classes_array']);
}
else if (is_string($variables['classes_array']))
{
$variables['classes'] = implode(' ', (array)$variables['classes_array']);
}
Maybe there is a better way to do this.

CommentFileSizeAuthor
#5 core_7x-implode_argument-3310364-5.patch692 byteseuk

Comments

JOINSO created an issue. See original summary.

cilefen’s picture

Issue tags: -#php80 +PHP 8.0
jaykumar95’s picture

Status: Active » Postponed (maintainer needs more info)

Drupal 7.92
PHP 8.0, 8.1

I have tested this, working fine for me no error on writing comment.

jacob.herrera’s picture

I got this same error while debugging a client site still on D7, but for me this happened in /includes/menu.inc. Will update as I debug.

euk’s picture

StatusFileSize
new692 bytes

This happens when you try to implode() a variable which is not set. PHP 7.x would display a notice, and the results of the call would be NULL, while in PHP 8.x it is a fatal error.

I found this issue in /includes/menu.inc, and the following patch helps:

euk’s picture

I believe the issue is the same for the all the places where implode() is used with an unset variable, which might be a lot of places.

poker10’s picture

@euk Can you please explain, how is this patch relevant to the error mentioned in the issue summary? I see that the problem mentioned in the summary is caused by some contrib module / theme passing a string to the classes instead of an array (which then affects template variables).

Your patch is making changes in the menu module, which seem unrelated to me. Probably the correct issue for the patch is this issue: #951098: tab_root_map and tab_parent_map notices undefined in _menu_translate()

euk’s picture

@poker10 - you are right, the patch might not be for this issue (posted in response to #4).
I believe the issue itself is the same though - the unset variable.

orjantorang’s picture

@euk thanks, I had the same issue in menu.inc, your patch worked for me as well. The default frontpage view from Views module crashed on:
TypeError: implode(): Argument #1 ($pieces) must be of type array, string given i implode() (rad 798 av .../includes/menu.inc).

Status: Postponed (maintainer needs more info) » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.