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.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | twig_template_warning_error-3220299-8.patch | 871 bytes | shelane |
Issue fork bootstrap-3220299
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
revati_gawasComment #3
revati_gawasAttached patch got resolved above issue. Please review. Thank you!
Comment #4
tanmaykComment #5
intrafusionPatch works as expected and removes these error messages
Comment #6
markhalliwellHey 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.Comment #7
sarguna raj m commentedHi,
Do we have any updated patch for this issue? I'm facing the same issue and my watchdog table overflows with this warning!
Thanks.
Comment #8
shelaneSince 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.
Comment #10
kreatil commentedI 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:
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').classThe patch provided in #8 does not solve the issue in my case.
Comment #11
wylbur commentedI 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.
Comment #13
shelaneComment #14
kreatil commentedI don't see this fixed.
Comment #15
shelane@kreatIL, can you confirm the version of the Bootstrap theme you are using where you are still seeing the issue?
Comment #16
kreatil commentedOk, 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.