Latest Examples & How Tos

  • Delete comments and disable commenting on all nodes of a particular content type

    Please TEST these queries before running them on a live site! I do not want to be held responsible if something goes wrong ;)

    Here is how a colleague explained his situation (the solution follows):

    The trap is that if you create a content type and don’t remember to set comments off, all the nodes you create have to be updated manually to turn it off, even after you change the default in the content type settings. In our case, I imported a ton of records, and only later realized comments were on.

  • Cache-related WSOD! HELP!

    Well, you've probably all had a White Screen Of Death a time or two while dealing with Drupal or PHP. They can be very frustrating, but are usually easy enough to resolve. The following illustrates how to recover from a Drupal WSOD as a result of making changes to your cache settings in the Site Configuration -> Performance admin page. Again, this "fix" should only be applied if you have issues that you know are related to recently altering the cache/aggregation/compression settings.

  • User Access Example #1 - A Mostly-Private Site

    We recently completed a site that offers only a small amount of anonymously-accessible content. I want to share the setup with you, as I feel it's pretty effective. The basic structure is as follows:

    1. Anonymous users can only see some pages (the ONLY content type they can ever see is Page; though this could change)
    2. Authenticated users can see most content, across most content types
    3. Client Admin users can see/edit everything
    4. If an anonymous user gets an "Access Denied" page, redirect them to the user login page, but maintain the original destination

  • Introduction to FirePHP

    FirePHP is a great tool for debugging PHP, and maintaining some level of control over your error handling. Also, it's a great tool for getting information about your variables, objects, etc. while your PHP code is executing. FirePHP requires only a small amount of setup and can be reused time and time again without tedious setups for each site on your server. This tutorial/example requires Firefox.

    Example FirePHP
  • Automatically Change Links from Absolute to Relative

    In this example I'm showing one way to quickly convert all a href and img src paths from absolute to relative. It's quite a time saver, but I suggest commiting your latest changes before trying anything here! If you aren't using a version control system, make a backup somewhere... please!
  • Changing the Title of a Node Add/Edit Page

    The following example shows one way of changing the title on add/edit pages for a specified content type. We'll accomplish this using hook_form_alter() in a custom module.
  • Using Vimdiff with Git

    The following examples show how to use vimdiff and git-show to view the differences between two versions of a file (that is in a git repository). Note that you must run these commands from the root of the git repository.

    Vimdiff with the peaksea colorscheme
  • Automatically Stage All Deleted Files (Git)

    Tab-completion is a really nice thing that we often take for granted. While working with Git I've found that it becomes inconvenient to stage (add for inclusion in the next commit) removed files using git rm path/to/my/file.php. Tab completion doesn't work on paths that no longer exist, so you have to manually type the path to the deleted item. The following snippet automatically stages ALL removed files.

    git ls-files -d -z | xargs -0 git rm --cached --quiet

  • Hiding Fieldsets in a Form using hook_form_alter()

    The following examples shows how to hide fields/fieldsets in forms using hook_form_alter(). I suggest using the devel module to determine how to target your field(s) or fieldset(s). You can add a dpm($form) to the function during development and testing.

  • Adding a Custom Token to the Ubercart Email Template(s)

    In this post I'll show how to set up an extra token for use in an Ubercart template. This will require creating a custom module (because we don't really want to modify others' modules). We'll use a few different hooks to create the token, and then simply modify the template to include this token. This setup requires the token module (which is required by Ubercart), so make sure this is enabled!

  • "755"-style Permissions with 'ls'

    After a quick Google search for "ls permissions octal" I found a very handy alias to put in my .bashrc. Not only is it handy to see the OCTAL value of the permissions for each file/dir, it will undoubtedly help you more quickly recognize/interpret the normal ls -al output.

  • Altering the basic properties of a Drupal textarea

    The following example, when placed in your theme's template.php file, will shrink the size of all of the "body" textareas on your site to 5 rows, and set the textarea as resizable. The function we're using to alter the textareas is theme_textarea().

  • Custom Image Link in a Drupal View

    The following code can be used in a views_customfield php field to create a custom image link. This example also shows how to use the db_query() function with a proper %-modifier.

    Custom views image link
  • Using module_list() to Show Active Drupal Modules

    This code can be used in a custom module, a template.php file, or most easily in a block. This looks good in the header bar on a demo site, without a title, as you can see in the screenshot! The reason I am using this is because I do a lot of Drupal demos and it's great to have the active modules showing so there is no question of what's required to do whatever it is I am showing. The list of modules is ordered by weight, then filename, which makes it easy to determine if your custom modules are running at the right times.

    Showing active modules in a block
  • Better Page Titles in Active Collab

    I've written a new Greasemonkey Script to make the page titles in Active Collab much more descriptive.