SlideShare a Scribd company logo
My	
  first	
  Drupal	
  module!	
  
         Why	
  not?	
  
About @Cvetko
                       •  Started as PHP developer with
                          custom CMS
                       •  “You need Linux” they said
                       •  I know Drupal since version 4.x
Twitter
@Cvetko
                       •  Working as Sys admin, Drupal
Skype                     developer, iOS developer, etc.
damjan.cvetan

Email
damjan@agiledrop.com
Drupal

•  Available from drupal.org.
•  Runs on every machine with
   PHP, supported database and
   web server.
•  Very customizable (themes,
   modules).
•  Good documented.
•  No limits.
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
  –  Written by community
  –  Shared under the same GNU Public License
•  Custom
  –  Created by website developer
Where to?

                            Drupal	
  
   Core	
  Modules	
                    Core	
  Themes	
  
         Contributed	
  
                                        Contributed	
  Themes	
  
          Modules	
  

      Custom	
  Module	
                   Custom	
  Theme	
  

   /sites/[all|mysite.com]/custom	
  
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
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.
*.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, …
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
•    Constants should be all-uppercase
•    Comments are your friends!
Half way there
Hook(s)
hook – [hoo k], noun Ÿ a curved or angular piece
of 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
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	
  
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/
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
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.
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.)
Get some real data
funcFon	
  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	
  
}	
  
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!
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.
Books

•  Drupal 7 Development by Example
•  Beginning Drupal 7
•  Drupal 7 Module Development
•  …
•  …
Q	
  &	
  A	
  

More Related Content

PDF
Jooctrine - Doctrine ORM in Joomla!
PPTX
Drupal Camp Porto - Developing with Drupal: First Steps
PPTX
Drupal 7 — Circle theme
PDF
Drupal 8: Routing & More
PPTX
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
PDF
lab56_db
PPTX
Let's write secure Drupal code! - Drupal Camp Poland 2019
PDF
Moodle 3.3 - API Change Overview #mootieuk17
Jooctrine - Doctrine ORM in Joomla!
Drupal Camp Porto - Developing with Drupal: First Steps
Drupal 7 — Circle theme
Drupal 8: Routing & More
Let's write secure Drupal code! - DrupalCamp Oslo, 2018
lab56_db
Let's write secure Drupal code! - Drupal Camp Poland 2019
Moodle 3.3 - API Change Overview #mootieuk17

What's hot (20)

PPTX
Let's write secure drupal code! - Drupal Camp Pannonia 2019
PPTX
PDF
[PyConTW 2013] Write Sublime Text 2 Packages with Python
PDF
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
PDF
Drupal 8 Sample Module
PDF
backend
PPTX
Let's write secure Drupal code! DUG Belgium - 08/08/2019
PDF
Drupal vs WordPress
PDF
Drupal 7 Theming - Behind the scenes
PDF
Drupal 7 database api
PPTX
PHP Basics and Demo HackU
PDF
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
PDF
13th Sep, Drupal 7 advanced training by TCS
PDF
Drupal 8 版型開發變革
PPT
PDF
Drupal Module Development
PDF
I use drupal / 我是 OO 師,我用 Drupal
PDF
Drupal 8: Forms
KEY
Tricky Migrations
PDF
PHP 5.3 Overview
Let's write secure drupal code! - Drupal Camp Pannonia 2019
[PyConTW 2013] Write Sublime Text 2 Packages with Python
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
Drupal 8 Sample Module
backend
Let's write secure Drupal code! DUG Belgium - 08/08/2019
Drupal vs WordPress
Drupal 7 Theming - Behind the scenes
Drupal 7 database api
PHP Basics and Demo HackU
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
13th Sep, Drupal 7 advanced training by TCS
Drupal 8 版型開發變革
Drupal Module Development
I use drupal / 我是 OO 師,我用 Drupal
Drupal 8: Forms
Tricky Migrations
PHP 5.3 Overview
Ad

Viewers also liked (16)

