Create a Drupal Photo Gallery Using Flickr, CCK & Views

STOP! This tutorial is outdated. An updated tutorial has been posted here.
Save yourself the confusion and head over there!

There’s an almost infinite number of ways to create a photo gallery with Drupal. I’ve tried many different approaches but never really been all that happy with the results. I’ve finally found a solution worth sharing/repeating. Here’s what we’ll be creating using Views, CCK and the Flickr module.

The flickr module is great right out of the box. It exposes some handy blocks for displaying recent photos and photosets as well as random photos, etc. What it doesn’t do however, is give you a block/page to browse all (or some) of your photosets. What if you only want to display one photoset but not another? What if you want to relate a photo or set to other nodes within your Drupal site? Well, out of the box, the flickr module doesn’t give you this option. So how do you fix this? Well, you use the Flickr CCK field that comes packed with the module – thats how!

Here’s the rundown of what you’ll need to do:

  1. Install/enable Views, CCK, and Flickr (including the Flickr field)
  2. Create new “Photo Set” node type
  3. Some theme overrides in template.php
  4. Drink a beer
  5. Implement an overlay/lightbox plugin of your choosing
  6. Use views to generate a page display for your new node type
  7. Drink another beer and enjoy your gallery!

The flickr module requires that you apply for a Flickr API key. Don’t worry, its free and you can get yours here. Add your API Key and Shared Secret to your installation at Admin > Settings > Flickr. You’ll also want to set a Default Flickr User Id on this page. If you don’t know yours off-hand (and who would?) you can get yours using idGetter.

Note: As my good pal @mrtnyeti pointed out, you must enable the Flickr Sets module that ships with the Flickr module as well. Without it, this technique won’t work.

All right, so once you’ve got Views, CCK and Flickr all enabled and ready to do your bidding, you need to create a new node type – call it whatever you want, I’m calling mine: Flickr Photoset

Now add a flickr field to this node type. Call this one whatever you want as well. I’m calling mine Flickr ID. Once you’ve got this done, create a few nodes – you’ll see you can specify a photo or a photoset. For this example, we’ll only be using photosets. You can grab the ID of a photoset by visiting that photoset on Flickr and grabbing the number following “sets” in the URL.

Note: You may not see anything being pulled from flickr after creating a node of this type. The quick fix is to head over to the “Display Fields” tab under the content type administration section. Click save without making any changes and you should see some output from Flickr.

Once saved, you’ll see that the default output really isn’t all that helpful or interesting. We don’t get the desired result at all. That’s where theme overrides come in. In this first override, we’ll alter the default output for a flickrfield photoset. Add the following code to template.php.

<?php
function phptemplate_flickrfield_photoset($img, $photo_url, $formatter, $photo_data, $node) {
  if (arg(0) != 'node') {
    $options = array('html' => true);
    return l($img, 'node/'.$node->nid, $options);
  } else {
    return theme_flickr_photoset($photo_data, $photo_data['owner'], $formatter);
  }
}
?>

Credit where it’s due: PETER TÖRNSTRAND got me started on the right path for this tutorial. I’ve made one alteration to his override though. I threw in a conditional for non-node pages – this way we get a simple thumbnail link on pages that aren’t node pages. Things like views pages or taxonomy term feeds. Once you’ve got that override in template.php, you should see a complete photoset being rendered when viewing the node – with the thumbnails linking to their homes on Flickr.

We can take it one step further though. Instead of linking directly to Flickr, we can pull the image asset into our page and display it using a trendy lightbox. I prefer the ColorBox jQuery plugin, but you can use whichever plugin you want. Just adapt the following theme override to suit your needs:

<?php
function phptemplate_flickr_photo($p, $size = NULL, $format = NULL, $attribs = NULL) {
  $img = flickr_img($p, $size, $attribs);
  $photo_url = flickr_photo_img($p, NULL, $format);
  $title = is_array($p['title']) ? $p['title']['_content'] : $p['title'];
  return l($img, $photo_url, array('attributes' => array('rel' => 'photos', 'title' => $title), 'absolute' => TRUE, 'html' => TRUE));
}
?>

Note: If you’re going to use ColorBox like I am on this site, you’ll need to upgrade your jQuery to 1.3 – the current version of Drupal (6.15) doesn’t ship with this version. I spent way too much time trying to debug this…but I digress.

If you need help installing the jQuery for your lightbox plugin, see my post on “How to Add Custom jQuery” from my Drupal 101 series.

The last step is to create a simple view to display a listing of your new node type. I set one up at “/photos”. I’ve attached the exported version of my view to the bottom of this post.

So, why is this solution better than what ships with the Flickr module? One word: Flexibility. Like I mentioned at the start of the post, you don’t have much control over what output you get with the Flickr module. This method allows you to not only choose which photo sets you display on your Drupal site, but also ways to tie your flickr sets to other nodes or data on your site. You could for example, add a node reference field to this node type to tie photo sets to other node types (pages, stories, events, etc.). You could also leverage other modules like Views or Taxonomy to create simple searches or browse pages. Get my drift?

My Gallery

If you’ve got a question – drop me a comment. I’m all about spreading the Drupal/Flickr love.

Happy Drupaling ninjas!

AttachmentSize
photo_gallery_view.txt3.56 KB

Comments

Really steps 4 and 7 are the most important!

I agree Dave!

Hi Chris, Nice tutorial! I recently built something very similar on my site, but wanted to include a link to the Flickr "slideshow" feature. I ended up writing a brief tutorial on how to achieve this: Implementing Flickr Slideshow Links By Theming Flickr.module. Take care, Eric

Thanks Eric - I plan on implementing the feature from your tutorial - great idea!

Thanks Chris, this was very helpful!

I'm pretty new to working with drupal and I built my template using a tutorial from Lynda.com. My question is I don't have a template.php page in my theme. Can I put the code somewhere else so it will work with displaying the whole gallery? Also, do I have to have a template.php file? I'm not sure but it looks like other themes don't have one. Thanks.

Thanks for posting! As for your questions, no - template.php is definitely not required. It is however, the place where you utilize the theme overrides I have listed in the post above. If you don't have one, just create it! It's just a php file where you can store your theme overrides, preprocess functions and custom functions of your own. You can read more about it on "Drupal.org":http://drupal.org/node/171194 - if you have any other questions, fire away.

Thanks. I'll read up on it but quick question - i did try to create a template.php file but it didn't do anything. does the file need to be somehow 'called' by the system or does just having a template.php file in the theme folder work?

andvines99 - the template.php file needs to be contained within your theme folder to be recognized. If you are doing a theme override (like those in this post) you may need to empty your Drupal cache to ensure that they get recognized. Hope this helps!

Thanks I'll try that. Thanks for the tutorial.

Thanks Chris! This really is a great way of saving 'upload' time - just point to your flickr account. Nice. Up until the Colorbox point, everything works well for me. 1) I replaced my jquery.js with version 1.3 (in the /misc folder) 2) I downloaded and activated the colorbox module http://drupal.org/project/colorbox 3) I added the second code snippet to my template file (the first one worked fine, so I'm pasting in the right place) ..but - now instead of an image opening in Flickr after I click on the thumbnail, it opens in the browser window, as a 'standalone' image -i.e. just the image on it's own, clearly not being wrapped/presented through Colorbox (http://farm5.static.flickr.com/4054/4410508137_abaefc0546.jpg) Do I need to modify the second code snippet with anything relevant to my specific installation, or is there some other way I can be sure that Colorbox is being used to present the images? Thanks again for the awesome tip Cheers, Chad

Hi Chad - Thanks for reading! It's really hard to say what might be causing your issue without seeing the code. My guess is that the new version of jQuery isn't being included properly. Have you cleared your cache? My recommendation would be to use the DEV branch of the jquery_update module to upgrade to jQuery 1.3. Replacing core files (/misc/jquery.js) is a big no-no. Hope this helps.

Hi Chris, Thanks for the reply. I've replace the jquery in /misc - thanks for the guidance. Just to be sure - the second code snippet you provide in your tip. You mention that I should modify it according to my needs. If I'm using Colorbox (like you) - do I need to change anything? or can I use as is? I don't see any path or other environment specific 'things' that I need to change... Cheers, Chad

Chad, You should be able to use my code as is. Happy Drupaling!

