340 likes | 482 Views
Hacking your way through the Drupal API, a themers intro Baris Wanschers (BarisW). 1/30/11. How to chop an onion?. Baris Wanschers. Drupal Specialist at Sogeti Board member of the Dutch Drupal foundation
E N D
Hacking your way through the Drupal API, a themers intro Baris Wanschers (BarisW) 1/30/11
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)
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 module • description = "The description of my module" • core = 7.x • ; Module dependencies • dependencies[] = taxonomy
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
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
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)
Questions? • Twitter: @BarisW • Drupal.org / IRC: BarisW • Web: http://www.bariswanschers.com/ • Mail: baris.wanschers@sogeti.nl