New blog

On Sunday afternoon I looked at my list and saw “clean litterbox”, “put away laundry”, and “scan papers”... so I opened my laptop and made myself a new blog.

I’ve imported thousands (really) of posts from my old blog, and I would like to get back to that style—a mix of brief obsessions, answers to my own tech questions, and mini-projects—but with more Drupal. I have a few posts in mind elaborating on the process of building this, but I’ve already written about a few features of the new blog in the colophon.

A few notes on old posts: There is a lot of link rot. They never had comments, and are generally too old to warrant them anyway. Some posts may be missing images, embedded objects, code highlighting, or other formatting. If I notice, I’ll fix them.

DrupalCon Paris video

I'm going to Paris this September for DrupalCon; as a professional Drupal developer it is an extremely useful event, and as a full-time web geek it's a great place to socialize with other people who do the same kind of work. I would also like the chance to present on some of the Drupal-as-a-GeoCMS work that has been happening this spring in front of a large audience who will ask sharp questions and then go use our work in their own projects.

I've entered a video contest for a conference ticket refund-- I've been wanting to make a movie with these toys since Easter:

You can vote for me on the DrupalCon Paris site and by rating my video on YouTube. You can also see the other contest entries listed as video responses under the original contest announcement.

php function of the day

Fairly often in Drupal I get handed arrays that have significant keys, but the values are either a label or false-y. I generally don't care about the items with empty values, or I specifically want to remove them. The solution is to use array_filter($arr) to remove any array items with false-y values. In fact, sometimes I don't even care about the labels at all, in which case I might do array_keys(array_filter($arr)). Examples below.

  1. $arr = array(
  2.   'key1' => 'Label 1',
  3.   'key2' => 'Second Label',
  4.   'key3' => 0,
  5.   'key4' => 'Label No. 4',
  6.   'key5' => 0,
  7. );
  9. $arr_filtered = array_filter($arr);
  10. /*
  11.    $arr_filtered = array(
  12.      'key1' => 'Label 1',
  13.      'key2' => 'Second Label',
  14.      'key4' => 'Label No. 4',
  15.    );
  16. */
  18. $arr_filtered_keys = array_keys(array_filter($arr));
  19. /*
  20.    $arr_filtered_keys = array(
  21.      'key1',
  22.      'key2',
  23.      'key4',
  24.    );
  25. */

array_filter() can also take a callback as the second argument, which lets you filter out items on your own terms. See the array_filter() documentation.

flat footing

My aunt shared these two "flat footing" videos on facebook:

Drupal Field Formatters

I develop with Drupal 6 at work. One of the things I've been finding useful recently is writing field formatters. Field formatters are handy because they're used to format CCK field content when it gets displayed in nodes and also to format fields in Views. You can choose between formatters for each field of a node type under the 'Display' tab in the node type's settings. In Views, if you choose a display type that uses fields, you can select a different field formatter when you add each field.

To write your own field formatters, you implement hook_field_formatter_info(), then create theme functions for your formatters and add them to your hook_theme() implementation. At one point this was documented in examples packaged with CCK, but it seems the examples are no longer part of the CCK download.

A simple hook_field_formatter_info() implementation looks like this:

  1. /**
  2. * Implementation of hook_field_formatter_info().
  3. */
  4. function example_field_formatter_info() {
  5.   return array(
  6.     'myformatter' => array(
  7.       'label' => t('My Formatter'),
  8.       'field types' => array('text'),
  9.       'multiple values' => CONTENT_HANDLE_CORE,
  10.     ),
  11.   );
  12. }
  • Your formatter should have a unique handle, in this case "myformatter". Using "x_formatter" or "x_module_name" here makes the theme key name confusing.
  • The 'field types' array contains the names of cck field types applicable to this formatter.
  • 'multiple values' determines the way field content is passed to the theme function. It may be either of two values:
    • CONTENT_HANDLE_CORE means that the formatter is called separately for each value in the field. If a field contains multiple values, then the formatter is called multiple times. Field content is passed to the formatter in $element['#item']. This is the default.
    • CONTENT_HANDLE_MODULE means that the formatter is called only once for a field, and the theme formatter gets all the field values at once. In this case, field content is passed to the formatter in $element[0]['#item'], $element[1]['#item'], $element[2]['#item'], etc. Note that the $elements array contains other information about the field, such as $element['#field_name']

You will need to add the field formatter theme function to your hook_theme() implementation:

  1. /**
  2. * Implementation of hook_theme().
  3. */
  4. function exmaple_theme($existing, $type, $theme, $path) {
  5.   return array(
  6.     'example_formatter_myformatter' => array(
  7.       'function' => 'theme_myformatter',
  8.       'arguments' => array('element' => NULL),
  9.     ),
  10.   );
  11. }
  • The theme array key, in this case 'example_formatter_myformatter' is constructed from {module name}_formatter_{formatter name}.
  • You don't need to include the 'function' => 'theme_myformatter' line; the default theme function name in this case would be 'theme_example_formatter_myformatter'.

The formatter function itself should return a stuff that is ready to output to the browser:

  1. /**
  2. * Theme function for myformatter from hook_field_formatter_info().
  3. * @param $element
  4. *   Array of formatter info and the item to theme. Field contents will either be
  5. *   in $element['#item'] or $element[$i]['#item'].
  6. */
  7. function theme_example_formatter_myformatter($element) {
  8.   return strtoupper($element['#item']['safe']);
  9. }

This field formatter simply displays the contents of the text field in all caps. You'll need to clear your cache before your new formatter shows up in the field display choices.

birthday list

I've been trying to think of a snappier way to introduce my birthday list this year, but nothing is surfacing. So: I'm turning 25 next week, this is a list of things I want for my birthday. If you send something via email, put "birthday list" in the subject line and I will not open it until the 18th.

  • a picture of you and your cat (or Mabel)
  • a cookie recipe
  • a long quote
  • a code snippet via twitter
  • a pattern
  • a café recommendation near my work in chicago