University of Oregon

Drupal DB backup bash script with Drush and Git

I like to make incremental backups of my drupal databases. To do that I’ve tried combine the best practices* into one bash script that I can run periodically and easily.
The end result is a bash function that’s easy and user friendly.
It works by leveraging Drush aliases and the drush sql-dump command in combination with git.

Usage is simple. From the command line:

$ dg_db_backup drush.alias "commit message" 

Note: dg_db_backup -> drush git database backup

Drupal 7 CCK – Creating ‘Label Bulk Operations’

I have a new D7 site in the works and I found that I wanted the majority of my content type field labels to display: inline as opposed to the default: ‘above’.
Several of my content types have dozens and dozens of fields to I wanted a quick way to automate changing all the selectors at once.

I suppose ‘repetition and redundancy are the Aunt and uncle of invention’.

Here’s what I came up with:
image of the new fieldset and buttons

Branching with Git – keeping remote and local sync'd

Max pointing me to his method of branching, inspired by:

Which creates a branch on the remote origin and then pulls it down and tracks it locally.

The author created a bash script and I wanted to make that a function to keep in my .bashrc file;


Mailman as a bulk mailer – imported member managment tips

I have 20000 email subscribers for two email lists (cjobs and ucjobs). These were all imported into our new Mailman service.
I had a few settings I needed to change to accommodate using Mailman as a bulk mailer. And it’s going pretty well.
First there were normal things like pre-pending the default email subject, updating the list name and bounce protocols, making sure no-one can post to the list except the one authorized user (bulk moderate every user).
That was easy.

But then there’s a default setting where any list member can view all the other members of the list… not good.
First I updated the html docs, removing the relevant info.
Then I needed update all the users; but there’s no bulk operation for that.
Instead there’s a Member Mgmt screen with columns of check boxes. CheckBoxMate to the rescue…
Almost; Mailman displays a max of 30 members at a time… (20000 / 30 = ouch), that’s a lot of clicking and dragging. (more…)

Updated the 'featured nodes' view for each book

I created a new block display in our frontpage_book_section view to display a more customized output.
Previously we had boxes with each ‘promoted’ node showing the title, teaser and ‘Read more’ link.

That worked fine in most cases but when we had a node with no teaser or body content those boxes were empty.
So I created a new block display that replicates the previous node output and also supports this ‘no teaser’ case.
When the view encounters a node without teaser content it displays all the ‘child links’ for that node instead. Which works great. The primary scenario where we have no teaser are ‘link pages’.
There are still cases where some users want to deliberately keep their featured boxes blank, even when there’s no content and in that case they can add an empty teaser to the body and the child links wont show up.

Here’s an example of the new featured boxes:
The current featured blocks don't have any blank cells

The bottom right box used to contain only the node title and left an empty impression on the user and now it’s displaying the child links.

Technical stuff:
That was done with Customfield: PHP code. I added some php to test for an empty teaser and call another view displaying the child links if need be.
Here’s the code:

node_revisions_teaser == ""){
  print views_embed_view('frontpage_book_section', 'attachment_1', $data->nid);

It works great.

Drupal 6 – Add css with drupal_add_js

I’ve used this method for years and I wanted to post a complete example as a reference.

I love the css_injector module but I like to keep my views’ css with my view.
So this code example has all my required elements for inserting css into a views php header;

  • CSS is readable during editing thanks to php’s HEREDOC
  • function are not re-declared
  • css is minified (Mostly, I keep the white space…)
  • the Drupal.behaviors syntax that ensures code is only ran on the page load
  • and it leverages drupal_add_js

Here it is with in-line comments: (more…)

Oracle SQL Custom Order by using Decode

I have a query that returns all employees plus their supervisors (based on reviews from the prior 5 years) then orders by

  1. UOID and
  2. Contract type (Primary, Secondary, Other) and then
  3. Review date

So if the employee has multiple positions we get the primary position first.
If they have had multiple reviews the latest one floats to the top.
Now we can use the first row returned for each EE to determine

  • their current primary org. and
  • their most recent supervisor (if there’s a review on file)

To do the custom order by I used the decode function on the employee’s contract type. *Thanks to (More Complex DECODE), and (Ordering With A Decode)
Here’s the whole order by line:


but let’s isolate the fun part:


Here I used decode like a case statement:
Switching on the contract_type;
if ‘P’ (Primary) then return ‘A’ for sorting
If ‘S’ (Secondary) then return ‘B’ for sorting
If ‘O’ (Other) then return ‘C’ for sorting
Default case: return ‘Z’ for sorting

It works great.

Redirect an entire Drupal site – .htaccess

I’ve migrated several Drupal sites to new servers lately and since some of those sites were in development they didn’t have a proper cName. So I’ve been using a .htaccess redirect to channel all traffic from the old site to the new ones.
Here’s an example of the new .htaccess file I placed in the /hr_diac directory on my old server:
##Redirect everything that pointed to /hr_diac to$1

RewriteEngine on
RewriteCond %{REQUEST_URI} /hr_diac [nc]
RewriteRule ^(.*)$$1 [r=301,nc]

To clarify; I completely replaced the drupal .htaccess file with this file.
It’s pretty simple but it works.

Drush Make post processing – Adding module type

We’re using drush make files to keep our Drupal sites updated.
Aegir will build a platform (base install for a multisite) from a make file. I believe it also re-builds the platform from that makeFile each time the platform is verified.

This is pretty handy but requires some leg work up front to generate the makeFile. is the go-to site to get a fresh makeFile built.
Though I just looked at the drush help file and saw that there’s a drush make-generate command ($ drush make-generate myMakeFile.make) that

Attempts to generate a makefile from the current Drupal install, specifying project version numbers unless not known or otherwise specified. Unversioned projects will be interpreted later by drush make as “most recent stable release”

So I gave it a shot and it did a great job.

Dealing with php 5.3 undefined index errors

I had an old php custom report that pulls data from a mysql db and I was getting “undefined index” errors when we moved to PHP 5.3.
This function helped:

But I also had to pre-set an array with all the default values.
Before I had

$totalColumn = array();

but now I had to set the default value for each column:

$totalColumn = array("minority_check" => 0, "count" => 0, "applicant_total" => 0, "Men_total" => 0, "Women_total" => 0, "men_women_total" => 0, "percent_of_women_to_total" => 0, "white_total" => 0, "white_plus_other_race_total" => 0,
	 "white_plus_other_race_AND_hispanic_total" => 0, "white_AND_hispanic_total" => 0, "multirace_AND_hispanic_total" => 0, "protected_class_AND_hispanic_total" => 0, "protected_class_but_not_white_AND_hispanic_total" => 0,
	 "declined_race_but_hispanic_total" => 0, "declined_to_ident_total" => 0, "declined_to_id_hispanic_total" => 0, "declined_to_ident_race_with_hispanic_total" => 0, "declined_to_ident_race_and_hispanic_total" => 0,
	 "declined_to_ident_race_OR_hispanic_total" => 0, "protected_class_total" => 0, "hispanic_total" => 0, "minority_total" => 0, "known_race_OR_hispanic_total" => 0, "percent_of_minority_to_total" => 0, "white_undeclared_minority_total" => 0,
	 "non_hispanic_total" => 0, "asian_total" => 0, "pacific_total" => 0, "black_total" => 0, "native_american_total" => 0, "multiracial_total" => 0, "multiracial_and_or_hispanic_total" => 0);