-
Using the path_to_theme() function
When we build Drupal sites we typically have a lot of custom design elements. It is important to be able to display images from the filesystem in a dynamic fashion (non-absolute paths). Should you need to move the site, you wouldn’t want to have to reset image paths everywhere. The base_path() and path_to_theme() functions do all of the dirty work for you.
12$theme_dir = base_path() . path_to_theme();print "<img src="{$theme_dir}/_images-base/myimg.png" alt="This is my image">";You may also want to check out the $directory variable that is part of the theme system in Drupal.
-
Add Javascript to a Specific Page
There are several ways to add javascript code to a page in Drupal. The method outlined below involves a modification to your theme’s template.php file. You’ll be editing (and un-commenting if necessary) your theme’s preprocess_page function. This basically lets you modify the variables that are available in your page.tpl.php file(s).
In the example below, I’ve checked if the page’s “is_front” property is true. You can use anything available in the $vars variable within the function. I suggest using the devel module to determine what variables are available. This can be done with: dpm($vars);
1234567891011121314/*** Override or insert variables into the page templates.** @param $vars* An array of variables to pass to the theme template.* @param $hook* The name of the template being rendered ("page" in this case.)*/function themename_preprocess_page(&$vars, $hook) {if ($vars['is_front']) {drupal_add_js(path_to_theme() . '/_javascript/homepage_rotate_bg.js', 'core');}$vars['scripts'] = drupal_get_js();} -
Get Taxonomy Terms by Count
This function returns an array of taxonomy term objects. It is much like taxonomy_get_tree(), and it does in fact use this function. We had a client that wanted a grid of product categories with an image for each term, but only for terms that have at least X associated nodes. This problem was solved using the function below, as well as another custom function to alter the views query (I’ll post about this later).
12345678910111213141516171819202122/*** Returns an array of taxonomy terms that have been associated with at least X nodes** @param $vid* The vocabulary id* @param $min_items* The minimum number of items that have been associated with this term* @return* An array of taxonomy term objects*/function mymodule_get_terms_by_count($vid, $min_items = -1){$terms = taxonomy_get_tree($vid);$items = array();foreach ($terms as $term) {$count = taxonomy_term_count_nodes($term->tid);if ($count == -1 || $count >= $min_items) {$items[] = $term;}}return $items;}I’d like to also mention that I toyed with the idea of adding a $max_items variable. It’s simple enough to do, but I figured (maybe incorrectly) that you wouldn’t ever need to say, “Give me all terms that have between 10 and 30 associated records.”
-
Taxonomy Menus for Custom Views
Note: If you’re looking to just output a list of taxonomy terms that link to a custom view, please see PHP Code to Output a List of Terms Linked to a Custom View. If you need actual Drupal menu items for each term in a vocabulary to point to a custom view, please read on.