introduction to module development · introducing mailfish ... reference pages on api.drupal.org...

37
Drupal Training Introduction to Module Development Monday, August 23, 2010

Upload: others

Post on 02-Jun-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Drupal Training

Introduction to Module Development

Monday, August 23, 2010

Page 2: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Alex Urevick-Ackelsberg

Monday, August 23, 2010

Page 3: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Alex Urevick-Ackelsberg

• Partner & Business Developer

Monday, August 23, 2010

Page 4: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Alex Urevick-Ackelsberg

• Partner & Business Developer

• Alex UA on Drupal.org

Monday, August 23, 2010

Page 5: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Alex Urevick-Ackelsberg

• Partner & Business Developer

• Alex UA on Drupal.org

• @alexu_a on twitter

Monday, August 23, 2010

Page 6: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Alex Urevick-Ackelsberg

• Partner & Business Developer

• Alex UA on Drupal.org

• @alexu_a on twitter

• Maintainer of emfield

Monday, August 23, 2010

Page 7: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Alex Urevick-Ackelsberg

• Partner & Business Developer

• Alex UA on Drupal.org

• @alexu_a on twitter

• Maintainer of emfield

• Admin for GSoC

Monday, August 23, 2010

Page 8: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Howard Tyson

• Senior Developer at Zivtech

• Drupal architect, developer, themer, site-builder, project manager, teacher and student

• tizzo on drupal.org, IRC, twitter, tumblr, anywhere I can get it

Monday, August 23, 2010

Page 9: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Drupal Community

• “Come for the code, stay for the community”

• #drupal and #drupal-support

• Drupalcons, camps, meetups, groups.drupal.org

Monday, August 23, 2010

Page 10: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Introducing Mailfish

• Collect and store email-addresses

• Display sign-ups to admins

• Allow per-node signups

Monday, August 23, 2010

Page 11: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Starting a new module

• Contributed and custom modules go into sites/all/modules

• Create the .info file

• Create the .module file

Monday, August 23, 2010

Page 12: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Hook System

• http://api.drupal.org/api/group/hooks/6

• To implement a hook, name a function as MODULENAME_HOOKNAME( )

• All implementations of a hook are run when the hook is invoked

Monday, August 23, 2010

Page 13: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Menu System

• hook_menu defines menu items

• Menu items may be normal items, local tasks (tabs) or only callbacks

• Access to menu items is typically based on permission

• hook_menu_alter can alter menu items

Monday, August 23, 2010

Page 14: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Menu Callbacks• Administrative menu callbacks (and

supporting functions) usually go into a MODULENAME.admin.inc file

• Non-administrative menu callbacks (etc.) usually go into a MODULENAME.pages.inc file

• This is a best practice for both readability and performance

Monday, August 23, 2010

Page 15: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Permissions System

• hook_perm defines available permissions

• Roles are granted permissions via the UI

• Permissions of the current user are checked with user_access( )

• often the menu system will be doing this for us

Monday, August 23, 2010

Page 16: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Form API

• A form is defined as an array of field arrays (or a multidimensional array)

• drupal_get_form renders the form array into HTML form elements

• See the Forms Quickstart and Forms Reference pages on api.drupal.org

Monday, August 23, 2010

Page 17: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

System Settings Forms

• system_settings_form adds additional features to a form array

• submit and reset buttons

• On submission, form fields are saved as Drupal variables

Monday, August 23, 2010

Page 18: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Variables System

• variable_get, variable_set, variable_del

• Variables are stored serialized in the variables table, allowing them to store structured data (like arrays and objects)

• Edit variables directly with devel or drush

Monday, August 23, 2010

Page 19: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

t( ): translatable

• All interface text should be translatable (via locale or string overrides module)

• Supports multiple placeholders:

• !(as is)

• @(sanitized)

• %(emphasized)

Monday, August 23, 2010

Page 20: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

hook_form_alter

• Alter any Drupal form’s fields, validation and submission handlers

• The $form_id is the name of the form definition function

• hook_FORMID_alter is usually ideal

• (but it won’t always work)

Monday, August 23, 2010

Page 21: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Install Files

• .install files contain hooks for

• install (MODULENAME_install())

• uninstall (MODULENAME_uninstall())

• database updates (MODULENAME_update_N())

Monday, August 23, 2010

Page 22: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Schema API

• drupal_install_schema

• drupal_uninstall_schema

• hook_schema defines arrays of database tables and fields

Monday, August 23, 2010

Page 23: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Database queries

• db_query connects to the database and runs queries

• enclose table names with { }

• use %d and ‘%s’ placeholders for all variables

Monday, August 23, 2010

Page 24: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Database Results• db_fetch_object returns an object out of

the first row of db results and removes that row

• db_fetch_array does the same but returns an array

• db_result does the same for a single result (if only one field was selected)

Monday, August 23, 2010

Page 25: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Looping through DB results

• while ($nid = db_result($result))

• while ($value = db_fetch_object($result))

Monday, August 23, 2010

Page 26: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Node objects

• Take a look at loaded node objects via devel module’s tab

• node_load

• node_save

• node_delete

Monday, August 23, 2010

Page 27: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

hook_nodeapi

• Effect what happens with node objects

• Alter nodes and run other code on creation, update, deletion, load, view etc.

Monday, August 23, 2010

Page 28: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Form Validation and Submission

• FORMID_validate and FORMID_submit are the naming conventions

• $form_state[‘values’] contains the form entries

Monday, August 23, 2010

Page 29: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Block System

• hook_block defines blocks

• The delta key distinguishes between multiple blocks per module

• this can be numeric or a string (not, strictly, a delta)

• Blocks may have configuration settings

Monday, August 23, 2010

Page 30: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

hook_cron

• Add code that runs periodically (on cron jobs)

• Send an email for periodic updates

Monday, August 23, 2010

Page 31: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Theme Registry

• modules must implement hook_theme to declare their themable functions and templates

• these functions must be titled theme_function_name

• theme(‘function_name’, $param) is then used to call the function

Monday, August 23, 2010

Page 32: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Implementing hook_theme()

Monday, August 23, 2010

Page 33: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Providing a default theme function

Monday, August 23, 2010

Page 34: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Providing a default template

Monday, August 23, 2010

Page 35: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Preprocess Functions

• Any module or theme can preprocess variables for a template

• Add or modify variables

• hook_preprocess_TEMPLATE_NAME( &$vars)

• template_preprocess_TEMPLATE_NAME

Monday, August 23, 2010

Page 36: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

To Infinity......and BEYOND!

• Or... To Drupal 7 and we don’t know what is beyond

• D7 is right around the corner

• Lets look at the major changes...

Monday, August 23, 2010

Page 37: Introduction to Module Development · Introducing Mailfish ... Reference pages on api.drupal.org Monday, August 23, 2010. System Settings Forms

Object Oriented DB

• An object oriented Database Abstraction layer built around PHP’s PDO

• Gives us support for more RDBMSes

• MySQL, PostgreSQL, SQLite, MS SQL, Oracle

• (and a few bits can work with Mongo)

Monday, August 23, 2010