0

I have below code in:

{% set img_url = item.image.url ? item.image.url : {{ base_path ~ directory }}'/images/default.png' %}

Twig file. How to make it work so it will take full default image path?

0

To get the base_url (path) you will need this in your THEMENAME.theme file

function THEMENAME_preprocess_page(&$variables) {
  global $base_url;
  $variables['base_path'] = $base_url;
}

Then it will be available in your twig template

But most of the times you will not need the full path to display an image

You can try the following

{% set img_url = item.image.url ? item.image.url : '/'{{ directory }}'/images/default.png' %}
  • You can use Drupal's base_path() function, to get a relative path to the drupal web. $GLOBALS['base_url'] does return the absolute url. Just using {{ '/' ~ directory }} does not work if your drupal website is installed in a subfolder of webroot (e.g. example.com/mydrupalproject/). – Philipp Michael Jan 15 '18 at 10:30

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.