Hey Chris! Thanks for the reply. This may be a really dumb question, but do I just include the code snippet for the theme override (the second snippet in your post) with no modifications at all if I'm using Colorbox? I get the feeling that colorbox is not being used to display the images.. Thanks Chad

Great tutorial! Is there a way for drupal to automatically look out for new flickr photo sets and pull them in without creating a new node all the time? I've found a bundle that does this using PHP however nothing yet that works with drupal. Cheers, Jamie

HI Jamie - the flickr module that I'm using in this tutorial will do this for you. Once enabled, it creates a page at /flickr I believe. Additionally, it creates a few blocks that will automatically pull photos from your selected account. Head over to the block administration page and you'll see these blocks.

Chris, Thanks a million for taking the time to share this info. I'm attempting to teach myself Drupal to replace my existing photo site. Apologies with the noob questions. When you say to create a 'new node type', is this the same as creating a 'new content type' or do I actually need to create a new module set with a .info and .module file? If so, should the .module file be created with the txt file you attached? Thanks, I appreciate any guidance!

Hey there -- yes I mean create a new Drupal Content Type. Node type and Content Type are interchangeable. Thanks for posting!

I've managed to get this working, but with Thickbox. I'd prefer to use Colorbox actually, but have not had much success. I've installed the jQuery update for Drupal 6.x, and nominated the .js file in my template.php for my theme, but it just doesn't work. Any ideas? T.

I believe you need to use the dev branch of the jQuery update module to get the proper build of jQuery for use with Colorbox.

Nice walkthrough. I've gotten so far as creating a new node and adding a photoset id to bring in a photoset. I understand about creating a view to display all nodes in one page. Here's my problem. I want to display all photosets without having to add them individually. Do you know how the Flickr module can do this? I'm assuming the api can do it. Thoughts?

I don't think the Flickr module provides this functionality right of the box. I'd checkout the Flickr API documentation...

Thanks for sharing your ideas with the world...!

Thank you so much for putting this together. There is a serious lack of documentation for these modules and you nailed it.

Thanks! Glad you found it helpful.

I love this idea. I've got a client who has an extensive flickr collection and they need it displayed on their website. I followed all of your instructions, but I'm getting two errors when I create a "Flickr Photoset" node and try to view it. I get a "Photoset not found" and a "User not found" error every time. I've triple checked all my info on the flickr config page (my API Key, Secret, and username). What could be the problem?

hmm, Tiberius...that's a tough one. Perhaps the photoset id is incorrect? Maybe you copied it wrong?

So I just tried entering the id of another photoset ... The good news: it works! The bad news: it works for some sets but not for others. Have you heard of this happening?

Nevermind! I just realized that the photoset I was trying to load was set to restricted permissions on flickr. Duh!

Hey Chris, thank you so much for sharing this. I'm super new to Drupal and I don't have very much experience using any kind of code, so I imported your view code, but get this error message: * Field handler node_data_field_flickr_id.field_flickr_id_id is not available. * Unable to import view. I used exactly the same names (ie: "Flickr Photoset" and "flickr_photoset") so I don't think that it's looking for fields, etc. that don't exist. Anything you can do to help would be great! Thanks

Hi Stefan - you aren't the first to have this issue. I'm wondering if this is a module issue. Perhaps newer versions of views or the Flickr module have broken my view. I would suggest just building the view from scratch. If you need any help with that, just let me know!

Like the others, I'm in the same boat with the 'Field handler node_data_field_flickr_id.field_flickr_id_id is not available' error, which is a little frustrating as I feel like I'm so close to having what I need from your tutorial... : ) Any tips on rebuilding this view from scratch, or perhaps some thoughts on how to fix the broken code to comply?

I am not seeing any difference when I add the new code to the template.php file for my current theme. It's just a single square photo pulled from my Flickr set. After I add the code, I'm supposed to see all Flickr photos from that set on the node that I've created, right? I'm also getting the same error as Stephan when trying to import the view... potentially this is because I didn't do the fancy box step? Thanks in advance... I'm happy that I got as far as I did. LOL.

I just figured out a problem I was having. When you're creating the Flickr ID field, under the "Type of data to store" dropdown, there are two options "Flickr photo" and "Flickr photo set". The template.php modification wasn't working for me, and it was because I was selecting "Flickr photo set" from that drop down. Make sure you just pic "Flickr photo." I'm still getting the "Field handler node_data_field_flickr_id.field_flickr_id_id is not available." error when trying to import the view though.

