• Development

    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.

    You may also want to check out the $directory variable that is part of the theme system in Drupal.

  • Development

    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);


  • Development

    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).

    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.”