Create a Drupal Photo Gallery Using Flickr, CCK & Views, Take Two

Back in February, I wrote a quick tutorial on how you can leverage Views, CCK and the Flickr module to create a photo gallery in Drupal. It is by far the most popular post on this humble site of mine (garnering about 36% of my traffic). Long story short, the Flickr module was updated (for the better) and the tutorial was outdated. The technique I described in the original tutorial was no longer correct. So, people following it (trusing that I knew what I was talking about) couldn’t replicate the results. This post is long overdue but I hope it helps clear things up on how to get the same results with the new Flickr module.

I recently whipped up a simple site for a cross country team I help coach that demos this functionality nicely. Here is an example gallery page – this is what we’ll be creating.

For the most part, the technique is the same, but I’ll run through all of the steps quickly below:

  1. Download and install Views, CCK and the Flickr modules
  2. Enable Views, CCK and the following Flickr modules
    • Flickr
    • Flickr Sets
    • FlickrField (under the CCK package)
  3. Configure the Flickr module settings at Site Configuration > Flickr
    • Enter your Flickr API Key and Shared Secret (you can get yours here if you don’t already have one)
    • Save your changes. Doing so should return your Flickr ID and populate it for you inside of Drupal. If not, you can get yours using this nifty tool
  4. Create a new content type to house your photo galleries from Flickr, I called mine “Photo Gallery”.
  5. Important – Add the Flickr field for capturing the photo set id:
    • Under manage fields for your new content type, select Flickr Photo as the type NOT FLICKR PHOTO SET – the photo set field only returns the primary or cover photo for the entered photo set. If you want a gallery of all of your images, you need to select the Flickr Photo Field type. Go ahead and save the changes and customize the field as you see fit. I made mine required. This is where most of the confusion came from – I believe this was a change from the module I used when I originally wrote the tutorial up.
  6. Now, head over to the “Display Fields” tab. You can make changes here as to how your field will display your images. I left the defaults, but you need to click “save” either way. Apparently, this forces your images to begin displaying on your nodes.
  7. Create some nodes. When you do so, you’ll see “Item Type” is an option under the Flickr Photo Set field we created earlier. Be sure to select “photoset” and then fill out the ID of the set you’re adding to your site. The ID of your photoset can be found in the URL of that gallery following “sets” – excluding the slashes, of course.

Your nodes should display the photos in your Flickr sets immediately. You’ll likely want to style them up a bit. Here’s what I added to my theme’s CSS:

div.flickr-photoset div.flickr-photoset-img {
  float: left;
  margin: 0 12px 12px 0;
}

div.flickr-photoset, div.flickr-photoset-meta, div.field-field-season {
  float: left;
  clear: both;
}

And here is the theme override I used in my template.php file to add the rel attribute and a class of “fancybox” to each image. This allowed me to use my favorite lightbox style plugin: Fancybox This override also links to the original size of the photo, so you get a much larger display than the default.

<?php
function yourTheme_flickrfield_photoset($img, $photo_url, $formatter, $photo_data, $node) {
 
$output = '<div class="flickr-photoset">';

  if (module_exists('flickr_sets')) {
   
$photos = flickr_set_load($photo_data['id']);
   
    foreach ((array)
$photos['photoset']['photo'] as $photo) {
   
//insert owner into $photo because theme_flickr_photo needs it
   
$photo['owner'] = $photos['photoset']['owner'];
   
$title = is_array($photo['title']) ? $photo['title']['_content'] : $photo['title'];
   
$img = flickr_img($photo, $formatter);
   
$original = flickr_photo_img($photo, 'b');

      if (arg(0) == 'node' && arg(1) == $node->nid) {
       
$output .= '<div class="flickr-photoset-img flickr-photoset-img-'. $formatter .'">'. l($img, $original, array('attributes' => array('title' => $title, 'rel' => 'photos', 'class' => 'fancybox'), 'absolute' => TRUE, 'html' => TRUE)) .'</div>';
      } else {
     
$output .= '<div class="flickr-photoset-img flickr-photoset-img-'. $formatter .'">'. l($img, 'node/'. $node->nid, array('attributes' => array('title' => $title), 'absolute' => TRUE, 'html' => TRUE)) . '</div>';
      }
    }
  } else {
   
$title = is_array($photo_data['title']) ? $photo_data['title']['_content'] : $photo_data['title'];
   
    if (
arg(0) == 'node' && arg(1) == $node->nid) {
     
$output .= '<div class="flickr-photoset-img">'. $img .'</div>';
    } else {
     
$output .= '<div class="flickr-photoset-img">'. l($img, 'node/'. $node->nid, array('attributes' => array('title' => $title), 'absolute' => TRUE, 'html' => TRUE)) . '</div>';
    }

  }
  $output .= '</div>';
 
$output .= '<div class="flickr-photoset-meta">';
 
$output .= '<p>'. $photo_data['description']['_content'] .'</p>';
 
$output .= '<cite>'. l(t('Source: Flickr'), $photo_url) .'</cite>';
 
$output .= '</div>';
  return
$output;
}
?>

