I'm trying to understand if/how you insert Twig variables in blocks in the custom block library.

To clarify, the block's HTML has been configured via the UI at /admin/structure/block/block-content

Standard Twig syntax, e.g. a 'say-something' tag like {{ base_path ~ directory }} - which I want so I can set an <img src attibute correctly - doesn't seem to be parsed at all and I don't see a way to use Tokens.

Examining the HTML source with Twig debugging on, it's theme hook is listed as "field", so if I add a preprocess function:

function themename_preprocess_field(array &$variables, $hook) {
  • that's being called rather too often
  • when it does reach the custom block, there doesn't seem to be anything in $variables that labels it as such, other than a render array with the processed text:


What's the simplest approach for this? Is the only way to do it to write a module?

Thanks, William

1 Answer 1


You can't use twig variables in the body field of a block (or a node). But you can use tokens. Install the modules Token and Token Filter. You need to add the token filter to the text format you are using in the body field.

  • Thanks. For benefit of others: you edit the text format via /admin/config/content/formats, choose Configure, check 'Replaces global and entity tokens with their values'. There's a list of all available tokens at /admin/help/token Dec 30, 2016 at 15:43

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.