1/30/11 hacking your way through the drupal api, a themers intro baris wanschers (barisw)

33
1/30/11 Hacking your way through the Drupal API, a themers intro Baris Wanschers (BarisW)

Upload: branden-dennis

Post on 26-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

1/30/11

Hacking your way through the Drupal API, a themers introBaris Wanschers (BarisW)

How to chop an onion?

Baris Wanschers

• Drupal Specialist at Sogeti

• Board member of the Dutch Drupal foundation

• Maintainer of some small modules (Google Fonts, Termcase, Menu Force, Translate This button, etc)

• Wrote some patches for Drupal 7 Core

• Build www.drupal7releaseparty.org

Agenda

• The joy of a contrib modules

• They come with limits

• More modules to fix this? Or can we do this ourselves?

• Some usecases and example code (don’t panic, it’s just PHP)

My first little module

What do you need?

• A folder to store the files in (mymodule)

• An info file (mymodule.info) to describe the module

• A module file (mymodule.module) with the actual code

mymodule.info

name = My moduledescription = "The description of my module"core = 7.x

; Module dependenciesdependencies[] = taxonomy

The hook system

hook_what?

• Drupal modules are build around hooks.

• Hooks are functions that Drupal tries to access

• Replace hook_ with yourmodule_

• Example: mymodule_menu is a call to hook_menu in mymodule

• Check the API on hook_. You can use these!

Example: hook_init

• Perform setup tasks

• This hook is run at the beginning of the page request

Example: hook_init

• Perform setup tasks

• This hook is run at the beginning of the page request

The power is yoursIf you know just these hooks

• hook_menu_alter to change navigation items

• hook_form_alter to edit forms

Also interesting

• hook_node_* (hook_node_view, hook_node_load, etc)

• hook_user_* (hook_user_view, etc)

Menu system

function devdays_menu() { $items['hello-world'] = array( 'title' => 'Hello world', 'page callback' => 'devdays_hello_world', 'access arguments' => array('access content'), 'type' => MENU_NORMAL_ITEM, ); return $items;}

DEMO

Form system

$form['title'] = array( '#type' => 'textfield', '#title' => 'My title', '#required' => TRUE, ); $form['description'] = array( '#type' => 'textarea', '#title' => 'Description', );

DEMO

Improve the user formUser register / log in

Improve the user formUser register / log in

Let’s begin!Useful links

• Devel module: http://drupal.org/project/devel

• Drupal API: http://api.drupal.org/api/drupal

• Drush: http://drupal.org/project/drush

DEMO

New revisionMake the log message required

New revisionMake the log message required

SummaryWhat did you learn?

• Basic insight in the hook_system

• Few lines of code instead of huge modules and hours of searching

• hook_menu_alter to change menu items (also to override a function)

• hook_form_alter to change forms (and change classes/id’s)

SummaryWhat did you learn?

Questions?

• Twitter: @BarisW

• Drupal.org / IRC: BarisW

• Web: http://www.bariswanschers.com/

• Mail: [email protected]