Like I said before, this post was long overdue…I hope this clears up any confusion the other post might have caused.

Comments

Guest's picture
I want to kiss you. If I weren’t married and knew where you lived, I would kiss you. So, instead… here is a great big THANK YOU!!! I spent SO much time on this last night to no avail and with your tutorial I was up and running in less than 30 minutes. Thank you SO much!!! Jessica – Halo Digital Design
Guest's picture
Thanks for this great tutorial very helpful, i just want to know if i can do somthing like this http://design.tedforbes.com/demo/sets.php creating a views which contain the photoset.
wackotaco's picture
Hi thanks for this very helpful tutorial, I’m wondering if it would be possible to open the clicked thumbnail image in a new node or store page instead of a lightbox or fancybox in the same page as the gallery. THANKS!!
Guest's picture
I think that I am almost there. I got the jquery update module and fancy box plugin and module working. But it seems like my update template.php file isn’t working properly. I changed “yourTheme” to my theme name. But its not adding the “rel” tag. Is there something I need to do in the node.tpl.php file?
Guest's picture
I am getting two errors when creating a new node for a gallery: Flickr error 1: Photoset not found Flickr error 1: User not found I have checked and double checked my id values. They are right, both in the admin/settings/flickr and when creating the node. Your tutorial says the images should load immediately. But I am not experiencing that same thing. I am trying to figure out what is different about it. Any ideas?
chris's picture
can you share the link with me?
chris's picture
Make sure you’re NOT using Photoset as the field type. It actually needs to just be “Flickr Photo”. Hope this helps!
Guest's picture
So, I need to choose photoset when creating the node or when setting up the content type? Thanks for the ultra fast reply! :)
chris's picture
When setting up the content type, choose Flickr Photo as the field type. Then when creating the node, make sure you choose photoset.
Guest's picture
Yeah, That is exactly what I did. I got my user id and photoset id from the url of my flickr page. So, I don’t know what the deal is.
chris's picture
Did you configure an API key for this site?
Guest's picture
Yes, I got a non-commercial api key and secret phrase. Do I need to register the site domain with Flickr?
chris's picture
Did you configure the Flickr module inside of Drupal with those credentials? If so, I can’t really help without seeing the code/site.
Guest's picture
I got it! My photos in flickr were set to show only to friends and family by default. I changed it to public and it works now. Alright!
Guest's picture
The API is working correctly because it has had 85 calls in the last 24 hours. Mostly last night. However, it does say that there are 0 authenticated users. That would make sense since drupal is saying “user not found”.
Physicalpatrick's picture
hi there…i’m having trouble getting the fancybox to work. there doesn’t seem to be an explaination of where that implementation is made. any suggestions?
chris's picture
You’ll need to use the jQuery update module. Without it, you won’t be able to make fancybox work at all. There is also a Fancybox module that makes using Fancybox a snap.
Tony's picture
Can we hide the “Source: Flickr” ? and create “Pages” ? thank you :D
chris's picture
You sure can. I believe you’ll just need to override the theme function for whatever area in question. Check the flickr module for its theme functions.
Ced's picture
Great tutorial! Really helpful. One issue however. For some reason everything works fine when and the images display properly when I’m logged into drupal and viewing the page but if I log out of drupal the images wont display. What do you think this could be? Thanks
chris's picture
this is likely a permissions issue. Make sure that anonymous users have the permissions to “View” your flickr field.
Guest's picture
That definitely sounds like a Permissions issue, just make sure the view settings for the field are checked for everyone.
Greg's picture
Where would one access the view settings for a field?
Ced's picture
Great tutorial! Easy to follow. Except for one lil problem I’m having. For some reason, the images do not show up when I log out of drupal. In fact any comp that is not logged into the website’s drupal interface cannot see the images. Would this be something to do with privileges? Do I need to enter in some sort of ID? Thanks Ced
Ameria's picture
Thank you so much for this tutorial, it’s working great so far! I was wondering about something from the out-of-date tutorial that isn’t getting used in the example for the new one. How would I go about pulling the cover images from multiple sets into a view to display on a main photo galleries page similar to the /photos page on the old tutorial? Would I have to add another field using the Flickr Photo Set type and pull that into the view, or is there an easier way that will grab the ID from the Flickr Photo field already used?
Guest's picture
Thanks for the great tutorial. It was very handy.
Guest's picture
I got everything installed per your instruction but the lightbox2 is not working. Do I have to do the “input owner in the $photo” to make lightbox2 work? I am not getting any error but everytime I click the photo it goes to the node then the original size. Please advice
Guest's picture
I am quite the drupal illiterate and understand a good portion of the tutorial directions but am still having trouble. I was able to use the flickr module successfully and have my photosets displaying correctly. With fancybox, however, I can’t seem to make everything work. Using the instructions above, I altered the css and the template.php files, changing the theme name as necessary. I called up fancybox using scripts[]= jquery-fancybox.1.3.4.js in the theme info, and tried again in the template file using drupal_add_js. I’m not sure I fully grasp what the note about //insert owner into $photo because theme_flickr_photo needs it means. What am I doing wrong?
Peel's picture
In the above tutorial, I don’t see where you incorporated the views module into this, yet in your example gallery page it looks like you’re using it to generate the grid output. Am I missing a step here? Thanks for your help.
Don's picture
Thanks for the useful tutorial. Have you ever done anything similar using a flickr group pool, or maybe a user’s favorites? It seems like that would be a nice way to let a community collaborate on photos.
Guest's picture
Sweet! I tried your previous tutorial with no succes. Now it runs smoothly. I used Lightbox as viewer, so I didn’t have to update jQuery of Drupal. Also I didn’t use the Views (yet) as I only needed to show one photoset.
chris's picture
Fantastic! Glad you found it useful.
Karl's picture
I am also having a problem with the Fancy box implementation. I have both Jquery and the fancybox installed. It also says under the fancybow tab that jquery was found and I saved the configuration. Your tutorial was excellent. I modified my CSS file and my template.php with your code. I can tell both files were successfully updated because the pictures are laid out the same as yours and the linked images are bigger than they were previously. The part I dont fully understand is how to use “rel attribute and a class of fancybox to each image” to activate fancybox. Right now its like fancybox isnt installed. Also I have installed: CCK, Image Cache, Imagefield, fancybox, Views, Chaos Tools, etc. I have tried disabling each one of my plugins, but it doesnt seem to affect it. Any help would be greatly appreciated.
chris's picture
Hi Karl, Have you tried using the jQuery update module? You need that to upgrade Drupal’s core jQuery to the jQuery you need for use with Lightbox. Also try using the Fancybox module. It will help you get the rel attributes specified on the elements of your choosing. Hope this helps!
Karl's picture
Thank you for your quick reply. I am using jQuery 1.3.2, Fancybox 1.3.4 and Fancybox Jquery plugin. My problem is that I am a complete noob when it comes to drupal and web programming for that matter. I dont know how to use the rel tags. What do you change in the fancybox module to make the photogallery look like your example? Do you need to add something to the “Activation” Field or Something under the Advanced Menu? I have tried googling and reading drupal’s site but I have not found the answer. Also, I tried out lightbox2 drupal plugin but honestly I like the look of fancybox better. Thank you again, -Karl P.S. This is my website: www.botsnbytes.com/drupal Check it out the “eval bot” or olimex msp430” links if you want to see what exactly I am trying to display.
chris's picture

