Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
When porting a theme from 4.7 and moving to a different server, I had to repair paths to images in the theme directory that were hard-coded into the tpl files.
With this patch, instead of:
<?php base_path() . path_to_theme() ?>/box_bottom.png
I can include, more succinctly:
<?php base_path_to_theme() ?>/box_bottom.png
These paths won't break when the theme is moved around in the sites directory or renamed.
Comment | File | Size | Author |
---|---|---|---|
#7 | theme_path_helper-110632-7.patch | 808 bytes | dropcube |
#6 | theme.inc_.patch | 708 bytes | pnm |
theme.inc_16.patch | 650 bytes | pnm | |
Comments
Comment #1
catchNo longer applies.
Comment #2
dvessel CreditAttribution: dvessel commentedYou can also do this:
This applies to the current version and goes way back to 4.7 I think. So, I don't see a real need for this patch.
Comment #3
pnm CreditAttribution: pnm commenteddvessel: your suggestion doesn't work for me in 5.0.
base_path_to_theme() returns
/drupal/subsite/sites/subsite/themes/subsite/box_bottom.png
url("$directory/box_bottom.png") returns
/drupal/subsite/?q=en/sites/subsite/themes/subsite/box_bottom.png
Comment #4
pnm CreditAttribution: pnm commentedComment #5
pnm CreditAttribution: pnm commentedComment #6
pnm CreditAttribution: pnm commentedI updated the above patch against HEAD.
Comment #7
dropcube CreditAttribution: dropcube commentedI suggest the following helper function:
It will avoid themers to repetitively use
base_path() . path_to_theme() . $file
, and instead use this more succinctly function.For example:
base_path() . path_to_theme() . '/fix-ie.css'
base_path() . path_to_theme() . '/images/bg.png'
Can be converted respectively to:
base_path_to_theme('fix-ie.css')
base_path_to_theme('images/bg.png')
Find attached the patch.
Comment #8
drawk CreditAttribution: drawk commentedPatch still applies, with offset
Comment #9
RobLoachI think helper functions like this are better lived in the template.php file of your theme, and not part of core where they might never be used. Just my thoughts though...
Comment #10
dvessel CreditAttribution: dvessel commentedI made a bad call on #2 but I still don't think this needs a function.
Instead, setup the variables within "template_preprocess" so it can be used in any preprocessor or template file. The function calls are cheap and it would be a two line change. The $base_path variable in "template_preprocess_page" should be moved to "template_preprocess" and the $directory variable should be changed to $theme_path. $directory is seldom used since it has little meaning in this context. Not many know that they can use it.
It would end up like this:
If we can prevent function calls inside templates, lets not provide another function as a shortcut. Make the variables available instead. If you need to call these paths from theme functions, the two functions "base_path" & "path_to_theme" should be fine.
Comment #11
webchickTo me
is still messy (and error prone; why not $base_path/$theme_path?). Could we do:
or similar? one variable to remember rather than two? I leave it to themers' discretion what that variable is best called.
Comment #12
RobLoachMorbus was throwing around the idea of a
base_url()
function...Comment #13
dvessel CreditAttribution: dvessel commentedThat would work too Webchick but having them separated is useful too so we might need 3 variables. If we stuck to one standard in how paths are used, a single path would be fine. For example, theme_image can't take in the base path while drupal_add_css does. Maybe another thing to tackle.
Comment #14
RobLoachDid I hear drupal_add_css? #266358: Use $options in drupal_add_css()
Comment #15
pnm CreditAttribution: pnm commentedSince every themer will face this problem, I think it's better to have a helper function, and "one best way" of doing it.
Having a standard way would reduce the chance of themers creating code that works on their current configuration -- a site in / -- but breaks if the site is moved to a subdirectory. This is the situation that led me to post this request in the first place.
In addition, people learning Drupal and reading theme code for the first time will better understand what to do.
Comment #16
Anonymous (not verified) CreditAttribution: Anonymous commentedThe last submitted patch failed testing.
Comment #17
sunMost modules in contrib that implement path lookup helper functions use an optional boolean argument to return the same path prefixed with base_path().
So:
Comment #18
RobLoachNote that there's also talk of vamping base_path() with hook_file() goodness to make it easy to push files to CDNs or host files externally.
Also, I think instead of using a PHP function for this, it might be nicer to give designers and theme developers a variable. $base_path, $path_to_theme, etc?
Comment #19
sunFor template files, we have $base_path and $directory already. IMHO, that is sufficient and allows for any flexible styling.
However, that "base_path() . path_to_theme()" weirdness not only used in theme.inc, template.php, but also in various modules and theme functions.
Comment #20
dropcube CreditAttribution: dropcube commentedComment #21
andrenoronha CreditAttribution: andrenoronha commentedhow can I call base_path() inside my template.php file???