5

I am creating custom theme for Drupal 8. I am uploading the logo from theme setting, but I am not able to print the path of that logo. What I am doing to print the full path of logo is using {{ logo }}, but it is not working.

In Drupal 7 we can get the full path of logo with the following code.

 <img src="<?php print $logo; ?>" alt="<?php print t('Home'); ?>" />

What is the equivalent for Drupal 8?

0

2 Answers 2

14

The class implementing the block showing the theme logo, in SystemBrandingBlock::build() contains the following code.

  $build['site_logo'] = array(
    '#theme' => 'image',
    '#uri' => theme_get_setting('logo.url'),
    '#alt' => $this->t('Home'),
    '#access' => $this->configuration['use_site_logo'],
  );

So, for the path of the logo, you can simply use theme_get_setting('logo.url'), which returns "/core/themes/bartik/logo.svg", in my test site.

If you want a relative URL for the logo file, for example because you want to use it for the src attribute of the <img> tag, you can do as template_preprocess_image() does.

$relative_url = file_url_transform_relative(file_create_url(theme_get_setting('logo.url')));

For an absolute URL, you can use the following code.

$absolute_url = file_create_url(theme_get_setting('logo.url'));

That code needs to go in a preprocess function, where it initializes a variable. For example, if I wanted the relative URL of the logo file in html.html.twig, I would use the following code.

function mytheme_preprocess_html(&$variables) {
  $variables['relative_logo_url'] = file_url_transform_relative(file_create_url(theme_get_setting('logo.url')));
}

And in the template, you would just use {{ relative_logo_url }} to get it.

1
  • The example of the absolute path does nothing, what would be the difference with the relative
    – chalo
    Oct 16, 2018 at 19:26
2

In Drupal 8, the site logo was moved in a block.

In block layout, you place a branding block in a region, which you then can configure to contain the logo, the site name and/or the slogan. You can place multiple branding blocks to use site branding elements in different regions.

1
  • 1
    Thank you for answer. I've placed the "Site branding" block to the appropriate region but the logo image is showing broken. When I check the source code it is showing this <img src="example.com/themes/theme_name/logo.svg" alt="Home"> but I added the .jpg image with different name from theme setting but every time it is showing logo image name logo.svg
    – fanatic
    Aug 12, 2016 at 16:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.