Hi Karl – if you’re using the fancybox module you’ll want to use Fancybox 1.3.1 – this is what the module recommends. You’ll also need to add “.fancybox” to the activation code to properly activate the effect on those links. That code must include the preceding period. This will target any element with the “fancybox” class. Looking at your setup, it looks like you’re almost there!

Guest's picture
That was really, really helpful. I made exactly the same mistakes – latest fancybox plugin, missing the selector from “activation” inside the Fancybox module settings. You should really include those two precious lines in the main article! :D Great job, btw.
Karl's picture
Thanks a lot!!!! I changed to the fancybox 1.3.1 and added “.fancybox” to the “jQuery selector:” field under activation. Now it works great! I dont know if you can tell but I am pretty excited about it. Thanks again!
ArtisticBB's picture
Thank you very much for the tutorial. I am using drupal 7 and it also works. I only had to change the first part of the overriding function to reflect the new way of passing variables to functions: function yourTheme_flickrfield_photoset($variables) { $img = $variables['0']; $photo_url = $variables['1']; $formatter = $variables['2']; $photo_data = $variables['3']; $node = $variables['4']; $output = '<div class="flickr-photoset">'; And also it would be useful if you add the explanation about the “.fancybox” jQuery selector to the end of the article!!
Guest's picture
i’m trying to implement this on a fresh install of Drupal 7 with the Flickr module and haven’t had any luck. Is there a different function I should be overriding when using the Flickr format filter to display a set?