I have a scenario where menu attributes added to menu items throws a twig template error when using a Bootstrap theme.

For example:

  • Menu 1
  • Menu 2
    • Menu 2.1
    • Menu 2.2

When I add an attributes to "Menu 2", the added attribute does display properly but twig template warning got throw on pages.

Warning message is :

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

Drupal Version: 9.1.0

Related issue: Attributes are removed from menu links

Also I am bit uncleared about this line https://git.drupalcode.org/project/bootstrap/-/commit/3b144f50#23b748238... as classes will be added as a part of preprocess.

Issue fork bootstrap-3220299

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

revati_gawas created an issue. See original summary.

revati_gawas’s picture

Issue summary: View changes
revati_gawas’s picture

StatusFileSize
new997 bytes

Attached patch got resolved above issue. Please review. Thank you!

tanmayk’s picture

Title: Throw twig template error » PHP warning from menu twig template
intrafusion’s picture

Status: Active » Reviewed & tested by the community

Patch works as expected and removes these error messages

markhalliwell’s picture

Version: 8.x-3.23 » 8.x-3.x-dev
Status: Reviewed & tested by the community » Needs work

Hey there, thanks for the patch! After reviewing the patch and while the removal of the lines does in fact prevent an error from appearing, it also removes functionality for container_attributes. This issue will need another approach.

sarguna raj m’s picture

Hi,

Do we have any updated patch for this issue? I'm facing the same issue and my watchdog table overflows with this warning!

Thanks.

shelane’s picture

Assigned: Unassigned » shelane
Status: Needs work » Needs review
StatusFileSize
new871 bytes

Since we don't want to actually remove the lines, we want to test if the value is actually a string before attempting to split on the value. If it is, it will split into an array, if it is not, it will simply assign the item_classes variable to the class value.

Anyone having this issue, please give this patch a test. I don't know how to definitively recreate the issue to give it an adequate test.

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

kreatil’s picture

I got exactly the error described in the OP. Operating on Drupal 9 with a Bootstrap 3.25 based child theme.

After upgrading from PHP 7.4.21 to PHP 8.1.6, it not only throws warnings, but breaks the whole site. Error log says:

TypeError: explode(): Argument #2 ($string) must be of type string, array given in explode() (Zeile 804 in [my_local_path]/vendor/twig/twig/src/Extension/CoreExtension.php)
#0 [my_local_path]/vendor/twig/twig/src/Extension/CoreExtension.php(804): explode(' ', Array)
#1 [my_local_path]/web/sites/default/files/php/twig/6381f38a21a2a_menu.html.twig_T3O3iqxO8YUMocYIXr3uioikC/INIBfYGQnuw2V12GUVVkQc1ExSg_FpS0jsNvJjexFQQ.php(93): twig_split_filter(Object(Drupal\Core\Template\TwigEnvironment), Array, ' ')

I temporally fixed the error by cloning menu.html.twig of the parent bootstrap (3.25) theme into my custom theme and editing line 27:
set item_classes = item.url.getOption('container_attributes').class | split(' ')
into
set item_classes = item.url.getOption('container_attributes').class

The patch provided in #8 does not solve the issue in my case.

wylbur’s picture

Status: Needs review » Needs work

I found this issue and can confirm @kreatil comments in https://www.drupal.org/project/bootstrap/issues/3220299#comment-14802492

This patch no longer applies. The container_attributes are passed as an array, so the split method cannot be applied in any circumstance.

The actions in comment #10 resolved this for me today.

  • shelane committed 1bb37f5a on 8.x-3.x authored by kreatIL
    Issue #3220299 by revati_gawas, kreatIL, shelane, markhalliwell, wylbur...
shelane’s picture

Status: Needs work » Fixed
kreatil’s picture

Status: Fixed » Needs work

I don't see this fixed.

shelane’s picture

@kreatIL, can you confirm the version of the Bootstrap theme you are using where you are still seeing the issue?

kreatil’s picture

Status: Needs work » Fixed

Ok, I tested it again with version 3.26 and can confirm that the error no longer occurs. I should have looked more closely. I am sorry.

Status: Fixed » Closed (fixed)

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