• Development

    Programmatically displaying an image using an imagecache preset

    The following illustrates one way to render an image using an Imagecache preset. Naturally, if your imagefield has multiple uploads you could wrap this in a foreach.

     

  • Development

    States in key|value format

    The following is a list of the United States in key|value format. It’s useful when creating a select list in Drupal.

  • Development

    Countries in key|value format

    The following is a list of countries in key|value format. It’s useful when creating a select list in Drupal.

  • Development

    Custom Voting API Calculation

    The Voting API is really nice when used in conjunction with a module like Fivestar. It takes all of the complexity out of setting up a voting/rating system. On occasion, however, you need it to do some things that aren’t built in. Using the VotingAPI’s API, I’ll show you how to trigger a function (send an email, write a message to the screen, etc.) when a piece of content receives a fifth vote of four stars or greater. That is, a node may have four votes of five stars, and two votes of two stars. As soon as the next greater-than-four vote goes in, we want to do something about it. In our case, the client wants to receive an email when a node is popular, and has had five four-star-or-greater votes.

  • Development

    Show Menu Description Field in Node Add/Edit Form

    Many components in Drupal have an optional title or description field. This text usually displays when a user hovers over an item. We had an interesting request: a client wanted to have a box that displayed text which would change every time you roll over a menu item. This can be accomplished using just a small amount of jQuery, and Drupal’s built-in menu handling. The menu system in Drupal can handle descriptions on each menu item, however you have to do this through the menu admin. The problem is that the client needed to be able to edit these descriptions at will.

  • 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

    Open All External Links in a New Window/Tab using jQuery

    … and to match two urls/domains/whatever: