Your SlideShare is downloading. ×
Learning the basics of the Drupal API
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Learning the basics of the Drupal API

3,913
views

Published on

Talk given at Drupalcamp Bucharest 2011.

Talk given at Drupalcamp Bucharest 2011.

Published in: Technology

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,913
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
44
Comments
0
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript

    • 1. LEARNING THE BASICS OF THE DRUPAL API Badiu Alexandru Drupalcamp Bucharest 2011
    • 2. THIS• We’re going to use Drupal 7• For beginners, intended as a continuation of “From zero to hero”• Going to assume you know basic concepts such as blocks and nodes Drupalcamp Bucharest 2011
    • 3. YOU’LL• The basics of module development• How to create blocks• How to check for permissions• How to create forms• How to create menu items• How to send email Drupalcamp Bucharest 2011
    • 4. MODULES• Modules are the building blocks• name.info• name.module• sites/all/modules Drupalcamp Bucharest 2011
    • 5. MODULES• Create blocks• Create content types• Create pages and forms• Augment Drupal core• Augment Drupal modules Drupalcamp Bucharest 2011
    • 6. DCAMP.M• DrupalCamp Forward• Allows a user to forward a node url to a friend• Displays a block on each node page• Can appear only on specific node types Drupalcamp Bucharest 2011
    • 7. DCAMP.M• dcamp.info name = DrupalCamp Forward description = Drupalcamp D7 Demo package = Drupalcamp version = 1.0 core = 7.x files[] = dcamp.module Drupalcamp Bucharest 2011
    • 8. DCAMP.M• dcamp.module• Is a php file• Contains the module code• Implements hooks Drupalcamp Bucharest 2011
    • 9. HOOKS• Hooks are callbacks• Everytime an action is performed in Drupal a specific hook is called• You can alter the action data• You can do unrelated things Drupalcamp Bucharest 2011
    • 10. HOOKS• hook_name• function dcamp_name($arg1, $arg2) {...} Drupalcamp Bucharest 2011
    • 11. HOOKS• http://api.drupal.org/api/drupal/ includes--module.inc/group/hooks/ 7• Lots of them• Block, Node, Forms, Images, Menus, Taxonomy, Permissions, Users Drupalcamp Bucharest 2011
    • 12. BLOCK• hook_block_info• hook_block_view• function dcamp_block_info()• function dcamp_block_view($delta = ‘’) Drupalcamp Bucharest 2011
    • 13. BLOCKfunction dcamp_block_info() { $blocks = array( forward_block => array( info => t(Forward node block), cache => DRUPAL_CACHE_PER_ROLE ) ); return $blocks;}function dcamp_block_view($delta = ) { $block = array( subject => t(Spread the word), content => Block contents. ); return $block;} Drupalcamp Bucharest 2011
    • 14. BLOCK• The block appears on every page• We want to limit it to roles via permissions• We want to limit it to nodes Drupalcamp Bucharest 2011
    • 15. PERMISSIO• hook_permission• Returns an array of permissions• user_access($perm) checksfunction dcamp_permission() { return array( forward node => array( title => t(Forward nodes to friends) ), );} Drupalcamp Bucharest 2011
    • 16. PERMISSIOfunction dcamp_block_content() { if (!user_access(forward node)) { return ; } return "Block content.";} Drupalcamp Bucharest 2011
    • 17. LIMITING• Internal Drupal path: node/4• Path alias: page/about-us.html• arg(0) = node, arg(1) = 4if (arg(0) == node && is_numeric(arg(1))) { $nid = arg(1);}else { return ;} Drupalcamp Bucharest 2011
    • 18. FORMS• Forms are generated via a structured array of elements• They have an ID which is the function that returns the structure• drupal_get_form(‘dcamp_form’) returns the form HTML Drupalcamp Bucharest 2011
    • 19. FORMS• Form generation: dcamp_form• Form validation: dcamp_form_validate• Form submit: dcamp_form_submit Drupalcamp Bucharest 2011
    • 20. FORMfunction dcamp_form($form, $form_state, $nid) { $form = array(); $form[nid] = array( #type => hidden, #value => $nid, ); $form[email] = array( #type => textfield, #title => t(Email address), #size => 25, #required => TRUE ); $form[submit] = array( #type => submit, #value => t(Forward) ); return $form;} Drupalcamp Bucharest 2011
    • 21. FORM• $form_state[‘values’]• form_set_errorfunction dcamp_form_validate($form, &$form_state) { $email = $form_state[values][email]; if (!valid_email_address($email)) { form_set_error(email, t(Please enter a valid emailaddress.)); }} Drupalcamp Bucharest 2011
    • 22. FORMfunction dcamp_form_submit($form, &$form_state) { global $user; $email = $form_state[values][email]; $nid = $form_state[values][nid]; // send an email with the url to the email address drupal_set_message(t(The node has been forwarded to%email., array(%email => $email)));} Drupalcamp Bucharest 2011
    • 23. SENDING• Complicated• Requires implementing a hook - hook_mail• Different email types• Of course, you can just use mail() Drupalcamp Bucharest 2011
    • 24. SENDINGfunction dcamp_mail($key, &$message, $params) { if ($key == forward) { $langcode = $message[language]->language; $message[subject] = t(Recommended site, array(),array(langcode => $langcode)); $message[body][] = t("Youve been recommended this !url.", array(!url => url(node/ . $params[nid],array(absolute => TRUE))), array(langcode => $langcode)); }}drupal_mail(dcamp, forward, $email,user_preferred_language($user), array(nid => $nid)); Drupalcamp Bucharest 2011
    • 25. ADMIN• We want an admin page• admin/config/content/forward• Another hook (no surprise there)• hook_menu• We’ll use a form Drupalcamp Bucharest 2011
    • 26. MENU• Every page in a Drupal site is a menu item• Does not mean it has to appear in the site menu Drupalcamp Bucharest 2011
    • 27. MENU• Url: • admin/config/content/forward • wildcards: node/%/forward• Title• Description• Type: Normal, Tab, Callback Drupalcamp Bucharest 2011
    • 28. MENU• Page callback• Page arguments• Access callback• Access arguments Drupalcamp Bucharest 2011
    • 29. SAVING• You could use the database• variable_set(‘variable’, $value);• Works with arrays and objects too• variable_get(‘variable’, $default_value); Drupalcamp Bucharest 2011
    • 30. RESOURCE• Read other people’s code• http://api.drupal.org• Pro Drupal 7 Development• Examples - http:// drupalexamples.info/• Textmate Bundle and alternatives Drupalcamp Bucharest 2011
    • 31. THANK andu@ctrlz.ro http://ctrlz.roDrupalcamp Bucharest 2011