LEARNING THE BASICS OF THE DRUPAL API      Badiu Alexandru     Drupalcamp Bucharest 2011
THIS•   We’re going to use Drupal 7•   For beginners, intended as a    continuation of “From zero to hero”•   Going to ass...
YOU’LL•   The basics of module development•   How to create blocks•   How to check for permissions•   How to create forms•...
MODULES•   Modules are the building blocks•   name.info•   name.module•   sites/all/modules            Drupalcamp Buchares...
MODULES•   Create blocks•   Create content types•   Create pages and forms•   Augment Drupal core•   Augment Drupal module...
DCAMP.M•   DrupalCamp Forward•   Allows a user to forward a node url    to a friend•   Displays a block on each node page•...
DCAMP.M•   dcamp.info      name = DrupalCamp Forward      description = Drupalcamp D7 Demo      package = Drupalcamp      ...
DCAMP.M•   dcamp.module•   Is a php file•   Contains the module code•   Implements hooks             Drupalcamp Bucharest 2...
HOOKS•   Hooks are callbacks•   Everytime an action is performed in    Drupal a specific hook is called•   You can alter th...
HOOKS•   hook_name•   function dcamp_name($arg1, $arg2)    {...}            Drupalcamp Bucharest 2011
HOOKS•   http://api.drupal.org/api/drupal/    includes--module.inc/group/hooks/    7•   Lots of them•   Block, Node, Forms...
BLOCK•   hook_block_info•   hook_block_view•   function dcamp_block_info()•   function dcamp_block_view($delta =    ‘’)   ...
BLOCKfunction dcamp_block_info() {  $blocks = array(     forward_block => array(       info => t(Forward node block),     ...
BLOCK•   The block appears on every page•   We want to limit it to roles via    permissions•   We want to limit it to node...
PERMISSIO•   hook_permission•   Returns an array of permissions•   user_access($perm) checksfunction dcamp_permission() { ...
PERMISSIOfunction dcamp_block_content() {  if (!user_access(forward node)) {    return ;  }    return "Block content.";}  ...
LIMITING•   Internal Drupal path: node/4•   Path alias: page/about-us.html•   arg(0) = node, arg(1) = 4if (arg(0) == node ...
FORMS•   Forms are generated via a structured    array of elements•   They have an ID which is the    function that return...
FORMS•   Form generation: dcamp_form•   Form validation:    dcamp_form_validate•   Form submit: dcamp_form_submit         ...
FORMfunction dcamp_form($form, $form_state, $nid) {  $form = array();    $form[nid] = array(      #type => hidden,      #v...
FORM•   $form_state[‘values’]•   form_set_errorfunction dcamp_form_validate($form, &$form_state) {  $email = $form_state[v...
FORMfunction dcamp_form_submit($form, &$form_state) {  global $user; $email = $form_state[values][email]; $nid = $form_sta...
SENDING•   Complicated•   Requires implementing a hook -    hook_mail•   Different email types•   Of course, you can just ...
SENDINGfunction dcamp_mail($key, &$message, $params) {  if ($key == forward) {    $langcode = $message[language]->language...
ADMIN•   We want an admin page•   admin/config/content/forward•   Another hook (no surprise there)•   hook_menu•   We’ll us...
MENU•   Every page in a Drupal site is a menu    item•   Does not mean it has to appear in    the site menu            Dru...
MENU•   Url:    •   admin/config/content/forward    •   wildcards: node/%/forward•   Title•   Description•   Type: Normal, ...
MENU•   Page callback•   Page arguments•   Access callback•   Access arguments            Drupalcamp Bucharest 2011
SAVING•   You could use the database•   variable_set(‘variable’, $value);•   Works with arrays and objects too•   variable...
RESOURCE•   Read other people’s code•   http://api.drupal.org•   Pro Drupal 7 Development•   Examples - http://    drupale...
THANK   andu@ctrlz.ro   http://ctrlz.roDrupalcamp Bucharest 2011
Upcoming SlideShare
Loading in …5
×

Learning the basics of the Drupal API

4,305 views

Published on

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
4,305
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
48
Comments
0
Likes
4
Embeds 0
No embeds

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
  • Learning the basics of the Drupal API

    1. 1. LEARNING THE BASICS OF THE DRUPAL API Badiu Alexandru Drupalcamp Bucharest 2011
    2. 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. 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. 4. MODULES• Modules are the building blocks• name.info• name.module• sites/all/modules Drupalcamp Bucharest 2011
    5. 5. MODULES• Create blocks• Create content types• Create pages and forms• Augment Drupal core• Augment Drupal modules Drupalcamp Bucharest 2011
    6. 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. 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. 8. DCAMP.M• dcamp.module• Is a php file• Contains the module code• Implements hooks Drupalcamp Bucharest 2011
    9. 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. 10. HOOKS• hook_name• function dcamp_name($arg1, $arg2) {...} Drupalcamp Bucharest 2011
    11. 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. 12. BLOCK• hook_block_info• hook_block_view• function dcamp_block_info()• function dcamp_block_view($delta = ‘’) Drupalcamp Bucharest 2011
    13. 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. 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. 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. 16. PERMISSIOfunction dcamp_block_content() { if (!user_access(forward node)) { return ; } return "Block content.";} Drupalcamp Bucharest 2011
    17. 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. 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. 19. FORMS• Form generation: dcamp_form• Form validation: dcamp_form_validate• Form submit: dcamp_form_submit Drupalcamp Bucharest 2011
    20. 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. 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. 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. 23. SENDING• Complicated• Requires implementing a hook - hook_mail• Different email types• Of course, you can just use mail() Drupalcamp Bucharest 2011
    24. 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. 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. 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. 27. MENU• Url: • admin/config/content/forward • wildcards: node/%/forward• Title• Description• Type: Normal, Tab, Callback Drupalcamp Bucharest 2011
    28. 28. MENU• Page callback• Page arguments• Access callback• Access arguments Drupalcamp Bucharest 2011
    29. 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. 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. 31. THANK andu@ctrlz.ro http://ctrlz.roDrupalcamp Bucharest 2011

    ×