This might be a really stupid question because I am overlooking something really simple but here is my issue. I have created a custom block in the custom block library in Drupal 8. I would like to insert images into the block from the images folder in my theme (not upload images using ckeditor).

Normally in Drupal 7 I can manually insert the image using an tag and all is good. In Drupal 8 this method is not working for me. Here is an example of the code that I enter into the source view:

<img class="socialmedia" src="/themes/custom/mytheme/images/social_media/facebook.png" />

However this does not work - no image is being displayed. I have verified that the path is correct, the spelling is correct, and tried with and without the leading slash. Nothing works.

I found a similar question, but the answers supplied don't address the question as they use php code.

What has changed between Drupal 7 and 8 in this regard? What am I missing?


  • Oh my bad. (I have been switching a bit too much between the two today.) Are you getting nothing at all or a red cross? It might be due to FilterHtmlImageSecure that is new in D8 and aims to filter insecure images. You can disable this filter on the settings page of each text format to check if it causes the error. Jun 25, 2017 at 22:25
  • @Neograph734 I am getting nothing at all. I have the FilterHtmlImageSecure filter set to off.
    – user5013
    Jun 25, 2017 at 23:29

2 Answers 2


With twig you can use {{ directory }} to point your theme directory.


Use the FormattableMarkup class for this:

$image = new FormattableMarkup('<img class="socialmedia" src="@image" />', [
          '@image' => '/themes/custom/mytheme/images/social_media/facebook.png'

$block['your_element'] = $image;

From the FormattableMarkup docs:

When cast to a string, this object replaces variable placeholders in the string with the arguments passed in during construction and escapes the values so they can be safely displayed as HTML. See the documentation of \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details on the supported placeholders and how to use them securely. Incorrect use of this class can result in security vulnerabilities.

  • Hi Gun5m0k3, your solution uses php code which you can't enter into the ckeditor text area for a custom block. I am looking for a solution that doesn't use php. Although thanks for pointing out the FormattableMarkup class - I didn't know about that.
    – user5013
    Jun 27, 2017 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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