Drupal + Flickr + jQuery

42
Drupal + Flickr + jQuery The process of building a module to pull images from Flickr and present them using jQuery. Presented by Benjamin Shell from WorkHabit

description

The process of building a module to pull images from Flickr and present them using jQuery.

Transcript of Drupal + Flickr + jQuery

Page 1: Drupal + Flickr + jQuery

Drupal + Flickr + jQueryThe process of building a module to pull images from

Flickr and present them using jQuery.

Presented by Benjamin Shell from WorkHabit

Page 2: Drupal + Flickr + jQuery

Why Flickr?

• Millions of people already use it

• Easy to upload photos

Page 3: Drupal + Flickr + jQuery

Why import to Drupal?

• Theming flexibility

• Integration

• Security

Page 4: Drupal + Flickr + jQuery

Demo

Page 5: Drupal + Flickr + jQuery

The Process of Developing a Module

Page 6: Drupal + Flickr + jQuery

Development Process

• Have a plan

• Evaluate existing modules

• Reconsider plan

• Outline what’s still needed

• Develop new module(s)

Page 7: Drupal + Flickr + jQuery

The Plan

• Automatically download new photos from Flickr and create Drupal nodes with attached images

• Use Views to select nodes based on tags and other photo attributes

• Render output as a sliding image viewer

Page 8: Drupal + Flickr + jQuery

Looking for Modules

• drupal.org

• drupalmodules.com

• CVS checkout of all contributions (grep search for keywords)

• Asking around (drupal.org, IRC, co-workers, conferences, etc.)

Page 9: Drupal + Flickr + jQuery

Existing Modules

Maybe:

• Activity Stream

• FlickrRippr

• Emfield/Emfield Import

• Flickr module

Definite:

• CCK

• Views

• Imagefield

• Imagecache

Page 10: Drupal + Flickr + jQuery

The Problem

• None of the Flickr modules worked the way I wanted them to

• I couldn’t find a Drupal module for rendering a sliding image viewer

Page 11: Drupal + Flickr + jQuery

The Options

• Reconsider the plan?

• Patch existing modules?

• Build new modules?

Page 12: Drupal + Flickr + jQuery

Building new modules

• Use existing modules as a starting point

• Collaborate with others

• Credit others in your documentation

• Share with the community

Page 13: Drupal + Flickr + jQuery

Data in / Data out

Input OutputStorage Retrieval

developersstart here

Page 14: Drupal + Flickr + jQuery

Module Development• Data Storage (CCK + Imagefield)

• Input mechanism (new module)

• Input some data (run cron)

• Select the data to be output (Views)

• Render the data with HTML (new module)

• Style with CSS (new module)

• Enhance with JavaScript (new module)

Page 15: Drupal + Flickr + jQuery

Three new modules:

1) Flickr API

2) Flickr Sync

3) Image Slider

Development Plan

Page 16: Drupal + Flickr + jQuery

Using an API

• Register for an API key

• Use a helper class or module

• Cache results

Page 17: Drupal + Flickr + jQuery

phpFlickr

• An open source PHP class for easily using Flickr API methods

• Handles caching

Page 18: Drupal + Flickr + jQuery

Flickr API Module

NEW M

ODULE

Page 19: Drupal + Flickr + jQuery

Flickr API Module

• Loads phpFlickr

• Stores the Flickr API key

• Can be used by other Flickr modules

Page 20: Drupal + Flickr + jQuery

Flickr Sync Module

NEW M

ODULE

Page 21: Drupal + Flickr + jQuery

Flickr Sync Module

Downloads new Flickr photos to new Drupal nodes

Page 22: Drupal + Flickr + jQuery

Image Slider Module

NEW M

ODULE

Page 23: Drupal + Flickr + jQuery

Concept based on jQuery slideViewer 1.1

Page 25: Drupal + Flickr + jQuery

CSS and JavaScript

Load only where needed.

Page 26: Drupal + Flickr + jQuery

Adding CSSdiv.imageslider { overflow: hidden; position: relative;}

div.imageslider ul { list-style-type: none; position: relative; left: 0; top: 0;}

div.imageslider ul li { float: left;}

Change left value to move

the slider

Page 27: Drupal + Flickr + jQuery

Adding JavaScript

JavaScript should be unobtrusive

Page 28: Drupal + Flickr + jQuery

Adding JavaScript

Follow good naming conventions

Page 29: Drupal + Flickr + jQuery

Naming Conventions

var box = getBox();

var newBoxHeight = 20;

box.setHeight(newBoxHeight);

Typically lower/upper , but just be consistent with Drupal and jQuery, or whatever JS libraries you work with.

Page 30: Drupal + Flickr + jQuery

Adding JavaScript

Use proper namespacing

Page 31: Drupal + Flickr + jQuery

JavaScript Namespacing

What’s wrong with this?

for (i = 0; i < 5; i++) {

// do something

}

Page 32: Drupal + Flickr + jQuery

JavaScript Namespacing

Using undefined variables clutters the global namespace!

Page 33: Drupal + Flickr + jQuery

JavaScript Namespacing

Define every variable!

for (var i = 0; i < 5; i++) {

// do something

}

Page 34: Drupal + Flickr + jQuery

JavaScript Namespacing

Namespace everything. Even JavaScript functions will overwrite each other:

function test() { alert('foo');}

function test() { alert('bar');}

test();

overwritten!

Page 35: Drupal + Flickr + jQuery

JavaScript Namespacing

var Drupal = Drupal || {};

Drupal.extend = function(obj) { ... }

Drupal.dimensions = function (el) { ... }

An example of proper namespacing:

Page 36: Drupal + Flickr + jQuery

Adding JavaScript

Learn to understand scope

Page 37: Drupal + Flickr + jQuery

Understanding Scope

function autoAttach() { var settings = { foo: 'bar' };

jQuery("a").mouseover(function() { alert(settings.foo); });}

Simple Example of jQuery scope:

Page 38: Drupal + Flickr + jQuery

Understanding Scope

function autoAttach() { var settings = { foo: 'bar' }; jQuery("a").mouseover(function() { onOver.call(settings); });}

function onOver() { // 'this' is the settings object alert(this.foo);}

Using a separate function for handlers

Page 39: Drupal + Flickr + jQuery

Understanding Scope

function autoAttach() { var settings = { foo: 'bar' }; jQuery("a").mouseover(function(evt) { onOver.call(settings, evt); });}

function onOver(evt) { // 'this' is the settings object alert(this.foo);}

Passing additional variables with ‘call’

Page 40: Drupal + Flickr + jQuery

Debugging JavaScript

http://getfirebug.com/

Page 41: Drupal + Flickr + jQuery

Drupal.org Releases

• http://drupal.org/project/flickrapi

• http://drupal.org/project/flickrsync

• http://drupal.org/project/imageslider

Page 42: Drupal + Flickr + jQuery

Questions?