Improve reliability of icon component in Drupal 10 by embedding rendering logic in Twig template

Created on 14 May 2025, 5 months ago

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.

Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇧🇪Belgium raluelrusu Brussels

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024