My	  first	  Drupal	  module!	           Why	  not?	  
About @Cvetko                       •  Started as PHP developer with                          custom CMS                  ...
Drupal•  Available from drupal.org.•  Runs on every machine with   PHP, supported database and   web server.•  Very custom...
A whole bunch of modules
Which one to use?
Missing a module?
Three kinds of modules (3 Cs)•  Core  –  Shipped with Drupal  –  Approved by core developers and community•  Contributed  ...
Where to?                            Drupal	     Core	  Modules	                    Core	  Themes	           Contributed	 ...
Module name•    Should be a UNIQUE “short name”•    Used in all file and function names•    Must start with a letter•    O...
Create a folder + module file•  Module name “dc_stats”.•  Create empty folder:  –  /sites/*/modules/custom/dc_stats/•  Cre...
*.info•  Required for every module!•  Standard .ini file format – key/value pairs  name = Drupal statistic!  description =...
Checkpoint•  Navigate to Modules section on your site•  You should see your module•  Enable it!
Coding standards•    Use an indent of 2 spaces, no tabs!•    UNIX line ending (n)•    Omit closing “?>” PHP tag•    Consta...
Half way there
Hook(s)hook – [hoo k], noun Ÿ a curved or angular pieceof metal or other hard substance for catching,pulling, holding, or...
How does it work?Foreach (enabled_module):!  module_name_menu();!                                                         ...
Hooks line up!•  hook_help() – Provides available   documentation.•  hook_menu() – For paths registration in   order to de...
API.drupal.org•  Drupal developer’s documentation.•  Doc for Drupal 4.6+.•  Describes function calls, their parameters   a...
Let’s code•  Define callback function dc_stats_page() as   follows:  funcFon	  dc_stats_page(){	  	  	    	  	  return	  "...
Hey Drupal! Come in!•  Register path with hook_menu().•  We will use basic return array structure.   funcFon	  dc_stats_me...
Get some real datafuncFon	  dc_stats_page(){	                                                                             ...
Happy ending•  Refresh your page and see your work.•  You can do much more – I’m certain!•  Use your PHP + any other knowl...
Links to consider•  http://api.drupal.org   http://drupalcontrib.org/•  http://buildamodule.com/•  http://drupal.org/proje...
Books•  Drupal 7 Development by Example•  Beginning Drupal 7•  Drupal 7 Module Development•  …•  …
Q	  &	  A	  
Upcoming SlideShare
Loading in...5
×

Drupal module development

333

Published on

Slides for Drupal Camp Alpe-Adria on April 13 2013 in Ljubljana (Slovenia). http://www.drupalalpeadria.org/

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
333
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Drupal module development

  1. 1. My  first  Drupal  module!   Why  not?  
  2. 2. About @Cvetko •  Started as PHP developer with custom CMS •  “You need Linux” they said •  I know Drupal since version 4.xTwitter@Cvetko •  Working as Sys admin, DrupalSkype developer, iOS developer, etc.damjan.cvetanEmaildamjan@agiledrop.com
  3. 3. Drupal•  Available from drupal.org.•  Runs on every machine with PHP, supported database and web server.•  Very customizable (themes, modules).•  Good documented.•  No limits.
  4. 4. A whole bunch of modules
  5. 5. Which one to use?
  6. 6. Missing a module?
  7. 7. Three kinds of modules (3 Cs)•  Core –  Shipped with Drupal –  Approved by core developers and community•  Contributed –  Written by community –  Shared under the same GNU Public License•  Custom –  Created by website developer
  8. 8. Where to? Drupal   Core  Modules   Core  Themes   Contributed   Contributed  Themes   Modules   Custom  Module   Custom  Theme   /sites/[all|mysite.com]/custom  
  9. 9. Module name•  Should be a UNIQUE “short name”•  Used in all file and function names•  Must start with a letter•  Only lower-case letters and underscores•  We will use name: “dc_stat” –  For display current node/user stats
  10. 10. Create a folder + module file•  Module name “dc_stats”.•  Create empty folder: –  /sites/*/modules/custom/dc_stats/•  Create new file “dc_stats.module” with opening PHP tag and NO closing tag!!•  Create new file “dc_stats.info” for meta information.
  11. 11. *.info•  Required for every module!•  Standard .ini file format – key/value pairs name = Drupal statistic! description = Provides some statistic data.! core = 7.x! package = Damjan Cvetan! Other optional keys: stylesheets, scripts, files, dependencies, …
  12. 12. Checkpoint•  Navigate to Modules section on your site•  You should see your module•  Enable it!
  13. 13. Coding standards•  Use an indent of 2 spaces, no tabs!•  UNIX line ending (n)•  Omit closing “?>” PHP tag•  Constants should be all-uppercase•  Comments are your friends!
  14. 14. Half way there
  15. 15. Hook(s)hook – [hoo k], noun Ÿ a curved or angular pieceof metal or other hard substance for catching,pulling, holding, or suspending something.•  Fundamental to Drupal modules.•  A way to interact with the core code.•  Occur at various points in execution thread.•  An event listener.•  Names as foo_bar() –  foo : module name, bar : hook name
  16. 16. How does it work?Foreach (enabled_module):! module_name_menu();! locale_menu()!end foreach;! user_menu()! contact_menu()! help_menu()! Call  dispatch   …! …! dc_stats_menu()! …! …! trigger_menu()! Call  for  hook:   path_menu()! hook_menu()   Drupal  runFme   Drupal  runFme  
  17. 17. Hooks line up!•  hook_help() – Provides available documentation.•  hook_menu() – For paths registration in order to define how URL request are handled.•  hook_init() – Run at the beginning of page request.•  hook_cron() – Called whenever a cron run happens.•  More at http://api.drupal.org/
  18. 18. API.drupal.org•  Drupal developer’s documentation.•  Doc for Drupal 4.6+.•  Describes function calls, their parameters and return values.•  You can see the code and “who” is calling this code within Drupal.•  http://api.drupal.org
  19. 19. Let’s code•  Define callback function dc_stats_page() as follows: funcFon  dc_stats_page(){          return  "Hello  world!  You’re  awesome!”;   }  •  This will return defined string on call.•  Put this code in dc_stats.module file.
  20. 20. Hey Drupal! Come in!•  Register path with hook_menu().•  We will use basic return array structure. funcFon  dc_stats_menu(){      $items[dc/stats-­‐page]  =  array(          Ftle  =>  Stats  info  page,          page  callback  =>  dc_stats_page,          access  arguments  =>  array(access  content),          type  =>  MENU_CALLBACK,      );      return  $items;   }  Visit URL: /dc/stats-page to see if it works.(You might need to clear cache first.)
  21. 21. Get some real datafuncFon  dc_stats_page(){          drupal_set_Ftle("Drupal  staFsFcs");      $node_count  =  $db_node_count;      $user_count  =  $db_users_count;      $header  =  array("InformaFon",  "Value");      $rows[]  =  array(Number  of  nodes:,  $node_count);      $rows[]  =  array(Number  of  users:,  $user_count);            return  theme_table(array(                                                        header  =>  $header,                                                        rows  =>  $rows,                                                        …                                                        …          ));  //  return  }  
  22. 22. Happy ending•  Refresh your page and see your work.•  You can do much more – I’m certain!•  Use your PHP + any other knowledge with existing Drupal functions, hooks and variables!
  23. 23. Links to consider•  http://api.drupal.org http://drupalcontrib.org/•  http://buildamodule.com/•  http://drupal.org/project/devel –  A suit of modules containing fun for module developers and themers.
  24. 24. Books•  Drupal 7 Development by Example•  Beginning Drupal 7•  Drupal 7 Module Development•  …•  …
  25. 25. Q  &  A  
  1. A particular slide catching your eye?

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

×