Intro to drupal module internals asheville

Embed Size (px)

Citation preview

  1. 1. Intro To Drupal Module Internals Greg Monroe Longsight, Inc. Triangle Drupal Users Group
  2. 2. How will this help? Drupal is user contributed. This means that documentation is often lacking. The people writing modules generally say Read the code Knowing some basics about Drupal module internals, even if you can't program, will help you use even the worst documented module.
  3. 3. Skills and Tools A very basic knowledge of PhP (e.g. can kinda follow the basic logic but may not be able to write it) A decent (plain) text editor ( e.g. Notepad++, Text Edit, Gedit ) Optionally, you could find a more PhP friendly editor that displays a function list and allows for searching thru multiple file/directories. (SciTE, Geany, NotePad++ with plugins, Eclipse, Aptana, Zend)
  4. 4. How modules work Core finds them via modules admin page Stores info about them in System DB table Enabling module sets flag in System DB table, creates required DB tables, does initial setup and registration duties. Main code for enabled modules loaded at page boot by core. Module code gets called via hooks by core and other modules Module Magic happens
  5. 5. What are Hooks Drupal is based on hooks Modules use them to extend Drupal and to extend themselves. Hooks are associated with key actions, e.g. saving a node. When a hook action happens, core or a module with hooks calls all modules that have implemented the hook for that action.
  6. 6. Some Hook Fundamentals Hooks are documented like hook_name, e.g. hook_block_info Modules implement hooks as functions with names that replace hook with the module name. E.g, the foo module would implement hook_menu as foo_menu Core hooks usually are in *.module files
  7. 7. POP QUIZ If you read something like: The foo module uses hook_menu to do that What name will this hook use?
  8. 8. Answer: This translates into a function with the form: foo_menu E.g.: function foo_menu() { Extra credit for knowing it might be in the foo.module file.
  9. 9. Additional Reading A good place to start learning about core hooks is http://api.drupal.org/ Search for Hooks for the main page or search for hook_ for a specific hook.
  10. 10. Useful Module Splunking Hooks Hook_help Hook_menu Hook_block_info Hook_theme Hook_permision Hook_views_api Note: You can find full documentation on hook by googling: Drupal api hook_
  11. 11. Module Directory Files for a module will be under a common directory. The name of this directory is the module's basename. E.g., Organic group is og and Node Gallery uses node_gallery Note: Module base directories can exist under other modules, e.g. og_views is found in og/modules/og_views
  12. 12. Module Files Required: .info .module Recommended: README(.txt) LICENSE(.txt) .install Naming Conventions: *.inc Include files loaded as needed *.pages.inc code needed for general pages *.admin.inc code needed for admin pages *.tpl.php Override-able theme template files *.api.php Documents the hooks this module has
  13. 13. An Example of Module files The Organic Group Module
  14. 14. Finding a Module Finding a module that is creating some page or block from the displayed page is hard and beyond this talk If you know the Human name, e.g. Organic Groups, you can find the base name from the drupal.org/project/ URL. Knowing this, see if you can download it from Drupal.org. Looking at a module outside your web server is MUCH safer. But if you think your sites code is custom or you cant find the Drupal Project.
  15. 15. Wheres Wald..err.. the Module Modules can live many places but by convention they generally live under one of three directories: /modules (Core only!) /sites/all/modules (for all sites) /sites/default/modules Note: In multisite setups, the sites/default part above may be your host name, e.g. sites/www.my.com Next check to see if a basename directory exists if not, use a file search tool to look for .info under these directories.
  16. 16. The .info file The module.info file contains: Basic description of the module. Dependencies Object class files used by this module. CSS and Script files Configure URL
  17. 17. The .Module File The .module file contains the core code for the module and generally all the hooks were interested in.
  18. 18. Finding Hooks With the .module file in an editor: If your editor show functions, just look for the _ postfix, e.g. _menu Search for hook_, e.g. hook_menu Search for _, e.g. og_menu Look for _, e.g. _menu
  19. 19. Hook_Help Notes: Supplies text for Drupal Advanced help system Case statements URL help applies to Return statements - Help text
  20. 20. Hook_Menu Notes: Defines URLs (as menu items or just pages) for pages the module creates $items[] statements URL of page - %xxx indicate argument substitution title array key - Menu/Page title page callback array key - function to create page access callback array key - function that determines access
  21. 21. Hook_Block_Info Notes: - Defines blocks this module adds - info array key is name shown in block admin display The block_view hook defines how the block contents are created The block_configure hook (not shown) defines any block specific configuration options
  22. 22. Hook_Theme Notes: _xxx keys - These define the theme hooks for this module arguments key - Defines the values passed to the theme hook template key - Indicates this theme hook is a .tpl.php template file path key - The directory the tpl.php file is located. Code for non-template hooks will be in theme_ functions.
  23. 23. Hook_Permission Notes: Defines any module specific permissions. Note that many modules use PhP constants to define permissions. So, look at these to get the names used in the permission settings screens.
  24. 24. Hook_views_api If you find this hook, you know that this module supports views and might be supplying blocks and pages thru code embedded views it installs. The path option will indicate where the views related .inc files will live. If you look thru these you will generally find the names of views this module creates. Other places to look for views information is in the Module .info file.
  25. 25. Some Advanced Hooks hook_node_* A set of hooks to modify the node save, display and load options. hook_user_* A set of hooks to modify user actions, e.g. load, save, display, login, etc. hook_form_alter Allows any Drupal form to be modified by another module hook_schema Defines database tables used by this Module. This will be in the .install file. hook_cron Defines any cron jobs a module needs hook_*_alter Functions of this form generally are using a hook to alter something that core or a module provides.
  26. 26. Questions / More Info The book Pro Drupal Development is a good reference Drupal API site has lots of docs on hook DrupalContrib.org has lots of docs on non- core hooks Of course Drupal.org and general search will help you find lots of good stuff. Especially if you search for hook_