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.
Comments
Comment #2
chintan.vyas commentedYou should set site_logo variable like this or simply add `/` in your preprocess function.
Comment #3
chintan.vyas commentedComment #4
henry.odiete commentedThe 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, especiallyfile_url_transform_relative(file_create_url($logo))is what is being used to implement site_logo in block--system--branding-block.html.twig.