Problem/Motivation

The current implementation of the EU ECL Icon component, as defined in the eu_europa_component_lib.icons.yml and icons component, works in Drupal 11, but is unreliable in Drupal 10, especially in contexts like Layout Builder, where the icon function may not be available, resulting in rendering failures.

Proposed resolution

To resolve this, I propose embedding the icon rendering logic directly into the component’s Twig template. This approach makes the component self-contained and more robust across different Drupal environments.

Implementation Plan

  • 1. Create a Twig extension to define a custom function (e.g., get_sprite_path()) that dynamically retrieves the SVG sprite path using Drupal’s module path services.
  • 2. Modify the icon component’s Twig template to directly incorporate the rendering logic, using the new get_sprite_path() function for the <use> element’s href attribute.
  • Eliminate the icon Function Dependency: Refactor the component to remove calls to the icon function, ensuring all logic is handled within the template itself.
  • The Twig extension must be registered in the module’s services configuration to ensure proper loading.

API changes

The get_sprite_path() function should leverage Drupal’s APIs (e.g., Drupal::service('extension.list.module')) to construct the sprite path dynamically.

Comments

raluelrusu created an issue. See original summary.

raluelrusu’s picture

Issue summary: View changes

  • raluelrusu committed 7ef8395b on 1.x
    Issue #3524451 by raluelrusu: delete components/icon/demo.twig
    

  • raluelrusu committed 8dc098b9 on 1.x
    Issue #3524451 by raluelrusu: delete eu_europa_component_lib.icons.yml
    

  • raluelrusu committed b103c888 on 1.x
    Issue #3524451 by raluelrusu: update icon component
    

  • raluelrusu committed 0c043dd4 on 1.x
    Issue #3524451 by raluelrusu: renamed component from icons to icon
    

  • raluelrusu committed afb369a3 on 1.x
    Issue #3524451 by raluelrusu: Register the twig extension as a service
    

  • raluelrusu committed 6593a201 on 1.x
    Issue #3524451 by raluelrusu: Create Twig Extension at src/TwigExtension...
raluelrusu’s picture

Status: Active » Fixed
raluelrusu’s picture

Status: Fixed » Closed (works as designed)