How can I get the base URL in the custom block?

enter image description here

  • Welcome to Drupal Answers! In a custom block you cannot normally use Twig, but only HTML markup. Are you using any specific module to allow that?
    – apaderno
    Sep 10 '20 at 8:21
  • No, I'm not using any specific module but I want the base URL in the block. Is this not possible in any way?
    – user99
    Sep 10 '20 at 9:00
  • For the content of a custom block you create from the user interface, you cannot use Twig nor PHP on a standard installation. Just enter the correct path of the image.
    – apaderno
    Sep 10 '20 at 9:00
  • Does this answer your question? How do I get the base path?
    – leymannx
    Sep 10 '20 at 9:57
  • I want the dynamic website URL inside the custom block. In D7, it was possible with the $base_url function inside the body but now in D8 PHP text format has been removed, only HTML markups are allowed which is making it impossible to happen. I'm not able to find the solution for this.
    – user99
    Sep 11 '20 at 9:06

First of all, don't prepend the base url by hand, use the core PHP function file_create_url or TWIG file_url:

<img src="{{ file_url('core/modules/image/sample.png') }}" />

And then, as commented, you can't use Twig out-of-the-box in Ckeditor. You need to install a module like Twig Input Filter and enable the Twig filter in /admin/config/content/formats for your text format.

  • This worked for me..thankyou so much!
    – user99
    Sep 11 '20 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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