• Development

    Views Taxonomy: Get Terms with Associated Nodes

    This example serves as both an example of how to alter a Views2 query, as well as how to use the get_terms_by_count() function I’ve written.

    Unfortunately there is not (at present) a Views2 taxonomy filter that lets you “Get only terms with at least X associated nodes.” We had a client request that terms without associated nodes be hidden. This was actually more complex than it sounds, but the solution led me to a whole new level of Views2 understanding. Views2 has a hook called hook_views_query_alter() that lets you alter a Views2 query before it is executed. This is exactly what we needed to do in order to only pull terms with associated nodes. Specifically, we needed to add an additional WHERE clause to the query.

  • Development

    Rebuilding the Drupal Navigation Menu (Automatically)

    The following steps will get you a completely rebuilt Navigation menu.

    1. Clear your cache
    2. Backup your database
    3. In the database, remove any records in {menu_links} where menu_name = navigation
    4. Create a file somewhere in your site’s directory structure
    5. Add this code to that file and then save it
    6. Tell Drupal to ignore this file using this method.
    7. Browse to the file in your web browser (it will run quickly, with no indication of completion). Wait for the page to stop loading, then check your navigation menu settings in the admin.
    8. When finished, delete or make-inaccessible the PHP file you just made!
  • Development

    $user Conditionals

    This is one example of performing an action based on whether or not the current user belongs to a certain role or roles. Note that the admin user (UID #1) matches as well.

    Note: You may want to try using drupal_goto()  (or drupal_access_denied() in this case) instead of a header redirect.

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

  • Development

    Multiple Submit Buttons on a Webform

    multiple-webform-submitI’ve spent a lot of time working with hook_form_alter() functions to modify forms, but the other day I was stumped by a simple problem. I needed to have two submit buttons on a form. By default, in the latest branch of Webform, you can change the text label on the default submit button for a webform. If you take a look at the screenshot on the right, you’ll see what my demo form looks like. The “Request More Information” button is the default submit button. The “Refer a Friend” button is the one I’m showing how to add via hook_form_alter().

  • Development

    Dynamic and AJAX-driven Select Lists in Webform

    multiple-webform-submit

    This example is a two-for-one deal! I’m going to demonstrate how to

    1. dynamically populate a webform select list on page load
    2. dynamically populate a webform select list based upon the value chosen in another webform select list

    For both parts of this example, the demo form I’ll be using is a “Get Started” form.