Drupal

Partying like it's 1999

I just got around to upgrading my website to Drupal 6. In the process I decided that I was going to redo the theme using 960.gs for a nice grid based design and then I got it into my head that I wanted a nice retro 1990 theme. So here it is, my stab at recreating the Netscape 2 experience. I'm not sure how long I'll be able to stand it but I'm going to enjoy it while it lasts.

Creating a CCK field in hook_install()

My rule of thumb for deciding what to post on this blog has been to document anything I've spent more than an hour trying to figure out. Today I've got a good one for anyone trying to create CCK fields as part of a module's installation process.

Back in Drupal 5 the Station module was made up of lot of custom code to track various values like a playlist's date or program's genre and DJs. During the upgrade to Drupal 6 I migrated that data into locked, CCK fields that were created when the module was installed. As people started to install the 6.x version of module I began getting strange bug reports about the Station Schedule that I couldn't seem to replicate on my machine.

Everything is an update

For some work projects we've started making all the configuration changes via update functions. These get checked into version control and from there deployed to the staging site for testing, and then eventually deployed on the production site. The nice thing about update functions is that you can test it on staging and be sure that exactly the same changes will occur on the production site.

Here's a few examples, I'll continue to update it as I get more good examples.

Installing a module

Simple one liner to enable several modules:

<?php
function foo_update_6000(&$sandbox) {
 
$ret = array();
 
drupal_install_modules(array('devel', 'devel_node_access'));
  return
$ret;
}
?>

Themeing a specific CCK field

I wasted more time that I want to admit do trying to figure this out. I was trying theme a specific CCK field named field_images on all the nodes where it appears. The devel_themer module was listing content-field-field_images.tpl.php as a candidate:

But after copying CCK's content-field.tpl.php into my theme and renaming it I couldn't seem to get the theme to pick it up. Roger López gave me the frustratingly simple answer on irc: "i think you need to have both templates in place"... duh. Copied content-field.tpl.php into my theme and everything worked great.

Getting PHP + GD + PostgreSQL working on OSX 10.5 (aka recompiling everything)

Finding myself in need of a PostgreSQL server to test some patches for Drupal core, I've decided to do a follow up to my guide to getting PHP + GD + MySQL installed on OS X.

Fortunately for me John VanDyk wrote up Beginning with Drupal 6 and PostgreSQL on OS X 10.5 Leopard which covers the nitty gritty of getting PostgreSQL server installed. He doesn't address recompiling PHP so I'll pick up the story there.

Last Updated: June 1, 2009

HOWTO: Views 2 Relationships

After getting sick of closing issues in various module's issue queues that boiled down to people not knowing how to use Views 2's relationship feature I decided to make a screencast explaining it:

http://drewish.blip.tv/file/1593750/

I think I need to get a microphone, and figure out all the features of the tool I was using but I'm excited to do more of these.

Media sprinting

So back in April I started talking to Keiran at about doing a media and files sprint... well it's finally happening. aaronwinborn in in Portland and dopry is going to be helping remotely. Aaron posted a great writeup on what we're hoping to accomplish so I'll blockquote at length:

Andrew Morton (drewish), Darrel O'Pry (dopry, remotely), and I are heading up a Media Code Sprint in Portland this week! Come help, in person or remotely, if you're interested in multimedia and Drupal! It has now officially started, and as I've volunteered to help keep folks updated, here goes...

First the reasons.

Number One: Better Media Handling in Core

Dries conducted a survey prior to his State of Drupal presentation at Boston Drupalcon 2008, and number one on the top ten (or 11) list of what would make THE KILLER DRUPAL 7 Release was "Better media handling".

Let me repeat that. Better media handling.

People have done really amazing stuff in contrib, but it is difficult (if not impossible in many cases) for developers to coordinate the use of files, as there is no good means for file handling in the core of Drupal. Thus, we have several dozen (or more) media modules doing some small part, or even duplicating functionality, sometimes out of necessity.

Migrating from D6 upload.module to filefield.module

So building on my last post for creating CCK fields, here's some code I whipped up to migrate from the D6's core upload.module to the filefield.module. This isn't general purpose code but might help someone else out. The catch is I'd built a video node with and was using the upload module to attach exactly two files, an image and a video. The new node will have separate thumbnail and video fields. If you'll be moving to a multi-value field this code won't work for you.

The gist is the same as before, setup your field for video and your field for images then export using:

