• Development

    Drupal Views – Show One (Most Recent) Item Per Group

    Alright, the title is a bit misleading because I’m not using Groups, but it is the best explanation of what I’m achieving with this example. My goal is to show a list of the most recent blog post in each category, where clicking the category takes you to the associated post.

    I have a content type called Blog Post which has the following fields:

    • Date (field_blog_date – date field – single value)
    • Category (field_blog_tr_category – taxonomy reference field – single value)
    • Items (field_blog_cr_items – content reference field – unlimited values)
  • Development

    Views Exposed Filter: Terms for current language only

    I’ve been working on a multilingual site that has a product finder. We have 3 exposed filters that allow a user to select an Activity, IP Category, or Industry. All of these filters are Taxonomy Term filters. Some of the terms throughout those vocabularies have a specific language set, and should only show when that language is active.

    The first step is to make sure the output (Views results) only shows products where the node language matches the current language. This is easy using the built in Node translation: Language = Current user’s language filter.

  • Development

    Get a List of Top-level Taxonomy Terms

    Quick Tip: Getting a list of taxonomy terms is easy using a view (type: Taxonomy). Trying to get only terms at the highest level is a bit trickier. One method I came up with is to add a Taxonomy: Parent term argument on the view. The key is to tell the argument to Provide default argument of PHP Code and set the value to  return 0;

    The result is that only top-level terms (terms without parents) are returned.

  • 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

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