15
Creating Custom Drupal Modules Drupal East Anglia UG – 16/6/2010 Alastair Aitchison

Creating Custom Drupal Modules

Embed Size (px)

DESCRIPTION

Presentation delivered to the East Anglia Drupal Usergroup meeting on 16th June 2010 demonstrating how to create a simple custom module in Drupal 7.x

Citation preview

Page 1: Creating Custom Drupal Modules

Creating Custom Drupal Modules

Drupal East Anglia UG – 16/6/2010Alastair Aitchison

Page 2: Creating Custom Drupal Modules

• Independent developer based in Norwich• 3 years Drupal experience• Current maintainer of Question module• Contributed to Automatic Nodetitles, Node

Import, Examples, Synonyms, Tagadelic, WebSnapr...

tanoshimi AlastairA [email protected]

About Me

Page 3: Creating Custom Drupal Modules

Types of Module

Page 4: Creating Custom Drupal Modules

Anatomy of a Module

Required• .info – metadata describing the module • .module – the module PHP codeOptional• .install – install / uninstall scripts• .css – stylesheets• .inc – additional include files• .test – unit test files

Page 5: Creating Custom Drupal Modules

Where Do Module Files Reside?

CORE • /modules/ subdirectory• Don’t edit or place other modules here!

CONTRIB & CUSTOM• /sites/all/modules/ subdirectory• Directory name matches module name

Page 6: Creating Custom Drupal Modules

.info file

• Metadata about the module• Name, description, version, dependencies etc.

• EXAMPLE!

Page 7: Creating Custom Drupal Modules

meeting.info

name = Meeting Demo Module

description = This is a demo module

for the Drupal UG meeting

core = 7.x

version = 7.0 - alpha

files[] = meeting.module

Page 8: Creating Custom Drupal Modules

Drupal’s Hook System

Build menus

Render nodeBuild blocks

hook_menu

hook_node_view

Page 9: Creating Custom Drupal Modules

.module file

• Implements one or more Drupal hooks – PHP functions that get called by Drupal

• Proper naming ensures that functions are automatically recognised and fired at the right time

• Syntax is modulename_hookname• EXAMPLE!

Page 10: Creating Custom Drupal Modules

meeting.module<?php

function meeting_menu() {

$items['meeting_page'] = array(

'title' => 'My newmenu item',

'description' => 'This is a test menu item',

'page callback' => 'meeting_page',

'access callback' => TRUE,

);

return $items;

}

function meeting_page() {

return 'Yay! This worked.... probably.';

}

Page 11: Creating Custom Drupal Modules

Altering Other Modules

• Edit user_login_form? (l. 1,154 of user.module)

• Use hook_form_user_login_form_alter

How to change this?

NO!YES!

Page 12: Creating Custom Drupal Modules

Alter Hooks

Build menus

Render nodeBuild blocks

hook_menu

hook_node_view

Perform

Alterations

hook_xxx_alter

Page 13: Creating Custom Drupal Modules

Not only forms can be altered...

• hook_link_alter• hook_mail_alter• hook_menu_alter• hook_profile_alter• hook_schema_alter• ...

Page 14: Creating Custom Drupal Modules

More Resources

• http://api.drupal.org – lists all available hooks, together with example usage

• http://drupal.org/project/modules - contrib module repository

• http://www.apress.org – publishers of Pro Drupal Development

Page 15: Creating Custom Drupal Modules

Things to Remember

• Custom modules add new functionality / alter other modules by implementing hooks

• Require .info (metadata) and .module (code)• Create in a subdirectory of sites/all/modules• DON’T EDIT CORE! Use hook_xxx_alter