26
Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Embed Size (px)

Citation preview

Page 1: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Introduction to Module Development

John Fiala and

Ezra Barnett Gildesgame 

Page 2: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

What is a module?

 

Apollo Lunar Service and Excursion Modules

Page 3: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

What is a module?

A piece of software that adds or extends functionality on a Drupal website

Page 4: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Adds functionality

o Ubercart - Provides a sophisticated online store

o Fivestar  - Provides a star rating widget for rating content

o Signup - Let's users sign up to attend events

Page 5: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Extends Functionality

• UC_Signup - Allows people to to pay for Ubercart Events• Token - Many modules rely on Token to provide

configurable messages including variable (like [user-name])

Page 6: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Often, adding a new feature entails extending or connecting existing

featuresYou usually don't have to start from scratch :)

• Fivestar stores data using the VotingAPI• UC_Signup connects Ubercart and Signup• Embedded Media Field provides a new field for use with the

Content Construction Kit

Page 7: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Core, Contrib & Custom

• Core - Part of the official Drupal package. Everyone using Drupal has it

• Contrib - Publicly available optional download, not specific to a particular website. (Though sometimes specific to a kind of feature)

• Custom - Specific to a particular website. Useful for that website, but not generally useful to the public.

Page 8: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Drupal Community

• Check out the issue queue• Find a related group on groups.drupal.org

Page 9: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Custom Modules

• Often avoidable

• Often necessary

• Usually cost more to maintaino API changes, version updateso Securityo Feature additionso Friends you lose when you duplicate their moduleo (We still <8 you)

Page 10: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

You usually don't have to start from scratch!

Drupal Core and Contrib have nice systems thatyou can harness in your module

o Displaying and processing forms for user input

o Handling user accounts

o Handling Content (Core node, contrib CCK)

o Creating custom listings of content (Views)

Page 11: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

And much, much more!

Page 12: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

How do modules add or extend functionality?

 

Page 13: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

How do modules add or extend functionality?

Page 14: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

With hooksI'm performing a particular action.

Does anyone have anything to say about it?

• Person A: I'm getting up to get napkins. Does anybody want me to get anything else?

• Person B: Yes! Please get straws.

• Module A: I'm presenting a form to the user.• Module B: Please add a checkbox to the form!

A - The hook DefinitionB - The hook Implemenation

Page 15: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Examples of Hook Events

• Displaying a form to the user (hook_form_alter)o Add a custom checkbox

• A user signs up for an account (hook_user)o Display a friendly message

• Submitting a node (hook_nodeapi)o Store custom data in the database

• The website sends mail (o Connect to an SMTP sever

Page 16: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Writing a module

• Create a .info file - Tell Drupal your module exists• Create a .module file

o Implement one or more hooks• Create a .install file (optional)

Page 17: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Go to http://drupal.org/node/231036Your Basic Info File:  ; $Id$name = Example moduledescription = "Gives an example of a module."core = 6.x

Optional: package = Viewsdependencies[] = viewsdependencies[] = panelsphp = 5.1

Page 18: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

How Do I Interact with a Form?

hook_form_alter(&$form, $form_state, $form_id)

The $form Array:(We'll get to this)

The Form Status$form_state['values'] is what was entered

Which Form Is This?Step 1: Use dpm($form_id);to find out!

http://api.drupal.org/api/function/hook_form_alter/6

Page 19: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Form Array?  What?Something like this:

$form['foo'] = array(  '#type' => 'textfield',   '#title' => t('bar'),  '#default_value' => $object['foo'],  '#size' => 60,  '#maxlength' => 64,   '#description' => t('baz'),);$form['submit'] = array(  '#type' => 'submit',  '#value' => t('Save'),);

QuickStart: http://api.drupal.org/api/file/developer/topics/forms_api.html/6

Page 20: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

How do you Interact with nodes?hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)

The node to change

What's happening to the node(There's a long list):delete/insert/update/view/validate

Extra data: if 'view', then $teaser, if 'validate', then $form.

Extra data: if 'view' then $page

http://api.drupal.org/api/function/hook_nodeapi/6

Page 21: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

What about interacting with User accouts.

hook_user($op, &$edit, &$account, $category = NULL)

What the User is Doing(Again, a list, but...)delete/insert/load/login/logout/register/update /etc, etc, etc

Form Values submitted

The User's User Object(Why isn't it $user?)

$category - the category of User Info being changed

Page 22: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

So, let's throw something together...

Page 23: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Modules That Help Build Modules

Page 24: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Token

 

Page 25: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

Image Credits

• Tube Man•  http://www.flickr.com/photos/redherring1up• NASA Toys

http://www.silentthundermodels.com/nasa_space_models/apollo.html

Page 26: Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

 

• Updating Modules from 5.x to 6.xhttp://drupal.org/node/114774

• Form API Reference - http://api.drupal.org/api/file/developer/topics/forms_api_reference.html/6

• Form API Quickstart -http://api.drupal.org/api/file/developer/topics/forms_api.html• "Easy" d.o Newbie taghttp://drupal.org/project/issues/search?text=&projects=&assigned=&submitted=&participant=&status%5B%5D=Open&issue_tags=Newbie