Drupal module development
-
Upload
damjan-cvetan -
Category
Technology
-
view
269 -
download
1
description
Transcript of Drupal module development
![Page 1: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/1.jpg)
My first Drupal module! Why not?
![Page 2: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/2.jpg)
About @Cvetko • Started as PHP developer with
custom CMS • “You need Linux” they said • I know Drupal since version 4.x
• Working as Sys admin, Drupal developer, iOS developer, etc.
Twitter @Cvetko Skype damjan.cvetan Email [email protected]
![Page 3: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/3.jpg)
Drupal
• Available from drupal.org. • Runs on every machine with
PHP, supported database and web server.
• Very customizable (themes, modules).
• Good documented. • No limits.
![Page 4: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/4.jpg)
A whole bunch of modules
![Page 5: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/5.jpg)
Which one to use?
![Page 6: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/6.jpg)
Missing a module?
![Page 7: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/7.jpg)
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
![Page 8: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/8.jpg)
Where to?
Drupal
Core Modules
Contributed Modules
Custom Module
Core Themes
Contributed Themes
Custom Theme
/sites/[all|mysite.com]/custom
![Page 9: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/9.jpg)
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
![Page 10: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/10.jpg)
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.
![Page 11: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/11.jpg)
*.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, …
![Page 12: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/12.jpg)
Checkpoint
• Navigate to Modules section on your site • You should see your module • Enable it!
![Page 13: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/13.jpg)
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!
![Page 14: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/14.jpg)
Half way there
![Page 15: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/15.jpg)
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
![Page 16: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/16.jpg)
How does it work?
Call for hook: hook_menu()
Drupal runFme Drupal runFme
Call dispatch
Foreach (enabled_module): ! module_name_menu(); !end foreach; !
locale_menu() !user_menu() !contact_menu() !help_menu() !… !… !dc_stats_menu() !… !… !trigger_menu() !path_menu() !
![Page 17: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/17.jpg)
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/
![Page 18: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/18.jpg)
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
![Page 19: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/19.jpg)
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.
![Page 20: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/20.jpg)
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.)
![Page 21: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/21.jpg)
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 }
![Page 22: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/22.jpg)
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!
![Page 23: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/23.jpg)
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.
![Page 24: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/24.jpg)
Books
• Drupal 7 Development by Example
• Beginning Drupal 7
• Drupal 7 Module Development
• …
• …
![Page 25: Drupal module development](https://reader033.fdocuments.in/reader033/viewer/2022042713/548aacb6b47959cb4c8b478e/html5/thumbnails/25.jpg)
Q & A