Click here to load reader

Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

  • View

  • Download

Embed Size (px)

Text of Introduction to Module Development John Fiala and Ezra Barnett Gildesgame

  • Introduction to Module Development

    John FialaandEzra Barnett Gildesgame

  • What is a module?Apollo Lunar Service and Excursion Modules

  • What is a module?A piece of software that adds or extends functionality on a Drupal website

  • Adds functionalityUbercart - Provides a sophisticated online store

    Fivestar - Provides a star rating widget for rating content

    Signup - Let's users sign up to attend events

  • Extends FunctionalityUC_Signup - Allows people to to pay for Ubercart EventsToken - Many modules rely on Token to provide configurable messages including variable (like [user-name])

  • Often, adding a new feature entails extending or connecting existing featuresYou usually don't have to start from scratch :)

    Fivestar stores data using the VotingAPIUC_Signup connects Ubercart and SignupEmbedded Media Field provides a new field for use with the Content Construction Kit

  • Core, Contrib & CustomCore - 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.

  • Drupal CommunityCheck out the issue queueFind a related group on

  • Custom ModulesOften avoidable

    Often necessary

    Usually cost more to maintainAPI changes, version updatesSecurityFeature additionsFriends you lose when you duplicate their module(We still

  • You usually don't have to start from scratch!Drupal Core and Contrib have nice systems thatyou can harness in your module

    Displaying and processing forms for user input

    Handling user accounts

    Handling Content (Core node, contrib CCK)

    Creating custom listings of content (Views)

  • And much, much more!

  • How do modules add or extend functionality?

  • How do modules add or extend functionality?

  • 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

  • Examples of Hook EventsDisplaying a form to the user (hook_form_alter)Add a custom checkboxA user signs up for an account (hook_user)Display a friendly messageSubmitting a node (hook_nodeapi)Store custom data in the databaseThe website sends mail (Connect to an SMTP sever

  • Writing a moduleCreate a .info file - Tell Drupal your module existsCreate a .module fileImplement one or more hooksCreate a .install file (optional)

  • Go to Basic Info File: ; $Id$ name = Example module description = "Gives an example of a module." core = 6.x Optional: package = Views dependencies[] = views dependencies[] = panelsphp = 5.1

  • 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 enteredWhich Form Is This?Step 1: Use dpm($form_id);to find out!

  • 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:

  • How do you Interact with nodes?hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)The node to changeWhat's happening to the node(There's a long list):delete/insert/update/view/validateExtra data: if 'view', then $teaser, if 'validate', then $form.Extra data: if 'view' then $page

  • 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, etcForm Values submittedThe User's User Object(Why isn't it $user?)$category - the category of User Info being changed

  • So, let's throw something together...

  • Modules That Help Build Modules

  • Token

  • Image CreditsTube Man Toys

  • Updating Modules from 5.x to 6.x

    Form API Reference -

    Form API Quickstart -"Easy" d.o Newbie tag

    Discuss what a hook is, and how they work. Maybe touch briefly on theme functions?Discuss what Devel and Coder are good for.