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

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

malcomio created an issue. See original summary.

malcomio’s picture

aaron.ferris’s picture

I 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).

aaron.ferris’s picture

I 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:

<span class="icon__wrapper wrapper-class-1 wrapper-class-2" data-vd-id="element-jgm31">
      <svg role="img" title="Icon title" aria-label="Icon title" class="icon icon--icon-name extra-class another-class" focusable="false" name-one="value-one" name-two="value-two" xmlns:xlink="http://www.w3.org/1999/xlink">
      <use xlink:href="#icon-name"></use>
  </svg>
</span>

Example without attributes in twig:
{{ icon('icon-name', 'Icon title') }}

Result:

<span class="icon__wrapper" data-vd-id="element-0wrprv">
      <svg role="img" title="Icon title" aria-label="Icon title" class="icon icon--icon-name" focusable="false" xmlns:xlink="http://www.w3.org/1999/xlink">
      <use xlink:href="#icon-name"></use>
  </svg>
</span>

Example with just icon name in twig:
{{ icon('icon-name') }}

Result:

<span class="icon__wrapper" data-vd-id="element-yxl9s">
      <svg aria-hidden="true" class="icon icon--icon-name" focusable="false" xmlns:xlink="http://www.w3.org/1999/xlink">
      <use xlink:href="#icon-name"></use>
  </svg>
</span>

(And no warnings).

aaron.ferris’s picture

Status: Active » Reviewed & tested by the community

  • malcomio authored 1d8b015d on 8.x-1.x
    Resolve #3474004 "Warnings foreach argument"
    
aaron.ferris’s picture

Merged to 2.x and cherry-picked to 8.x

New releases for both branches.

Thanks!

aaron.ferris’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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