Creating Custom Drupal Modules Drupal East Anglia UG – 16/6/2010 Alastair Aitchison
<ul><li>Independent developer based in Norwich </li></ul><ul><li>3 years Drupal experience </li></ul><ul><li>Current maint...
Types of Module
Anatomy of a Module <ul><li>Required </li></ul><ul><li>.info  – metadata describing the module  </li></ul><ul><li>.module ...
Where Do Module Files Reside? <ul><li>CORE  </li></ul><ul><li>/modules/  subdirectory </li></ul><ul><li>Don’t edit or plac...
.info  file <ul><li>Metadata about the module </li></ul><ul><li>Name, description, version, dependencies etc. </li></ul><u...
meeting.info <ul><li>name = Meeting Demo Module </li></ul><ul><li>description = This is a demo module  </li></ul><ul><li>f...
Drupal’s Hook System Build menus Render node Build blocks hook_menu hook_node_view
.module  file <ul><li>Implements one or more Drupal  hooks  – PHP functions that get called by Drupal </li></ul><ul><li>Pr...
meeting.module <ul><li><?php </li></ul><ul><li>function meeting_menu() { </li></ul><ul><li>$items['meeting_page'] = array(...
Altering Other Modules <ul><li>Edit user_login_form?  (l. 1,154 of user.module)  </li></ul><ul><li>Use hook_form_ user_log...
Alter Hooks Build menus Render node Build blocks Perform Alterations hook_menu hook_node_view hook_xxx_alter
Not only forms can be altered... <ul><li>hook_link_alter </li></ul><ul><li>hook_mail_alter </li></ul><ul><li>hook_menu_alt...
More Resources <ul><li>http://api.drupal.org  – lists all available hooks, together with example usage </li></ul><ul><li>h...
Things to Remember <ul><li>Custom modules add new functionality / alter other modules by implementing hooks </li></ul><ul>...
Upcoming SlideShare
Loading in...5
×

Creating Custom Drupal Modules

16,537

Published on

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

Published in: Technology
1 Comment
6 Likes
Statistics
Notes
  • really infomative..ease of understanding...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
16,537
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
272
Comments
1
Likes
6
Embeds 0
No embeds

No notes for slide

Creating Custom Drupal Modules

  1. 1. Creating Custom Drupal Modules Drupal East Anglia UG – 16/6/2010 Alastair Aitchison
  2. 2. <ul><li>Independent developer based in Norwich </li></ul><ul><li>3 years Drupal experience </li></ul><ul><li>Current maintainer of Question module </li></ul><ul><li>Contributed to Automatic Nodetitles , Node Import , Examples , Synonyms , Tagadelic , WebSnapr ... </li></ul><ul><li>tanoshimi AlastairA [email_address] </li></ul>About Me
  3. 3. Types of Module
  4. 4. Anatomy of a Module <ul><li>Required </li></ul><ul><li>.info – metadata describing the module </li></ul><ul><li>.module – the module PHP code </li></ul><ul><li>Optional </li></ul><ul><li>.install – install / uninstall scripts </li></ul><ul><li>.css – stylesheets </li></ul><ul><li>.inc – additional include files </li></ul><ul><li>.test – unit test files </li></ul>
  5. 5. Where Do Module Files Reside? <ul><li>CORE </li></ul><ul><li>/modules/ subdirectory </li></ul><ul><li>Don’t edit or place other modules here! </li></ul><ul><li>CONTRIB & CUSTOM </li></ul><ul><li>/sites/all/modules/ subdirectory </li></ul><ul><li>Directory name matches module name </li></ul>
  6. 6. .info file <ul><li>Metadata about the module </li></ul><ul><li>Name, description, version, dependencies etc. </li></ul><ul><li>EXAMPLE! </li></ul>
  7. 7. meeting.info <ul><li>name = Meeting Demo Module </li></ul><ul><li>description = This is a demo module </li></ul><ul><li>for the Drupal UG meeting </li></ul><ul><li>core = 7.x </li></ul><ul><li>version = 7.0 - alpha </li></ul><ul><li>files[] = meeting.module </li></ul>
  8. 8. Drupal’s Hook System Build menus Render node Build blocks hook_menu hook_node_view
  9. 9. .module file <ul><li>Implements one or more Drupal hooks – PHP functions that get called by Drupal </li></ul><ul><li>Proper naming ensures that functions are automatically recognised and fired at the right time </li></ul><ul><li>Syntax is modulename_hookname </li></ul><ul><li>EXAMPLE! </li></ul>
  10. 10. meeting.module <ul><li><?php </li></ul><ul><li>function meeting_menu() { </li></ul><ul><li>$items['meeting_page'] = array( </li></ul><ul><li>'title' => 'My newmenu item', </li></ul><ul><li>'description' => 'This is a test menu item', </li></ul><ul><li>'page callback' => 'meeting_page', </li></ul><ul><li>'access callback' => TRUE, </li></ul><ul><li>); </li></ul><ul><li>return $items; </li></ul><ul><li>} </li></ul><ul><li>function meeting_page() { </li></ul><ul><li>return 'Yay! This worked.... probably.'; </li></ul><ul><li>} </li></ul>
  11. 11. Altering Other Modules <ul><li>Edit user_login_form? (l. 1,154 of user.module) </li></ul><ul><li>Use hook_form_ user_login_form _alter </li></ul>How to change this? NO! YES!
  12. 12. Alter Hooks Build menus Render node Build blocks Perform Alterations hook_menu hook_node_view hook_xxx_alter
  13. 13. Not only forms can be altered... <ul><li>hook_link_alter </li></ul><ul><li>hook_mail_alter </li></ul><ul><li>hook_menu_alter </li></ul><ul><li>hook_profile_alter </li></ul><ul><li>hook_schema_alter </li></ul><ul><li>... </li></ul>
  14. 14. More Resources <ul><li>http://api.drupal.org – lists all available hooks, together with example usage </li></ul><ul><li>http://drupal.org/project/modules - contrib module repository </li></ul><ul><li>http://www.apress.org – publishers of Pro Drupal Development </li></ul>
  15. 15. Things to Remember <ul><li>Custom modules add new functionality / alter other modules by implementing hooks </li></ul><ul><li>Require .info (metadata) and .module (code) </li></ul><ul><li>Create in a subdirectory of sites/all/modules </li></ul><ul><li>DON’T EDIT CORE! Use hook_xxx_alter </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×