introduction to module development · introducing mailfish ... reference pages on api.drupal.org...
TRANSCRIPT
Drupal Training
Introduction to Module Development
Monday, August 23, 2010
Alex Urevick-Ackelsberg
Monday, August 23, 2010
Alex Urevick-Ackelsberg
• Partner & Business Developer
Monday, August 23, 2010
Alex Urevick-Ackelsberg
• Partner & Business Developer
• Alex UA on Drupal.org
Monday, August 23, 2010
Alex Urevick-Ackelsberg
• Partner & Business Developer
• Alex UA on Drupal.org
• @alexu_a on twitter
Monday, August 23, 2010
Alex Urevick-Ackelsberg
• Partner & Business Developer
• Alex UA on Drupal.org
• @alexu_a on twitter
• Maintainer of emfield
Monday, August 23, 2010
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
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
Drupal Community
• “Come for the code, stay for the community”
• #drupal and #drupal-support
• Drupalcons, camps, meetups, groups.drupal.org
Monday, August 23, 2010
Introducing Mailfish
• Collect and store email-addresses
• Display sign-ups to admins
• Allow per-node signups
Monday, August 23, 2010
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
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
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
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
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
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
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
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
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
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
Install Files
• .install files contain hooks for
• install (MODULENAME_install())
• uninstall (MODULENAME_uninstall())
• database updates (MODULENAME_update_N())
Monday, August 23, 2010
Schema API
• drupal_install_schema
• drupal_uninstall_schema
• hook_schema defines arrays of database tables and fields
Monday, August 23, 2010
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
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
Looping through DB results
• while ($nid = db_result($result))
• while ($value = db_fetch_object($result))
Monday, August 23, 2010
Node objects
• Take a look at loaded node objects via devel module’s tab
• node_load
• node_save
• node_delete
Monday, August 23, 2010
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
Form Validation and Submission
• FORMID_validate and FORMID_submit are the naming conventions
• $form_state[‘values’] contains the form entries
Monday, August 23, 2010
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
hook_cron
• Add code that runs periodically (on cron jobs)
• Send an email for periodic updates
Monday, August 23, 2010
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
Implementing hook_theme()
Monday, August 23, 2010
Providing a default theme function
Monday, August 23, 2010
Providing a default template
Monday, August 23, 2010
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
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
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