<?php
var_export
(content_fields('field_web_video', 'video'), 1);
?>
and
<?php
var_export
(content_fields('field_video_thumb', 'video'), 1);
?>

Then roll that into an update function that also moves the file data around in the database. Code is after the jump.

Programatically creating a CCK field in Drupal 6

I spent some time today trying to figure out how to create a CCK field as part of an hook_update_N function. Unlike previous versions of CCK, in 6 it's very easy to manipulate the fields from code.

The first step is to create the field using CCK's UI. Once you've got the field setup the way you'd like it use PHP's var_export() to dump the contents of the node's field as an array:

var_export(content_fields('field_translator_note', 'feature'));

Prefilling the ImageCache cache with wget

Today I needed to get Drupal's ImageCache module to regenerate a bunch of resized images. ImageCache doesn't create the images until a browser requests them and at that point the new image is saved to the disk for future use.

One way to generate the images would have been to just click my way through every page on the site but I'm way to lazy for that. So I used wget:

wget -r -nd --delete-after http://example.com

By using the recursive (-r) and --delete-after switches I was able to have it crawl the site and get all the images generated. Bonus points for running in on the server so that the transfers were via the loopback interface so the transfer didn't count against the monthly bandwidth limit.

DrupalCampPDX

drupalcamppdx keynote I took a break today from my work at Sticky building a website for a major sports apparel company that cannot be named, for DrupalCampPDX. Ben Kaplan and I did the keynote presentation this morning which was definitely an honor. I followed that up with a panel on best development practices which was a bit unplanned but went really well. This afternoon I'm doing a presentation on Views2 and Panels2, I'm a little freaked out on that since I've done zero preparation at this point.

Drupal6: Display block for one node type

This is a little snippet I came up with to get a block to show up on a single node type:

<?php
$menu
= menu_get_item();
if (
$menu['path'] == 'node/%' && isset($menu['page_arguments'][0]->type)) {
  return
$menu['page_arguments'][0]->type == 'story';
}
?>

It uses the node type stored in the menu system so you don't have to match arg(0) etc.

Getting PHP + GD + pdo_mysql working on OSX 10.5 (aka recompiling everything)

This guide walks through the steps necessary to setup PHP on Leopard in order to run the HEAD version of Drupal. The basic steps are installing several prerequisites then recompiling Apache and PHP from source. It could totally bork your system, I'm just writing it down so the next time I need to do this I can remember what I did. I wish I could give credit to all the places I stole bits from but I didn't do a good job of keeping notes early on.

Every time Apple releases a security update it seems to end up overwriting PHP or Apache and I end up revisiting these instructions. Since it's my personal guide I'm continually modifying it to match my current needs. For example, the last big change was adding in old mysql extension and the new PDO-mysql extension. The upside for you, kind reader, is that this keeps the instructions up-to-date. The down side is that when you come back in two months and try to repeat one part of this it may not work because I've changed some of the earlier steps.

Last Updated: June 1, 2009

DrupalCon Day 1

Made it out to Boston last night. I'd gotten upgraded to business class on the flight from Portland to NYC which was a nice treat. NYC to Boston in coach was a bit of a blow to my ego but the scenery made up for it. The highlight of the flight was looking out the window and seeing a white speck fly by, it was three white balloons. As the sun set we were flying through a beautify pink mist.

I'm staying with a friend of my brother's friends. Great place but it's an hour away from the conference center by train/bus so I was up early this morning. I've got a nice tickle in my throat so I hope I don't end up sick.

Here's the presentations I'm hoping to hit today:

Sorting numeric values stored in character fields with views.module

I spent a good chunk of time this morning trying to figure out how to get the views module to sort a character field with numeric data correctly. The audio module has a normalized table of meta-data meaning that there's one column for the tag name and one for the value. The value is stored as a character string which causes problem when sorting numeric data like the track numbers or years. If you've got a SELECT value FROM audio_metadata ORDER BY value that returns the range of numbers 1...13 it ends up sorted as 1,10,11,12,13,2,3...9. The trick as I discovered is to add zero to the field to coerce it to a numeric value: SELECT value + 0 AS v FROM audio_metadata ORDER BY v.

The problem then is to figure out how to get the views module to generate this bit SQL to get the sorting right. The solution I came upon is when defining the field set 'notafield' to TRUE and provide a 'query_handler' to generate the correct SQL. I've included the relevant parts of the audio module below to demonstrate how it works. You can see the complete code here.

Syndicate content