The goal of this presentation is to give Drupal new comers some insights about key aspects of developing with Drupal.
The idea is to give the audience some guidelines about good practices of Drupal development along with some tips and, by a simple example application, present the most common and important structures/characteristics of the Drupal API.
3. Agenda
• First steps
• Some basics of the Drupal API
• Presentation of coding examples
• Questions and Discussion
4. Set your Developing Environment
• Netbeans
• Eclipse Look for:
• Debugger integration
• Aptana • Function list view
• Komodo IDE • Jump to function
• Source control integration
• Coda • Ability to search across files
• Ability to configure syntax style
• Vim (follow coding standards)
• …
5. Debugger is your friend
See how the information flows
Track bugs and understand code
6. Follow the coding standards
• drupal.org/coding-standards
• http://tinyurl.com/configuring-netbeans
• Use the coder module to validate your code
7. What is assumed
You know the basic components of Drupal
Content
Blocks Taxonomies Users
Types
Modules Themes Fields
9. Drupal API family
Module Theme
Form API
System System
Schema
Field API DB Layer
API
Menu Search
…
System API
10. Hook concept
• “Event listeners”
• Possibility to act in certain points of a page
execution
– To add our custom functionalities
– To alter other’s functionalities
• Drupal Moto: Don’t hack Core or
others code!
11. Hooks
hook_init hook_menu
Drupal Core has
hook_form_alter hook_theme
about 250 hooks!
See the full list:
http://tinyurl.com/drupal-hooks
hook_mail hook_field_info
hook_schema …
12. It all resumes to…
module_invoke_all(‘hook_name’)
or
module_invoke(‘module_name’,’hook_name’)
13. What you need for a module
Files created in:
sites/all/modules/custom/yourmodule
yourmodule.info yourmodule.module
yourmodule.install README.txt
(optional) (good practice)
14. .info File
name = Your module name
description = Line description about module purpose
package = Group of modules it should belong
core = 7.x (or other version of Drupal)
files[] = yourmodule.module (and all other files which
code should run of every page)
15. Module Purpose
• Create blocks
• Create pages and forms
• Create a new field type
• Extend/customize Drupal core
• Extend/customize Contributed modules
• …
16. Form API
• drupal_get_form($form_id)
– $form_id usually is <module_name>_<description>_form
– Builds a form structure
• Many form elements
– textfield, select, checkbox, radio, password…
– The full reference list http://tinyurl.com/drupal-form-api
• validate and submit handlers
– <form_id>_validate(&$form, &$form_state)
– <form_id>_submit(&form, &$form_state)
17. Cache in Drupal
• Internal cache
– Menu, Form, Path, Filter, Field…
– Cache Page (specific for anonymous users)
• The one you activate in “Performance” settings
• Custom cache
– Two simple functions:
• cache_set($cid, $data, $bin, $expire)
• cache_get($cid, $bin)
18. Database Access
Static Dynamic
Queries queries
• Usually simple queries • OOP query object
• Only Select queries may • Database agnostic
be static • Possibility to alter query
elements
db_query($sql_query)
Full database API documentation
http://drupal.org/developing/api/database
19. Theme System
Template File or Template Function
Preprocessing
Functions
• Set the variables for the templates
• Define the logic
20. Theme Simple Example
function theme_custom_output($variables) {
function yourmodule_theme() { …
return array( return $output;
'custom_output' => array( }
'variables' => array('node' => NULL),
),
); Function template_custom_output($variables) {
} …
return $output;
}
…
print theme('custom_output', array('node' => function yourtheme_custom_output($variables) {
…
$node));
return $output;
… }
All valid theme definitions!
21. Theming tips
• Use devel themer module to find the theming
elements on your page
• Clear theme registry cache when setting new
templates
Useful Resources:
http://tinyurl.com/drupal-theme-implementations
http://tinyurl.com/render-arrays
22. I see a lot of arrays!
$page = array(
'#show_messages' => TRUE,
'#theme' => 'page', In many cases, the data used to
'#type' => 'page',
'content' => array( build a page (and all parts of it) is
'system_main' => array(...), kept as structured arrays until the
'another_block' => array(...),
'#sorted' => TRUE, rendering stage
),
'sidebar_first' => array(
... Enormous flexibility in changing
),
'footer' => array( the layout or content of a page
... and its components!
),
...
);
23. Use Drupal’s utility functions
• Function l(): Formats an internal or external URL link
as an HTML anchor tag.
• Function t(): wrap text to be translated.
• Function drupal_set_message(): status message for
user performed operations
• Function watchdog(): logs system messages to drupal
log.
24. Simplify with Drush
• Command line tool to do common Drupal
actions
– Possibility to create your own drush
commands…writing hooks!
Clear caches $ drush
Download/Enable modules $ drush help <command>
Synchronize dev and production databases
Check the log messages
…
25. Version Control Everything
• Code + Configurations (in database)
– Code, easy to version control
– Configurations… there comes Features +
Strongarm module!
• Database configurations exported to code
• Tip: Keep your features small
26. Learning Resources
• Web
– http://api.drupal.org
– http://drupal.org/project/examples
– http://drupal.org/developing/api
• Training
– http://drupalize.me
– http://buildamodule.com/
• Books
– Drupal 7 Module Development
– Pro Drupal 7 Development
– Using Drupal 7
Notas do Editor
Referirquemais a diantevãoserapresentadosexemplos da utilidade do Debugger.
Apresentarmodule_invoke_all('init'); no ficheiro “common.inc”
Mostrar a form de administraçãocontruída.Mostrar a página da form apiAproveitarparafalar do “hook_menu” e “hook_permission”
Aproveitarestetópicoparafalar no módulo “examples”.
Apresentar a página de sandbox com uma query muito simples feita.Aproveitarparafalar do restante código.
Exemplos: page, item_list, table, select list, etcEach theme can take control over most of Drupal's output, and has complete control over the CSS.Utilizar Debugger parafazer debug nafunção: “portocamp_theme_registry_alter” e mostrarosdiversoselementos de theme definidos.
Mostrarexemplo do hook_themecriado.
Show command line with drush and execute simple commands.