Problem/Motivation
Since updating to 8.x-1.9, we've observed many warnings similar to this:
Warning: foreach() argument must be of type array|object, string given in Drupal\Core\Template\Attribute->__construct() (line 85 of core/lib/Drupal/Core/Template/Attribute.php).
Drupal\Core\Template\Attribute->__construct(' focusable="false"') (Line: 324)
Drupal\Core\Theme\ThemeManager->render('twig_svg', Array) (Line: 491)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 248)
Drupal\Core\Render\Renderer->render(Array) (Line: 475)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 94)
__TwigTemplate_abff251e479301460c3f95297fd8352e->
Unknown macro: {closure}
() (Line: 1775)
Twig\Extension\CoreExtension::captureOutput(Object) (Line: 40)
__TwigTemplate_abff251e479301460c3f95297fd8352e->doDisplay(Array, Array) (Line: 360)
Twig\Template->yield(Array) (Line: 335)
Twig\Template->render(Array) (Line: 38)
Twig\TemplateWrapper->render(Array) (Line: 33)
twig_render_template('themes/custom/mycustomtheme/templates/misc/flag.html.twig', Array) (Line: 348)
Drupal\Core\Theme\ThemeManager->render('flag', Array) (Line: 491)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 504)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 248)
Drupal\Core\Render\Renderer->render(Array) (Line: 475)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 79)
__TwigTemplate_1b0b6d1686a1498389ae4ab8ff9ecd1b->block_content(Array, Array) (Line: 430)
Twig\Template->yieldBlock('content', Array, Array) (Line: 65)
__TwigTemplate_1b0b6d1686a1498389ae4ab8ff9ecd1b->doDisplay(Array, Array) (Line: 360)
Twig\Template->yield(Array) (Line: 335)
Twig\Template->render(Array) (Line: 38)
Twig\TemplateWrapper->render(Array) (Line: 33)
twig_render_template('core/themes/stable9/templates/block/block.html.twig', Array) (Line: 348)
Drupal\Core\Theme\ThemeManager->render('block', Array) (Line: 491)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 248)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 165)
Drupal\Core\Render\Renderer->Drupal\Core\Render{closure}() (Line: 638)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 164)
Drupal\Core\Render\Renderer->renderInIsolation(Array) (Line: 191)
Drupal\Core\Render\Renderer->doRenderPlaceholder(Array) (Line: 228)
Drupal\Core\Render\Renderer->renderPlaceholder('callback=Drupal%5Cblock%5CBlockViewBuilder%3A%3AlazyBuilder&args%5B0%5D=neso_theme_flagblock&args%5B1%5D=full&args%5B2%5D&token=lyGZENngjeMSUm6nLL4kqnTsdEgN6gQuYw6_eGm9tYo', Array) (Line: 697)
Drupal\big_pipe\Render\BigPipe->renderPlaceholder('callback=Drupal%5Cblock%5CBlockViewBuilder%3A%3AlazyBuilder&args%5B0%5D=neso_theme_flagblock&args%5B1%5D=full&args%5B2%5D&token=lyGZENngjeMSUm6nLL4kqnTsdEgN6gQuYw6_eGm9tYo', Array) (Line: 524)
Drupal\big_pipe\Render\BigPipe->Drupal\big_pipe\Render{closure}()
Fiber->start() (Line: 531)
Drupal\big_pipe\Render\BigPipe->sendPlaceholders(Array, Array, Object) (Line: 283)
Drupal\big_pipe\Render\BigPipe->sendContent(Object) (Line: 113)
Drupal\big_pipe\Render\BigPipeResponse->sendContent() (Line: 423)
Symfony\Component\HttpFoundation\Response->send() (Line: 20)
Steps to reproduce
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Issue fork twig_svg-3474004
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
malcomio commentedI think this probably comes from the changes in https://git.drupalcode.org/project/twig_svg/-/commit/b3e19eeeb641f770749... for #3441358: Mechanism for including SVGs from PHP code
Comment #4
aaron.ferris commentedI think this has come in as part of the move to the twig template, whereas this loop before was always a string in the flat markup, with it now being a twig template it's throwing complaints (assumedly because we use attributes as the var name?)
Original loop: https://www.drupal.org/project/twig_svg/issues/3023150
Twig: https://www.drupal.org/project/twig_svg/issues/3023153
Change makes sense to me thanks, ill have a proper look over the weekend (test etc).
Comment #5
aaron.ferris commentedI think this fix is good
Complete Example in twig:
{{ icon('icon-name', 'Icon title', ['extra-class', 'another-class'], {'name-one': 'value-one', 'name-two': 'value-two'}, ['wrapper-class-1', 'wrapper-class-2']) }}Result:
Example without attributes in twig:
{{ icon('icon-name', 'Icon title') }}Result:
Example with just icon name in twig:
{{ icon('icon-name') }}Result:
(And no warnings).
Comment #6
aaron.ferris commentedComment #9
aaron.ferris commentedMerged to 2.x and cherry-picked to 8.x
New releases for both branches.
Thanks!
Comment #10
aaron.ferris commented