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);
...
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | twig_core_3215952.patch | 811 bytes | visway12 |
Issue fork drupal-3215952
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
Comment #2
larowlanCan you reproduce this with vanilla Drupal?
This sounds like a twig template has an invalid use of the split filter
Comment #3
philben commentedHi Larowlan,
Sorry, I haven't tried that yet.
Comment #5
cilefen commentedI am closing this support request because there have been no recent comments.
Comment #6
alberto56 commentedI 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(',') %}
{% endfor %}
...
If you are using PHP 7, you will see the following in your markup:
* for "": ""
* for "something": "
* for "something,something else": "
In PHP 7, a PHP warning will be logged:
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:
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:
See also https://drupal.stackexchange.com/questions/228388/how-do-i-get-the-raw-f...
Comment #7
alberto56 commentedMoving this to fixed based on the above, please change the status if needed.
Comment #10
kreatil commentedI 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.
Comment #11
visway12 commentedFixed this issue by applying a custom patch.
PHP: 8.1.0
Drupal: 10.2