Creating Custom Drupal Modules

Post on 08-May-2015

16,336 views 4 download

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

transcript

Creating Custom Drupal Modules

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

• 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 alastair@a3uk.com

About Me

Types of Module

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

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

.info file

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

• EXAMPLE!

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

Drupal’s Hook System

Build menus

Render nodeBuild blocks

hook_menu

hook_node_view

.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!

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.';

}

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!

Alter Hooks

Build menus

Render nodeBuild blocks

hook_menu

hook_node_view

Perform

Alterations

hook_xxx_alter

Not only forms can be altered...

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

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

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