Drupal Module Development

An Introduction

http://svenaas.github.io/dmd/

Created by Sven Aas / @svenaas
Presented at the Web Conference at Penn State, June 9, 2014

#psudmd

Sven Aas

Lead Web Applications Developer

Mount Holyoke College

@svenaas

My Drupal Experience

  • Started exploring Drupal in 2010
  • 2011: DrupalCon Chicago and Training
  • 2012: DrupalCon Denver and Site Launch
  • 2014: DrupalCon Austin

My Core Message

You can do this

Drupal 7

Architecture ([Over]simplified)

On every HTTP request ...

  • index.php is requested
  • bootstrap.inc is loaded along with other core code
  • all [enabled] module and theme code is loaded
  • index.php passes request to a handler

Hooks

  • Callback API
  • Exposed by Drupal Core and some modules
  • Event-based
  • All modules can implement hook handlers
  • All modules' hook implementations get called

Menus

  • Routing — associate a URL with a method
  • Permissions – whether a URL can be accessed
  • Menus

Templates

Render Arrays

  • Content data and metadata
  • Context
  • Messy: inconsistent and overcomplicated

Anatomy of a Module

  • my_module.info
  • my_module.module
  • my_module.install

my_module.info


name = My Module
description = Make everything beautiful
package = My Package
core = 7.x

; This is a comment
dependencies[] = entity
stylesheets[all][] = css/my_module.css
files[] = my_module_extra_bits.inc

my_module.install


<?php

/**
 * @file
 * Contains install and update functions for My Module.
 */

/**
 * Implements hook_install().
 */
function my_module_install() {      
  // Do installation stuff
}

my_module.install functions

  • my_module_install()
  • my_module_schema()
  • my_module_update_7001()
  • my_module_uninstall()

my_module.module


<?php
/**
 * @file
 * Make everything beautiful 
 */

/**
 * Page callback for administrative settings
 */
function my_module_admin() {
  // Do menu stuff
}

my_module.module functions

  • my_module_admin()
  • my_module_menu()
  • my_module_permission()
  • Other hook implementations
  • Internal functions

Tools

watchdog / log

  • watchdog()
  • Shows up in administrative UI at Reports / Recent log messages (admin/reports/dblog)

Devel module

Inspector

  • DOM Inspection
  • Network requests
  • JavaScript console

Doing Something Useful

Modifying a Form

Drupal APIs

Responsibilities

Security

Translatability

Compatibility

  • Independence
  • Dependencies

Performance

Extensibility

Contrib

Drupal.org

  • Projects
    • Modules
    • Themes
  • Users

Types of projects

Code standards

Project Applications

Drupal 8

Larry Garfield's presentation What to Expect from Drupal 8 (for developers) is an excellent summary of the changes coming down the pike.

Empowerment

Many Drupal customizations can be implemented in custom modules much more easily than you think.

  • You can do this
  • Your students can do this

Resources

Thank you

Questions?

Stay in Touch