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.
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | core_7x-implode_argument-3310364-5.patch | 692 bytes | euk |
Comments
Comment #2
cilefen commentedComment #3
jaykumar95Drupal 7.92
PHP 8.0, 8.1
I have tested this, working fine for me no error on writing comment.
Comment #4
jacob.herrera commentedI 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.
Comment #5
euk commentedThis 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:Comment #6
euk commentedI 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.Comment #7
poker10 commented@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
classesinstead 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()
Comment #8
euk commented@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.
Comment #9
orjantorang commented@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).