Problem/Motivation

Adding external link or a link with query string to user or main navigation menu throws the twig error below:


Warning: explode() expects parameter 2 to be string, array given in twig_split_filter() (line 785 of vendor/twig/twig/src/Extension/CoreExtension.php). 

Steps to reproduce

Add external or query stringed link (example: https://www.drupal.org/ or /user/login?u=x001) to user or main navigation menu.
Clear the cache and try navigating through the site.

Proposed resolution

I resolved it temporarily by changing line 785 (in vendor/twig/twig/src/Extension/CoreExtension.php) to:


if (\strlen($delimiter) > 0) {
        $value = is_array($value) ? implode(' ', $value) : $value;
        return null === $limit ? explode($delimiter, $value) : explode($delimiter, $value, $limit);
...

CommentFileSizeAuthor
#11 twig_core_3215952.patch811 bytesvisway12

Issue fork drupal-3215952

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Philben created an issue. See original summary.

larowlan’s picture

Category: Bug report » Support request
Status: Active » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

Can you reproduce this with vanilla Drupal?

This sounds like a twig template has an invalid use of the split filter

philben’s picture

Hi Larowlan,
Sorry, I haven't tried that yet.

Version: 9.1.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

cilefen’s picture

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

I am closing this support request because there have been no recent comments.

alberto56’s picture

I have some steps to reproduce, as well as a workaround.

This can happen if

* Your node has a field field_xyz which accepts values such as "", "something", "something,something else"
* Your theme has its custom implementation of node.html.twig
* In your theme, you want to explode the value of field_xyz at the comma (","), so that the value "" will become [""], the value "something" will become ["something"], and the field "something,something else" will become ["something", "something else"]
* You upgrade from PHP 7 to PHP 8

Your theme implementation might look like this:

...
{% for item in node.field_xyz.value|split(',') %}

{{ item }}

{% endfor %}
...

If you are using PHP 7, you will see the following in your markup:

* for "": ""
* for "something": "

something
"
* for "something,something else": "
something
something else
"

In PHP 7, a PHP warning will be logged:

Warning: explode() expects parameter 2 to be string, array given in twig_split_filter() (line 804 of vendor/twig/twig/src/Extension/CoreExtension.php)

In PHP 8, this is an error, not a warning, so your theme will break completely.

At its core, this is because, in twig, "node.field_xyz.value" can be either an array (in case the value is empty) or a string.

You can inspect this by putting this in your template:

{{ node.field_xyz.value|json_encode }}

Notice that the result of this can be either an array or a string. It is therefore important to not pass this directly to split, which expects a string. If you pass a string directly to this, it will result in the warning (for PHP 7) or error (for PHP 8): "explode() expects parameter 2 to be string, array given in twig_split_filter()"

I found the only way that works for me is:

{% set node_field_xyz_value = node.field_xyz.value %}
{% if node.field_xyz.value is iterable %}
  {% set node_field_xyz_value = node.field_xyz.value|join("") %}
{% endif %}
{{ node_field_xyz_value|split(',') }}

See also https://drupal.stackexchange.com/questions/228388/how-do-i-get-the-raw-f...

alberto56’s picture

Status: Closed (outdated) » Fixed

Moving this to fixed based on the above, please change the status if needed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

kreatIL made their first commit to this issue’s fork.

kreatil’s picture

I get exactly the error as described in the OP. The system is running Drupal 9.4.1.
Warning: explode() expects parameter 2 to be string, array given in twig_split_filter() (line 804 of /vendor/twig/twig/src/Extension/CoreExtension.php)
It seems to have something to do with the menu, because when I remove the main menu block, the error disappears. However, I don't have any external links in the menu items, and no queries either.

visway12’s picture

StatusFileSize
new811 bytes

Fixed this issue by applying a custom patch.
PHP: 8.1.0
Drupal: 10.2