PDF
Aegir Cpanel for Drupal Sites
PDF
Essential things that should always be in your car
PDF
Activism x Technology
PDF
How to Battle Bad Reviews
PDF
Catálogo Lexus NX
PDF
Hakkımızda
PPSX
Marie Brandhøj Wiuff fra KORA - borger og lægeperspektiver
PPTX
Γ9. Η εκστρατεία του Δράμαλη - Δερβενάκια
PPTX
High leverage brewster post
PPTX
บทที่ 2 การเคลื่อนที่ของสิ่งมีชีวิต
PDF
Eden Brown Built Environment EVP Document
DOCX
DEB CV 2015
DOC
Horarios talleres de acompañamientos actualizados 25 enero 2016
PDF
The Ultimate Guide To ACA Compliance
PDF
Farkımız
Aegir Cpanel for Drupal Sites
Essential things that should always be in your car
Activism x Technology
How to Battle Bad Reviews
Catálogo Lexus NX
Hakkımızda
Marie Brandhøj Wiuff fra KORA - borger og lægeperspektiver
Γ9. Η εκστρατεία του Δράμαλη - Δερβενάκια
High leverage brewster post
บทที่ 2 การเคลื่อนที่ของสิ่งมีชีวิต
Eden Brown Built Environment EVP Document
DEB CV 2015
Horarios talleres de acompañamientos actualizados 25 enero 2016
The Ultimate Guide To ACA Compliance
Farkımız
Ad

Similar to Drupal module development (20)

PDF
Gajendra sharma Drupal Module development
ODP
Custom module and theme development in Drupal7
PDF
2007 Fsoss Drupal Under The Hood
PDF
Drupal 7-api-2010-11-10
PPTX
Drupal module development
ODP
Intro to drupal module internals asheville
PPT
Drupal 6 in a nutshell
PDF
Advanced moduledevelopment d6_slideshare
KEY
Modules Building Presentation
PDF
Drupal as a Programmer-Friendly CMS at ConFoo
PDF
Domas monkus drupal module development
PDF
Intro to drupal_7_architecture
PPT
Drupal Modules
PPT
Drush. Why should it be used?
PDF
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
PPT
How to? Drupal developer toolkit. Dennis Povshedny.
PPT
DRUPAL Menu System
PPT
Drupal Menu System
ODP
Drupal development
PDF
Drupal Day 2012 - Automating Drupal Development: Make!les, Features and Beyond
Gajendra sharma Drupal Module development
Custom module and theme development in Drupal7
2007 Fsoss Drupal Under The Hood
Drupal 7-api-2010-11-10
Drupal module development
Intro to drupal module internals asheville
Drupal 6 in a nutshell
Advanced moduledevelopment d6_slideshare
Modules Building Presentation
Drupal as a Programmer-Friendly CMS at ConFoo
Domas monkus drupal module development
Intro to drupal_7_architecture
Drupal Modules
Drush. Why should it be used?
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
How to? Drupal developer toolkit. Dennis Povshedny.
DRUPAL Menu System
Drupal Menu System
Drupal development
Drupal Day 2012 - Automating Drupal Development: Make!les, Features and Beyond

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Approach and Philosophy of On baking technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
sap open course for s4hana steps from ECC to s4
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
cuic standard and advanced reporting.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
KodekX | Application Modernization Development
PPTX
MYSQL Presentation for SQL database connectivity
Spectral efficient network and resource selection model in 5G networks
Unlocking AI with Model Context Protocol (MCP)
Programs and apps: productivity, graphics, security and other tools
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Electronic commerce courselecture one. Pdf
Approach and Philosophy of On baking technology
Network Security Unit 5.pdf for BCA BBA.
Mobile App Security Testing_ A Comprehensive Guide.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Building Integrated photovoltaic BIPV_UPV.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
sap open course for s4hana steps from ECC to s4
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
cuic standard and advanced reporting.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
KodekX | Application Modernization Development
MYSQL Presentation for SQL database connectivity

Drupal module development

  • 1. My  first  Drupal  module!   Why  not?  
  • 2. About @Cvetko •  Started as PHP developer with custom CMS •  “You need Linux” they said •  I know Drupal since version 4.x Twitter @Cvetko •  Working as Sys admin, Drupal Skype developer, iOS developer, etc. damjan.cvetan Email damjan@agiledrop.com
  • 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. A whole bunch of modules
  • 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. Where to? Drupal   Core  Modules   Core  Themes   Contributed   Contributed  Themes   Modules   Custom  Module   Custom  Theme   /sites/[all|mysite.com]/custom  
  • 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. 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. *.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. Checkpoint •  Navigate to Modules section on your site •  You should see your module •  Enable it!
  • 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!
  • 15. Hook(s) hook – [hoo k], noun Ÿ a curved or angular piece of 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. 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. 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. 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. 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. 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. Get some real data funcFon  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. 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. 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. Books •  Drupal 7 Development by Example •  Beginning Drupal 7 •  Drupal 7 Module Development •  … •  …
  • 25. Q  &  A