2

So I'm using drupal_get_path() within a block to reference an image in my themes//images directory.

And this works. At the root and all first level pages. example.com, example.com/about, etc.

However, if I am on a page such as example.com/about/csa:

Instead of continuing to output the filepath as /sites/all/... it outputs the file as //sites/all/...

This only happens when I'm in a subdirectory of the site.

Does anyone know why this might happen?

EDIT:

<?php $src = drupal_get_path('theme', $GLOBALS['theme']) . '/images/carrots.png'; 
echo '<img src=' . $src . ' alt="carrot background image" >'
?>
3
  • Post the code on how you used drupal_get_path? Commented Jan 31, 2014 at 5:29
  • Enable pathologic module and check it on in the desired filter(for example full html) and run your html code using check_markup function.
    – AgA
    Commented Jan 31, 2014 at 5:43
  • I was under the impression that pathologic was more for sitewide brokenness or for having your Drupal root somewhere other than your web root. I'll give it a shot. I've also updated my question with the code.
    – aprohl5
    Commented Jan 31, 2014 at 16:27

1 Answer 1

5

Its because drupal_get_path returns relative path.

Try this:

<?php 
  global $base_path;
  $src = $base_path . drupal_get_path('theme', $GLOBALS['theme']) . '/imag/carrots.png'; 
  echo '<img src=' . $src . ' alt="carrot background image" >'
?>

Edit with url function:

<?php 
  $src = url(drupal_get_path('theme', $GLOBALS['theme']) . '/imag/carrots.png'); 
  echo '<img src=' . $src . ' alt="carrot background image" >'
?>
2
  • Thank You. I used the pathologic module and it solved my problem. Is there a reason that one convention would be better or worse than the other?
    – aprohl5
    Commented Jan 31, 2014 at 18:49
  • 2
    Great! Pathlogic is definitely a good module. But I won't use it just for this minor issue. Every module brings its own set of function calls. I always try to keep the number of contributed modules at minimum. This bloats drupal less. better site performance. easier updates/upgrades. lesser bugs.
    – Amrit
    Commented Feb 1, 2014 at 3:15

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.