3

I have a theme called Eternal with the following data in it's eternal.info file-

name = Eternal
description = A Customized Drupal 7 Theme
core = 7.x
engine = phptemplate

In the template folder lies the page.tpl.php. Under the /themes/ folder, the address becomes -

/eternal/templates/page.tpl.php

There are several lines inside the page.tpl.php that require I define a path for an image, like so-

<img src="images/facebook_icon.png" alt="image comes here">

The /images/ folder is located inside the /eternal/ folder, so to access it from /templates/ one would need to travel one level up and then access the /images/ folder. That would look like this - /../images/.

The problem is, I cannot define a link that works in Drupal. And I'm sure the images exist in that directory.

I've tried these-

<img src="../images/facebook_icon.png" alt="image comes here">

<img src="images/facebook_icon.png" alt="image comes here"> <!-- not like this would have worked anyway -->

<img src="<?php drupal_get_path('theme', 'eternal'); ?>/images/facebook_icon.png" alt="image comes here">

It just shows the alt text in every case. No image. How to accomplish this and what is the recommended and proper way to do this?

5
  • <?php drupal_get_path('theme', 'eternal'); ?> should be what you need. What's its result/return value?
    – Letharion
    Commented Jul 26, 2013 at 12:37
  • When I print it, it returns nothing. Commented Jul 26, 2013 at 13:18
  • I can't think of any way that would happen. Clear cache. Try disabling the theme and re-enable it, not that I now how that would help. Double-check your spelling of the theme name in the code, and do other fundamental sanity checks. Last resort, directly debug api.drupal.org/api/drupal/includes!bootstrap.inc/function/…
    – Letharion
    Commented Jul 26, 2013 at 13:28
  • Is the theme enabled and in use?
    – Clive
    Commented Jul 26, 2013 at 13:40
  • 1
    It's a best practice to use background image instead of img element if the image is not part of the content. See stackoverflow.com/questions/492809/… for more detailed explanation
    – ya.teck
    Commented Jul 26, 2013 at 15:00

4 Answers 4

6
<?php print drupal_get_path('theme', 'eternal'); ?>

as you've already tried, is the correct way to solve your problem. Notice though that you have to print it out as well, as drupal_get_path will only return the path, not print it for you.

I can't immediately think of any reason why it would not work in your case. Some potential suggestions to try could be

  1. Double-check your spelling of the theme name in the code.
  2. Clear caches.
  3. Try disabling the theme/module and re-enable it.

Failing everything else, you'll need to debug drupal_get_filename() directly.

1
  • This worked perfectly: <?php print drupal_get_path('theme', 'eternal'); ?> I was missing the print. Commented Jul 29, 2013 at 12:19
3

You can get path to your theme with $directory variable which will be available in all your templates.

<img src="<?php print $base_path . $directory; ?>/images/example.png"/>

See template_preprocess()

If you have multiple img elements in your page template you can create a dedicated variable for page.tpl.php that will keep path to your images directory.

/**
 * Impelements hook_preprocess_page().
 */
function eternal_preprocess_page(&$vars) {
  $vars['images_dir'] = $vars['base_path'] . $vars['directory'] . '/images';
}

Now, it looks more clear.

<img src="<?php print $images_dir; ?>/example.png"/>
0
1

As mentioned by Letharion, drupal_get_path() should be your first port of call.

Returns the path to a system item (module, theme, etc.).

The only reason I have posted this as an answer, rather than just left it at Letharions answer is that I wanted to bring attention to the System Stream Wrapper module as well:

Provides stream wrappers to access module, theme, profile, and library files and directories.

Which means that theme://eternal/images/example.jpg would also work with this module enabled.

1

You have to print/echo the output of drupal_get_path() for it to actually appear in your markup. By default it simply returns a PHP string.

<img src="<?php print($base_path.'/'.drupal_get_path('theme', 'eternal')); ?>/images/facebook_icon.png" alt="image comes here">
4
  • As a side note, your relative paths aren't working because they'd be looking relative to the path of the page being viewed, not to your /templates/ folder.
    – Eric N
    Commented Jul 26, 2013 at 14:12
  • You forgot to prefix the path with $base_path variable.
    – ya.teck
    Commented Jul 26, 2013 at 14:55
  • @Xio You're right; I'm accustomed to working with a site that doesn't have a base path deeper than the document root so I haven't needed to use it. Even so, I forgot to include a leading slash. Edited the answer to make it more universal.
    – Eric N
    Commented Jul 26, 2013 at 15:01
  • You should not include a leading slash since it's already present in $path_path variable.
    – ya.teck
    Commented Jul 26, 2013 at 15:25

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.