Drupal Camp Porto - Developing with Drupal: First Steps
-
Upload
luis-carneiro -
Category
Technology
-
view
2.611 -
download
0
description
Transcript of Drupal Camp Porto - Developing with Drupal: First Steps
Developing for Drupal: first steps, tips & tricks
Drupal Camp Portugal 2012Luís Carneiro
http://drupal.org/user/780690 [email protected]
Resources
• The example sandbox module– http://tinyurl.com/portocamp-module
Agenda
• First steps• Some basics of the Drupal API• Presentation of coding examples• Questions and Discussion
Set your Developing Environment
• Netbeans• Eclipse• Aptana• Komodo IDE• Coda• Vim• …
Look for:• Debugger integration• Function list view• Jump to function • Source control integration• Ability to search across files• Ability to configure syntax style
(follow coding standards)
Debugger is your friend
See how the information flowsTrack bugs and understand code
Follow the coding standards
• drupal.org/coding-standards
• http://tinyurl.com/configuring-netbeans
• Use the coder module to validate your code
What is assumed
Content Types Blocks Taxonomies Users
Modules Themes Fields
You know the basic components of Drupal
Drupal Stack
Drupal Theme
Contributed Modules Custom Modules
Drupal Core
Drupal API family
Module System
Theme System Form API
Field API DB Layer Schema API
Menu System
Search API …
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!
Hooks
hook_init hook_menu
hook_form_alter hook_theme
hook_mail hook_field_info
hook_schema …
Drupal Core has about 250 hooks!
See the full list:http://tinyurl.com/drupal-hooks
It all resumes to…
module_invoke_all(‘hook_name’)or
module_invoke(‘module_name’,’hook_name’)
What you need for a module
yourmodule.info yourmodule.module
yourmodule.install(optional)
README.txt(good practice)
Files created in:sites/all/modules/custom/yourmodule
.info File
name = Your module namedescription = Line description about module purposepackage = Group of modules it should belongcore = 7.x (or other version of Drupal)files[] = yourmodule.module (and all other files which code should run of every page)
Module Purpose
• Create blocks• Create pages and forms• Create a new field type• Extend/customize Drupal core • Extend/customize Contributed modules• …
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)
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)
Database Access
Static Queries
Dynamic queries
• Usually simple queries• Only Select queries may
be static
db_query($sql_query)
• OOP query object• Database agnostic • Possibility to alter query
elements
Full database API documentationhttp://drupal.org/developing/api/database
Theme System
Template File Template Function
Preprocessing Functions
• Set the variables for the templates• Define the logic
or
Theme Simple Example
function yourmodule_theme() { return array( 'custom_output' => array( 'variables' => array('node' => NULL), ), );}
function theme_custom_output($variables) { … return $output;}
Function template_custom_output($variables) { … return $output;}
function yourtheme_custom_output($variables) { … return $output;}
All valid theme definitions!
…print theme('custom_output', array('node' => $node));…
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-implementationshttp://tinyurl.com/render-arrays
I see a lot of arrays!$page = array( '#show_messages' => TRUE, '#theme' => 'page', '#type' => 'page', 'content' => array( 'system_main' => array(...), 'another_block' => array(...), '#sorted' => TRUE, ), 'sidebar_first' => array( ... ), 'footer' => array( ... ), ...);
In many cases, the data used to build a page (and all parts of it) is kept as structured arrays until the rendering stage
Enormous flexibility in changing the layout or content of a page and its components!
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.
Simplify with Drush
• Command line tool to do common Drupal actions– Possibility to create your own drush commands…
writing hooks!
Clear cachesDownload/Enable modulesSynchronize dev and production databasesCheck the log messages…
$ drush$ drush help <command>
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
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