26
Developing for Drupal: first steps, tips & tricks Drupal Camp Portugal 2012 Luís Carneiro http://drupal.org/user/ 780690 [email protected]

Drupal Camp Porto - Developing with Drupal: First Steps

Embed Size (px)

DESCRIPTION

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.

Citation preview

Page 1: 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]

Page 2: Drupal Camp Porto - Developing with Drupal: First Steps

Resources

• The example sandbox module– http://tinyurl.com/portocamp-module

Page 3: Drupal Camp Porto - Developing with Drupal: First Steps

Agenda

• First steps• Some basics of the Drupal API• Presentation of coding examples• Questions and Discussion

Page 4: Drupal Camp Porto - Developing with Drupal: First Steps

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)

Page 5: Drupal Camp Porto - Developing with Drupal: First Steps

Debugger is your friend

See how the information flowsTrack bugs and understand code

Page 6: Drupal Camp Porto - Developing with Drupal: First Steps

Follow the coding standards

• drupal.org/coding-standards

• http://tinyurl.com/configuring-netbeans

• Use the coder module to validate your code

Page 7: Drupal Camp Porto - Developing with Drupal: First Steps

What is assumed

Content Types Blocks Taxonomies Users

Modules Themes Fields

You know the basic components of Drupal

Page 8: Drupal Camp Porto - Developing with Drupal: First Steps

Drupal Stack

Drupal Theme

Contributed Modules Custom Modules

Drupal Core

Page 9: Drupal Camp Porto - Developing with Drupal: First Steps

Drupal API family

Module System

Theme System Form API

Field API DB Layer Schema API

Menu System

Search API …

Page 10: Drupal Camp Porto - Developing with Drupal: First Steps

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!

Page 11: Drupal Camp Porto - Developing with Drupal: First Steps

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

Page 12: Drupal Camp Porto - Developing with Drupal: First Steps

It all resumes to…

module_invoke_all(‘hook_name’)or

module_invoke(‘module_name’,’hook_name’)

Page 13: Drupal Camp Porto - Developing with Drupal: First Steps

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

Page 14: Drupal Camp Porto - Developing with Drupal: First Steps

.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)

Page 15: Drupal Camp Porto - Developing with Drupal: First Steps

Module Purpose

• Create blocks• Create pages and forms• Create a new field type• Extend/customize Drupal core • Extend/customize Contributed modules• …

Page 16: Drupal Camp Porto - Developing with Drupal: First Steps

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)

Page 17: Drupal Camp Porto - Developing with Drupal: First Steps

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)

Page 18: Drupal Camp Porto - Developing with Drupal: First Steps

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

Page 19: Drupal Camp Porto - Developing with Drupal: First Steps

Theme System

Template File Template Function

Preprocessing Functions

• Set the variables for the templates• Define the logic

or

Page 20: Drupal Camp Porto - Developing with Drupal: First Steps

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));…

Page 21: Drupal Camp Porto - Developing with Drupal: First Steps

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

Page 22: Drupal Camp Porto - Developing with Drupal: First Steps

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!

Page 23: Drupal Camp Porto - Developing with Drupal: First Steps

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.

Page 24: Drupal Camp Porto - Developing with Drupal: First Steps

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>

Page 25: Drupal Camp Porto - Developing with Drupal: First Steps

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

Page 26: Drupal Camp Porto - Developing with Drupal: First Steps

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