C'mon Drupal: Make Displaying Images Easier with Representative Image

Sometimes Drupal makes me bang my head on the desk. It can perform all these incredibly complex tasks and support countless technologies but occasionally it's the simple problem that flumuxes it. For a long time I've been banging my head against just such a problem: lets say I want to create a list of content containing different types of content each with an image.

"That's simple" you say, "just add the same image field to the different content types, fire up views and your set." The problem is that adding the same field to all of my content types is not very conducive for using features… things get complicated pretty quickly when you share fields like that. Not to mention this would be a disaster if I were using a distribution like COD or Panopoly.

"No problem" you tell me, "just whip out some Display Suite, build some view modes and quit your complaining." Okay, okay… that's actaully a pretty good solution in a lot of cases, but lets say I want to replace the default admin page at /admin/content so that it includes a thumbnail next to each piece of content. This is a perfectly legit use case, but you'd be hard pressed to get a table view working by using (the stupendous) Display Suite module.

Set it, and forget it

Just as my head was meeting my desk yet again because of this issue I noticed a new sandbox project on drupal.org (from alberto56) that helps to solve this. Hishad an interesting use-case as well; he wanted all of his content (regradless of type) to have an image displayed in his OpenGraph Metadata. He needed a token that would work consistantly across content types. And thus the Representative Image module was born. At its most simple this module allows you to choose (on a per-entity bundle basis) what field should be used when ever Drupal needs an image for the given entity.

Imagine a photo gallery content type with  "title", "blurb", "cover image", and "gallery images" fields. In this case, just set this content type to use "cover image" as it's representative image and you're all set. Or let's say you have added an "avatar" field as well as a "photos of you" field to the user entity (by default this only has one bundle also called user). Set the "avatar" field to be the representative image and you're done. Now you can create a view with the "representative image" field and it will always work with all entities; now you have a "representative image" token at your disposal and your OG Tags will reliably have an image.

But wait swoosh, swoosh, there's more

Order now and you'll also get these great new features:

  • Media module integration - if you use media for your images you can choose that field instead of a traditional image field.
  • Default images - if a piece of content has no image you can set representative image to just find any image attached to that entity or to use the site logo instead. This way you can 100% garuntee an image is returned. (well, this is almost done: http://drupal.org/node/1988658)
  • Extensability - there are some hooks available to give developers fine-tuned control of the representative image being returned. (see http://bit.ly/18Z3w1X for details)