Skip to main content

Drupal 9: How to get the absolute path of a custom icon in your custom theme

This is how you can get in a custom or in a preprocess function the absolute path of an icon that you want to use in your custom theme.

    global $base_url;
    $default_theme_name = \Drupal::config('system.theme')->get('default');
    $theme_path = \Drupal::service('extension.list.theme')->getPath($default_theme_name);
    $popular_icon =
      $base_url . '/' .  $theme_path . '/assets/css/images/popular_icon.svg';

The "assets/css/images" path is where i have the custom icons or images that i want to use for my theme.

The $base_url can be skipped if you just want to use the relative path.

subtheme