creating custom drupal modules
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.xTRANSCRIPT
![Page 1: Creating Custom Drupal Modules](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/1.jpg)
Creating Custom Drupal Modules
Drupal East Anglia UG – 16/6/2010Alastair Aitchison
![Page 2: Creating Custom Drupal Modules](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/2.jpg)
• 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](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/3.jpg)
Types of Module
![Page 4: Creating Custom Drupal Modules](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/4.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/5.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/6.jpg)
.info file
• Metadata about the module• Name, description, version, dependencies etc.
• EXAMPLE!
![Page 7: Creating Custom Drupal Modules](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/7.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/8.jpg)
Drupal’s Hook System
Build menus
Render nodeBuild blocks
hook_menu
hook_node_view
![Page 9: Creating Custom Drupal Modules](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/9.jpg)
.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](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/10.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/11.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/12.jpg)
Alter Hooks
Build menus
Render nodeBuild blocks
hook_menu
hook_node_view
Perform
Alterations
hook_xxx_alter
![Page 13: Creating Custom Drupal Modules](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/13.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/14.jpg)
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](https://reader033.vdocument.in/reader033/viewer/2022061615/554bddddb4c9058f6c8b579b/html5/thumbnails/15.jpg)
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