Problem/Motivation
After creating a simple Tab and Tab section structure (complete with some sample content), upon saving the node, the following Twig error is displayed:
TypeError: Unsupported operand types: string + int in __TwigTemplate_50b45d73b2f3aed42e8c7384c3269f44->doDisplay() (line 215 of sites/default/files/php/twig/641b2ce01bdd8_paragraph--bp-tabs.html.t_JiV0wBml5bZeEjCJFMlyyWRdu/lqvEdco9u8Yxwi-Sgr_RutcqRYOef0bdL_fPCEa3b08.php).
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 367)
Twig\Template->display(Array) (Line: 379)
Twig\Template->render(Array, Array) (Line: 40)
Twig\TemplateWrapper->render(Array) (Line: 53)
twig_render_template('modules/contrib/bootstrap_paragraphs/templates/paragraph--bp-tabs.html.twig', Array) (Line: 372)
Drupal\Core\Theme\ThemeManager->render('paragraph', Array) (Line: 433)Steps to reproduce
- Fresh Drupal 10.0.5 site
- PHP version 8.1
- Dev version of Bootstrap Paragraphs
- Latest release version of Bootstrap base theme
- Content type with a single multi-valued Paragraphs field.
- Node with 1 "Tabs" paragraph type and 2 nested "Tab section" paragraph types (each with Simple content).
This was happened upon during a Drupal class that I'm teaching, we also encountered several other similar issues, so expect some of our students to report those as well :)
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | 3349742-14-bootstrap-paragraphs.patch | 5.88 KB | kerrymick |
Issue fork bootstrap_paragraphs-3349742
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
ultimikeUpdated with more of the stack trace.
Comment #3
dalemoore commentedThe problem seems to be from the introduction of this filter:
{% for key, item in content.bp_tab_section|filter(key => key|first != '#') %}combined with
#{{ paragraph_id }}-{{ key + 1 }}I don't think that filter does what it's supposed to do in Drupal 10. Before it was written like this:
{% for key, item in content.bp_tab_section if key|first != '#' %}
But that won't work in Drupal 10, either.
And you can't just break up the for and if statements like this:
Because loop.first isn't working either. There are tons of other things in that array that start with #.
Comment #4
ultimikeWe made some progress on a fix for this in class, but for some odd reason, we couldn't get the Twig
|filterfilter to work.As a desperation play, I got things working with a for loop and an if-statement in the paragraph--bp-tabs.html.twig file.
I would love for someone with stronger Twig skills than I to tell me where the issue lies with the
|filterthanks,
-mike
Comment #5
dalemoore commented@ultimike does the loop.first not work for you too? I am using that same method, but the loop.first doesn't make the first tab active on page load. Wondering if it's something in my code or if not working for you as well?
Comment #6
ultimike@dalemoore - I'm not 100% sure of what you're asking, but I can confirm that the "original code" (in my snippet above) does not work for me.
{% for key, item in content.bp_tab_section|filter(key => key|first == '#') %}I spent some time trying to figure out why, to no avail. Which is why I ended up rewriting it (for my own sanity) using a for-loop and if-statement. But, I really don't understand what I'm missing in the "original code".
-mike
Comment #7
dalemoore commented@ultimike I'm referring to this part:
<li role="presentation" class="{% if loop.first %}active{% endif %}"><a href="#{{ paragraph_id }}-{{ key + 1 }}" aria-controls="{{ item['#paragraph'].bp_tab_section_title.value }}" role="tab" data-toggle="tab">{{ item['#paragraph'].bp_tab_section_title.value }}</a></li>If that part is working, when the page loads the first tab should be active and its tab panel should be shown. If it's not working, then no tab panel will be visible until you first click one of the tabs. For me, using the code you have works to make the tabs work, but the first tab and its panel aren't shown on page load because the "active" class isn't being applied.
Comment #8
amirez commented@ultimike Apparently that Twig|filter
{% for key, item in content.bp_tab_section|filter(key => key|first == '#') %}doesn't work on my site neither.I tried your "workaround code", it did work but I got several warnings:
Warning: Array to string conversion in __TwigTemplate_50b45d73b2f3aed42e8c7384c3269f44->doDisplay() (line 141 of sites/default/files/php/twig/641b550d27442_paragraph--bp-tabs.html.t_JiV0wBml5bZeEjCJFMlyyWRdu/q6IuPi1QF5Vc0ZOn_ezvXfazGeN5Ju6PQDbYqX9W-jI.php).Comment #9
afarsal commentedHello,
I have same error. I change to
{#% for key, item in content.bp_tab_section|filter(key => key|first != '#') %#}
{% for key, item in content.bp_tab_section %}
{% if key|first != '#' %}
<li role="presentation" class="{% if loop.first %}active{% endif %}"><a href="#{{ paragraph_id }}-{{ key + 1 }}" aria-controls="{{ item['#paragraph'].bp_tab_section_title.value }}" role="tab" data-toggle="tab">{{ item['#paragraph'].bp_tab_section_title.value }}</a></li>{% endif %}
{% endfor %}
...
{# Loops through the tab sections again to print the tab section bodies. #}
{#% for key, item in content.bp_tab_section|filter(key => key|first != '#') %#}
{% for key, item in content.bp_tab_section %}
{% if key|first != '#' %}
And for accordion same error for paragraph--bp-accordion.html.twig
and the same change
{# Loop through all of the accordion sections. #}
{#% for key, item in content.bp_accordion_section|filter(key => key|first != '#') %#}
{% for key, item in content.bp_accordion_section %}
{% if key|first != '#' %}
...
{% endif %}
{% endfor %}
After this change no error and tabs and accordions works fine for tabs without error. But for accordion, the accordions list is not displayed and activated immediately. You have to select the first tab to display the accordions.
Need a stable release and test also for bootstrap 5 (with barrio bootstrap 5 theme)
Thanks very well.
AFA
Comment #10
loopy1492 commentedWe are getting the same error on paragraph--bp-accordiaon.html.twig. It is not happy with the for+if.
This is similar to an issue where you can't do for ifs anymore similar to this:
has to be
So any single line for/ifs need to be changed to separate for and then ifs.
Comment #12
loopy1492 commentedI suspect that the reason this wasn't flagged by the Upgrade Status module is because the |filter is broken not these templates themselves. My guess is that the definition uses if on a for tag which has been deprecated which seems strange to me, but researching the reasoning behind that is probably best left for another individual.
Comment #13
loopy1492 commentedComment #14
loopy1492 commentedComment #15
loopy1492 commentedComment #16
stacypendell commentedPatch fixes the problem for me. Same problem also seen on Drupal 9.5 when upgrading to the dev version of this module, and the patch fixed it there too.
Comment #17
keiserjb commentedThe patch doesn't apply for me. Using the dev version of the module on 10.0.9.
Comment #18
keiserjb commentedGot it to work, another patch was messing it up.
Comment #19
yecmom commentedHi,
Patch doesn't fix the problem for me on 9.5.9 : my carousel literally disappears though no error is logged.
Here's my code from line 169 of paragraph--bp-carousel.html.twig (slightly adjusted for Bootstrap 5):
And here's my previous code, that works but isn't compatible with Drupal 10:
If anyone could tell me what I did wrong, I would be very grateful !
Thanks!
Comment #20
platinum1 commentedSame problem
Comment #21
steveoriolHello, @platinum1 and @yecmom,
I manage to make it works by adding manually the patch on issue :
https://www.drupal.org/project/bootstrap_paragraphs/issues/3221758
>> https://www.drupal.org/files/issues/2022-03-15/bootstrap_paragraphs-3221...
Comment #22
kerrymick commented{% for key, item in content.bp_slide_content if key|first != '#' %}
needs to be updated to {% for key, item in content.bp_slide_content|filter((value, key) => key|first != '#') %}
For reference https://www.drupal.org/docs/upgrading-drupal/prepare-major-upgrade/prepa...
Patch attached.
Comment #23
shobhit_juyal commentedThe same issue was coming in bp-accordion and patch suggested in #22 is fixing that issue as well.
Comment #24
draenen commented#22 worked for us with 2.0.x-dev on Drupal 10.1.2
Comment #25
thejimbirch commentedPatch applies and has two approving comments.
Comment #27
thejimbirch commented