University of Oregon

Drupal – adding debug messages to your custom module – Improved

As a follow up to ‘Drupal – adding debug messages to your custom module‘. I improved the code and wanted to share.
Now you can drop in a few functions and add one line to your hook_admin_settings function to get debugging functionality.
If you want to start with a baseline of functionality you can also update each of your functions to declare themselves in the debug messages. Which is handy for tracking down issues.

In the end you get the same addition to your GUI with control over how and when debug messages are used.
Screen Shot of the new checkboxes
You also get the same reporting output, to the screen, watchdog, or both:
Here’s a quick look at my watchdog log gui with both types of error reporting enabled:
Screen Shot of watchdog screen

All done with cleaner code.

Drupal – adding debug messages to your custom module

Note: There is a follow up post here: Drupal – adding debug messages to your custom module – Improved

I work with a few custom modules and lately I’ve been migrating one from PHP 5.2 to 5.3 & Webform 2 to 3.

It’s been a challenge to sort out where some of the the errors are being generated from so I added a grip of debugging statements. In fact, I used a regex replace to log the name of each function that’s run in hopes that I could track down errors in chronological order.

That worked OK but I though I may want to turn it off or switch from drupal_set_message() to watchdog() calls when it’s ready to go to production. So I wrapped each log item in an if statement and tested for two new module variables:

  • Enable debugging message feedback
  • Enable watchdog debugging feedback

So now I have some GUI control over how and when debug messages are used.
Screen Shot of the new checkboxes

Drupal – Leveraging built in string functions in custom modules

Drupal includes native versions of a few common php string functions; strlen() for example is called with drupal_strlen().
drupal_strlen counts the amount of characters in a UTF-8 string vs. counting the bytes of a string. Not a huge difference perhaps but according to the brief documentation: This is less than or equal to the byte count. So if you want better accuracy then user the drupal_str function

I was introduced to these functions a few years ago, when using the Drupal coder module:

I wanted to quickly prepare a custom module to use the drupal flavored functions today and thought I’d share the regex I used in to find & replace:
(DreamWeaver regex)

([ (.]+)(substr|strlen|strtolower|strtoupper|ucfirst)



A quick replace and you’re Drupal friendly.

Drupal – Add a datestamp to your custom modules

I have several custom Drupal modules and when ever I make revisions & create a new version I update the datestamp.
Here’s a bookmarklet I use in Firefox to generate a datestamp.
Just drag that to your bookmark bar and you’re good to go.

Here are my steps to use it:

  1. Click Datestamp bookmarklet;
  2. Select all;
  3. Copy;
  4. and then paste into your file:
    datestamp = "1354564856"

Here’s the code: (more…)

New custom module to add theme name as body class and cross-theme stylesheet

I’ve created a new custom module for Drupal 6 to help manage styles for elements that exist in multiple themes.
It simply includes two files; one js and one css. The js file appends the theme name as a class to the body and the stylesheet includes some styles for a few different themes.
It could be two separate modules. One to append the body with the theme class, and the other would require the former and would override styles for specific elements.
But since first task is so small I included it in my module.

The jquerymenu module is the impetus behind the custom module. jquerymenu creates an ideal vertical navigation for a number of my sites and I wanted to unify the style of the menu across themes, but more specifics on that below.

The main challenge to building this module was getting the name of the current theme. It’s quite easy to find the default theme but that has little bearing on what theme is actually being used on the page. We may be on page that uses the administrative theme or in a ‘section’ that uses an alternative theme.
I’ve yet to find a server-side solution; though, I’m sure there is one. Now that I mention it, I should try looking inside the sections module to see if I can find any clues.
So my solution was to search the href of all the stylesheets in the document head for a link to a known theme path. eg: themes/ or sites/all/themes. I found in testing that the last one of these is the current theme.