Presentation for the talk I gave at Drupal Camp Baltics (2010 september 25th in Riga). The main focus is on the basic concepts behind drupal module development. A short overview of available APIs and examples of the most common hooks are given.
3. Overview Drupal’s components Modules APIs
What is drupal?
CMS
Node system
Taxonomy
CCK & Views
4. Overview Drupal’s components Modules APIs
What is drupal?
CMS FRAMEWORK
Node system Module Architecture
Taxonomy Theming
CCK & Views APIs
5. Overview Drupal’s components Modules APIs
Drupal development workflow
Where did my time go?
6. Overview Drupal’s components Modules APIs
Should You develop a custom module?
7. Overview Drupal’s components Modules APIs
Considerations
Not-invented-here?
Maintenance - you will need to maintain and update even if
you do not release it.
8. Overview Drupal’s components Modules APIs
Considerations
Not-invented-here?
Maintenance - you will need to maintain and update even if
you do not release it.
Investment - making custom one-off modules is not overly
cost-effective. Will you need it again? In the foreseeable
future?
9. Overview Drupal’s components Modules APIs
Considerations
Not-invented-here?
Maintenance - you will need to maintain and update even if
you do not release it.
Investment - making custom one-off modules is not overly
cost-effective. Will you need it again? In the foreseeable
future?
Lonely path - it takes time to flush out all the bugs. Only in
this case you will almost always be the only one finding them.
10. Overview Drupal’s components Modules APIs
Alternatives
To custom modules
Contrib - over 4500 modules, are you sure your problem
hasn’t been tackled?
11. Overview Drupal’s components Modules APIs
Alternatives
To custom modules
Contrib - over 4500 modules, are you sure your problem
hasn’t been tackled?
Extending similar modules - base your work on a tested
codebase.
12. Overview Drupal’s components Modules APIs
Alternatives
To custom modules
Contrib - over 4500 modules, are you sure your problem
hasn’t been tackled?
Extending similar modules - base your work on a tested
codebase.
One-off solutions - if it’s a rare problem, sometimes hacks
are not the worst idea.
13. Overview Drupal’s components Modules APIs
Enough talk
Let’s get to the programming
Things you need to know:
Drupal’s architecture
Drupal module structure
Drupal core subsystems and APIs
Coding conventions
14. Overview Drupal’s components Modules APIs
Enough talk
Let’s get to the programming
Things you need to know:
Drupal’s architecture
Drupal module structure
Drupal core subsystems and APIs
Coding conventions
Things you REALLY need to know:
PHP
MySQL
HTML, CSS
javascript (probably)
26. Overview Drupal’s components Modules APIs
How does this work?
27. Overview Drupal’s components Modules APIs
Hooking things together
Delayed publishing
Problem
We need to publish nodes of unverified users not immediately, but
after a certain wait period.
28. Overview Drupal’s components Modules APIs
Hooking things together
Delayed publishing
Problem
We need to publish nodes of unverified users not immediately, but
after a certain wait period.
Hooks used
hook_perms
To define a permission ’Publish nodes immediately’.
hook_nodeapi
To forcefully unpublish new nodes.
hook_cron
To publish nodes whose wait period has already passed.
29. Overview Drupal’s components Modules APIs
Hooking things together
Term-node synchronization
Task
Create a node for every term in the dictionary and vice-versa,
constantly synchronizing the contents of a node’s body and the
term’s description.
30. Overview Drupal’s components Modules APIs
Hooking things together
Term-node synchronization
Task
Create a node for every term in the dictionary and vice-versa,
constantly synchronizing the contents of a node’s body and the
term’s description.
Hooks used
hook_form_alter
To add options to taxonomy vocabulary edit form.
hook_taxonomy
To react to term creation, updates and deletes.
hook_node_info
To define a custom node type.
hook_insert, hook_update, hook_delete
To react to node creation, updates and deletes.
31. Overview Drupal’s components Modules APIs
So where do we put this?
32. Overview Drupal’s components Modules APIs
The structure of a drupal module
module_name/
* module_name.info - module description
module_name.install - installation/uninstallation
* module_name.module - module code
33. Overview Drupal’s components Modules APIs
The .info file
name = Devel
description = Various blocks, pages..
package = Development
dependencies[] = menu
core = 6.x
34. Overview Drupal’s components Modules APIs
The .module file
A simple php file
Hooks and internal functions
36. Overview Drupal’s components Modules APIs
3 most common APIs/hooks
37. Overview Drupal’s components Modules APIs
Menu API
Tasks
routing
menus
breadcrumbs
38. Overview Drupal’s components Modules APIs
Menu API
An example
function devel_menu() {
$items = array();
$items[’devel/queries’] = array(
’title’ => ’Database queries’,
’page callback’ => ’devel_queries’,
’access callback’ => ’devel_menu_access_store_queries’,
’access arguments’ => array(),
’menu_name’ => ’devel’,
);
...
return $items;
}
39. Overview Drupal’s components Modules APIs
Menu API
40. Overview Drupal’s components Modules APIs
Form API
Tasks
Form creation and rendering
Form altering
Processing
Form validation and security
41. Overview Drupal’s components Modules APIs
Form API
What’s in a form?
Forms are defined as nested arrays
$form = array();
$form[’mail’] = array(
’#type’ => ’textfield’,
’#title’ => t(’Email’),
’#size’ => 20,
’#maxlength’ => 128,
’#required’ => TRUE,
);
return drupal_get_form($form);
42. Overview Drupal’s components Modules APIs
Form API
Altering forms
Any Form API form can be altered by other modules
Fields can be added, removed or changed
Additional validation and submit processing can be added
43. Overview Drupal’s components Modules APIs
Form API
hook_form_alter
function hook_form_alter(&$form, $form_state, $form_id)
$form
The form array - passed by reference, can be altered
$form_state
The form’s state (if the form has already been rendered)
$form_id
The id of the form being processed
44. Overview Drupal’s components Modules APIs
Form API
An example
function path_form_alter(&$form, $form_state, $form_id) {
if (isset($form[’type’]) && isset($form[’#node’])
&& $form[’type’][’#value’] .’_node_form’ == $form_id) {
$path = isset($form[’#node’]->path) ? $form[’#node’]->path : NULL;
$form[’path’] = array(
’#type’ => ’fieldset’,
’#title’ => t(’URL path settings’),
’#collapsible’ => TRUE,
’#collapsed’ => empty($path),
’#access’ => user_access(’create url aliases’),
’#weight’ => 30,
);
...
45. Overview Drupal’s components Modules APIs
hook_node_api
Tasks
React to all operations on nodes
Additional processing or validation of node fields
Add additional content to nodes on load
46. Overview Drupal’s components Modules APIs
function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
$node
The node object
$op
The operation being performed
$a3, $a4
Additional parameters, dependant on operation
47. Overview Drupal’s components Modules APIs
Node API
Operations
load
insert
update
delete
validate
presave
etc.
48. Overview Drupal’s components Modules APIs
Node API
An example
function nodeasblock_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case ’load’:
$node->nodeasblock = db_result(
db_query(’SELECT nid FROM {nodeasblock}
WHERE nid = %d’, $node->nid));
break;
case ’insert’:
...
49. Overview Drupal’s components Modules APIs
Tips and tools
50. Overview Drupal’s components Modules APIs
Tools
devel module
cache operations, dummy content generation, debugging aids
coder module
coding standards validation, basic module security tips
drupal api module
have a local instance of api.drupal.org
simpletest module
automated unit testing of your module (highly encouraged)
version control
cvs, svn, git, mercurial - anything is better than nothing
51. Overview Drupal’s components Modules APIs
Tips
Coding practices
Coding standards
keep your code clean and documented - if you ever decide to
release it
Caching
because performance matters
Splitting large module files
parts can be loaded on demand - less load on the system
Looking for help
#drupal
go to the irc.freenode.net irc channel for help
api.drupal.org
complete reference of drupal core APIs
52. Overview Drupal’s components Modules APIs
.. and never
53. Overview Drupal’s components Modules APIs
.. and never
ever
54. Overview Drupal’s components Modules APIs
.. and never
ever
hack
55. Overview Drupal’s components Modules APIs
.. and never
ever
hack
core