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

Latest Articles

VSCode Drupal Coding Standards Setup (From Scratch) admin_pixelthi… Mon, 12/22/2025 - 16:01 VSCode Drupal Coding Standards Setup (From Scratch)

VSCode Drupal Coding Standards Setup (From Scratch)

vscode
Using drupal/core-composer-scaffold to prevent rewriting .htaccess or robots.txt file admin_pixelthi… Sun, 12/01/2024 - 16:49 Using drupal/core-composer-scaffold to prevent rewriting .htaccess or robots.txt file

Using drupal/core-composer-scaffold to prevent rewriting .htaccess or robots.txt file

composer
Controlling ckeditor styling options with hook_editor_js_settings_alter admin_pixelthi… Thu, 05/16/2024 - 10:49 Controlling ckeditor styling options with hook_editor_js_settings_alter

Controlling ckeditor styling options with hook_editor_js_settings_alter

php hook hook_editor_js_settings_alter hook_field_widget_single_[WIDGET_TYPE]_form_alter