1 / 33

1/30/11

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

terena
Download Presentation

1/30/11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. How to chop an onion?

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

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

  5. My first little module

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

  7. mymodule.info • name = My module • description = "The description of my module" • core = 7.x • ; Module dependencies • dependencies[] = taxonomy

  8. The hook system

  9. 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!

  10. Example: hook_init • Perform setup tasks • This hook is run at the beginning of the page request

  11. Example: hook_init • Perform setup tasks • This hook is run at the beginning of the page request

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

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

  14. Form system • $form['title'] = array( • '#type' => 'textfield', • '#title' => 'My title', • '#required' => TRUE, • ); • $form['description'] = array( • '#type' => 'textarea', • '#title' => 'Description', • ); • DEMO

  15. Improve the user formUser register / log in

  16. Improve the user formUser register / log in

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

  18. New revisionMake the log message required

  19. New revisionMake the log message required

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

  21. SummaryWhat did you learn?

  22. Questions? • Twitter: @BarisW • Drupal.org / IRC: BarisW • Web: http://www.bariswanschers.com/ • Mail: baris.wanschers@sogeti.nl

More Related