That's a good distinction Jared - and thanks for posting. The error that you're seeing -- I'm wondering if it might be a versioning issue. What versions of views and the flickr module are you using? I'm thinking an update was made to one of these modules that breaks my view that you've imported.

Hi, I'm trying to just get the basic flickr module running before I attempt what you have done. I apologize if the answer is really easy. I installed the module, configured the api, and when I write the [flickr-photoset:id=72157624073349011,size=m] code in the content node, I get the "error 1, photoset not found" error. I checked the handy site at http://www.flickr.com/services/api/explore/ and it only works when I select "Sign call as with full permissions" selection. If I try the other two options, I get the same error. Can you tell me if I'm missing something? thanks! Dave

Hi Dave - this sounds more like a permissions issue with Flickr. Are your images public?

Yes, i set them to public. They still won't display. I tried the flcikr attach module and they show up in the create content window, but won't display when the page is published. Its maddening.

Thanks for the guide. This will be great if I get it working. I have followed your guide, but cannot get the changes to template.php to take affect. I am only seeing the single photo instead of the full set. I am using a Zen subtheme and am adding the code to the bottom of the template.php file. It looks like another person may have had this problem, but I didn't find an answer. Any help would be awesome. Thanks!

jbb, did you be sure to select "Photoset" when you created the new node? This module lets you embed single photos or photosets. To use the same technique that I've demoed here, you need to select the photoset option.

i have the same problem and i have selected "Flickr Foto set". I try to override the function in my template.php (garland) but nothing happens.

I'm having issues getting colorbox to work, similar to Chad's post earlier where my flickr pics open in a new window with no colorbox, etc. I have colorbox unzipped in sites/all/libraries and installed the modules colorbox 6.x-1.x-dev and jquery_update 6.x-2.0-alpha1. I have also tried the dev version of jquery_update with no luck. Just for fun, I tested the setup with lightbox2 tonight and it worked. Any ideas to help me get colobox functioning? Thx.

I'm having issues getting colorbox to work, similar to Chad's post earlier where my flickr pics open in a new window with no colorbox, etc. I have colorbox unzipped in sites/all/libraries and installed the modules colorbox 6.x-1.x-dev and jquery_update 6.x-2.0-alpha1. I have also tried the dev version of jquery_update with no luck. Just for fun, I tested the setup with lightbox2 tonight and it worked. Any ideas to help me get colobox functioning? Thx.

Hi Brad - it sounds like you're having either an issue with Drupal using the correct version of jQuery (colorbox requires a separate version than the one that ships with Drupal) or there is a syntax issue. The DEV branch of the jquery update module may no longer be using the correct version of jQuery that the most recent version of ColorBox requires. I think Colorbox recently released an update which may require the newest version of jQuery. I would start there.

Thanks for the note. I attempted this with several different versions of jQuery, but never got it to work. I don't think it's a syntax issue because I tried it with lightbox and it worked fine. I'm giving up for now. If anyone gets this to work with the newer version of jQeary, etc. please post which versions you used so I can try this again. Thx.

So I have this up and working, but my question is, how can I make the "teaser" view only show like 10 photos and below those have a "see all photos" link to the node? Then, if you're viewing the node view of the photoset, have it show all the photos.

Absolutely fantastic tutorial...just what I was looking for. I was just just wondering if anyone had created a block from this view that would display a single thumbnail from the most recent photosets. I tried modifying the template.php changes in the tutorial, but I seem to be doing more harm than good. Thanks again.

Hi Chris This article is really helpful, especially given the distinct lack of documentation on the flickr module in general. I'm running drupal 6.x and have followed your steps to create the new node type with the flickr field. Then I've craeted a new node to test that. My page will only display the first image of that set, and doesn't display that as a link or anything else. I've tried playing around with this but really don't know how to solve this. Can you help? The page it's on is here: http://www.david-paul.co.uk/node/12 Thanks David

Hi David - thanks for your comments. The flickr module has changed slightly since I wrote this original tutorial. I think this is what's causing issues for you (and others). I'll look into updating the tutorial soon. Are you sure you've selected "Photoset" as the type for your node?

Pages