I am trying to get theme logo path in my custom module and image is broken due to missing leadng slash in the function.

/**
 * Implements hook_preprocess_HOOK().
 */
function MY_MODULE_preprocess_page(&$variables) {

  $site_config = \Drupal::config('system.site');
  $variables['site_name'] = $site_config->get('name');
  $variables['site_slogan'] = $site_config->get('slogan');
  $variables['site_logo'] = \Drupal::theme()->getActiveTheme()->getLogo();

}

If I use {{site_logo}} in page.html.twig the logo image is broken. If I add a leading slash to the site logo then the logo is being rendered.

My page.twig.html with and without leading slash in site_logo variable .

{#Broken Logo #}

<img class="img-responsive" src="{{ site_logo }}" />

{#Rendered Logo #}

{% if site_logo %}
    <a href="{{ path('<front>') }}" title="{{ 'Home'|t }}" rel="home">
      <img src="{{ "/"~site_logo }}" alt="{{ 'Home'|t }}" />
    </a>
 {% endif %}

I am using latest stable Drupal 8 Version with Bartik Theme.

CommentFileSizeAuthor
broken-logo.png2.78 KBbhanu951

Comments

Bhanu951 created an issue. See original summary.

chintan.vyas’s picture

You should set site_logo variable like this or simply add `/` in your preprocess function.

$logo = \Drupal::theme()->getActiveTheme()->getLogo();
$variables['site_logo'] = file_url_transform_relative(file_create_url($logo));
chintan.vyas’s picture

Priority: Normal » Minor
henry.odiete’s picture

Assigned: Unassigned » henry.odiete
Status: Active » Closed (works as designed)

The image isn't broken as \Drupal::theme()->getActiveTheme()->getLogo() is taking the direct config setting from logo image in appearance. I believe the solutions provided by @chintan-vyas are correct, especially file_url_transform_relative(file_create_url($logo)) is what is being used to implement site_logo in block--system--branding-block.html.twig.