Successfully reported this slideshow.
Your SlideShare is downloading. ×

Drupal Best Practices

Upcoming SlideShare
Introduction to jQuery
Introduction to jQuery
Loading in …3

Check these out next

1 of 47 Ad

More Related Content

Viewers also liked (20)


Similar to Drupal Best Practices (20)


Recently uploaded (20)

Drupal Best Practices

  2. 2. We are going to cover... <ul><li>Drupal Best practices for </li></ul><ul><ul><ul><li>Development Environment </li></ul></ul></ul><ul><ul><ul><li>Module Development </li></ul></ul></ul><ul><ul><ul><li>Theme Development </li></ul></ul></ul><ul><ul><ul><li>Other stuff </li></ul></ul></ul>
  3. 3. Installation <ul><li>Always use the latest CVS code </li></ul><ul><ul><ul><li>Not necessarily the HEAD version </li></ul></ul></ul>
  4. 4. Geting Drupal from CVS <ul><li>$ cvs checkout -r DRUPAL-5 -d mysite drupal </li></ul>
  5. 5. Getting contibuted modules <ul><li>$ cd mysite </li></ul><ul><li>$ cvs checkout -d modules/views -r DRUPAL-5 contributions/modules/views </li></ul><ul><li>$ cvs checkout -d modules/cck -r DRUPAL-5 contributions/modules/cck </li></ul>
  6. 6. Now updates are a snap <ul><li>$ cvs update -dP </li></ul><ul><li>This gets any updates to the core and the modules for the installed version </li></ul>
  7. 7. Updating to new Drupal version <ul><ul><li>Be sure that contibuted modules have been ported to the new version </li></ul></ul><ul><ul><li>$ cvs update -r DRUPAL-5-2 -dP </li></ul></ul>
  8. 8. Revision Control you project <ul><li>Subversion users </li></ul><ul><ul><li>$ cd mysite </li></ul></ul><ul><ul><li>$ svn import /path/to/svn/repository </li></ul></ul><ul><li>Bazaar users </li></ul><ul><ul><li>$ cd mysite </li></ul></ul><ul><ul><li>$ bzr init </li></ul></ul>YOU CANNOT USE CVS HERE
  9. 9. Best Practices so far <ul><li>Get the latest version from CVS </li></ul><ul><li>Check for updates before going further </li></ul><ul><li>Get the needed contributed modules from CVS </li></ul><ul><li>Have your own revision control system in place </li></ul><ul><ul><li>You do not own Drupal CVS repository </li></ul></ul><ul><li>Update(CVS) periodically to get latest fixes </li></ul><ul><li>Don't even try using CVS </li></ul>
  10. 10. Module Development <ul><li>UNDERSTANT </li></ul><ul><li>ALL </li></ul><ul><li>PREQUISITES </li></ul><ul><li>VERY </li></ul><ul><li>THOUROUGHLY </li></ul>
  11. 11. Make sure you understand <ul><li>Drupal's database schema </li></ul><ul><li>Just to give you a glimpse </li></ul>
  12. 13. Understand hooks <ul><li>Drupal has hooks for every thing </li></ul><ul><li>CRUD operations </li></ul><ul><li>Modiying into user data at runtime </li></ul><ul><li>Modifying almost everything at runtime </li></ul><ul><li>Path definitions (rails is a bit late here) </li></ul><ul><li>And much much more </li></ul>
  13. 14. And obiviously <ul><li>You should know </li></ul><ul><li>PHP </li></ul><ul><li>SQL </li></ul><ul><li>Good programming style </li></ul>
  14. 15. Ask questions <ul><ul><ul><li>Is the functionality already available in any contributed module? </li></ul></ul></ul><ul><ul><li>Is there a contributed module that does something similar that I need? </li></ul></ul><ul><ul><li>Can I adapt some existing module to do what I need to do? </li></ul></ul>`
  15. 16. Code you own modules when needed <ul><li>DO NOT FORCE </li></ul><ul><li>A DONKEY </li></ul><ul><li>RUN A DERBY </li></ul><ul><li>NEITHER FORCE </li></ul><ul><li>A STALLION </li></ul><ul><li>TO PULL A CART </li></ul>
  16. 17. If it is a Node Module <ul><li>Try to use </li></ul><ul><li>Content </li></ul><ul><li>Construction </li></ul><ul><li>Kit </li></ul><ul><li>if possible... </li></ul>
  17. 18. With CCK <ul><li>Use </li></ul><ul><li>VIEWS </li></ul><ul><li>Views module provides creating custom views of the node related data and other data as well. </li></ul>
  18. 19. While writing module <ul><li>You will be </li></ul><ul><li>Creating Forms </li></ul><ul><li>Recieving input </li></ul><ul><li>Doing SQL stuff </li></ul><ul><li>Oupting Stings </li></ul><ul><ul><li>containing Text, HTML and other content </li></ul></ul>
  19. 20. Use the Form API <ul><li>Form API has benifits </li></ul><ul><li>Define forms in php syntax </li></ul><ul><ul><li>Drupal will generate the HTML for you </li></ul></ul><ul><ul><ul><li>Dupal takes care proper html </li></ul></ul></ul><ul><ul><ul><li>Makes form alterable at the runtime by other modules </li></ul></ul></ul><ul><ul><ul><li>Makes programatic submission possible </li></ul></ul></ul>
  20. 21. Convention over Configuration <ul><li>Use Form API conventions </li></ul><ul><li><?php </li></ul><ul><li>function module_form(){ </li></ul><ul><li>//form definitions </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  21. 22. Validate <ul><li><?php </li></ul><ul><li>function module_form_validate ($form_id, $form_values) { </li></ul><ul><ul><li>// do the validation </li></ul></ul><ul><ul><li>// set the errors </li></ul></ul><ul><li>} </li></ul><ul><li>function module_form_submit ($form_id, $form_values) { </li></ul><ul><ul><li>// perform submit </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  22. 23. Input validation <ul><li>Make sure you check all text only fields with </li></ul><ul><li>check_plain() </li></ul><ul><li>It checks for malicious contents in like clever scripts, specially HTML etc. </li></ul>
  23. 24. Othe use full functions <ul><li>filter_xss() </li></ul><ul><li>check_markup() </li></ul><ul><li>check_url </li></ul><ul><li>valid_url() </li></ul><ul><li>valid_email() </li></ul>
  24. 25. <ul><ul><li>Drupal has a very powerfull input filter syetem </li></ul></ul><ul><li>LEARN IT </li></ul><ul><li>USE IT </li></ul><ul><li>No Excuses </li></ul>
  25. 26. SQL <ul><li>CONSTRUCT </li></ul><ul><li>GOOD </li></ul><ul><li>QUERIES </li></ul>
  26. 27. BAD SQL... <ul><li>$result = db_query(”SELECT * </li></ul><ul><li>FROM {node} </li></ul><ul><li>WHERE </li></ul><ul><li>uid = $uid”); </li></ul>
  27. 28. Good SQL <ul><li>$result = db_query(”SELECT * </li></ul><ul><li>FROM {node} </li></ul><ul><li>WHERE </li></ul><ul><li>uid = %d”, $uid); </li></ul>
  28. 29. printf style convervsion specs <ul><li>%d = integer </li></ul><ul><li>$f = float </li></ul><ul><li>'%s' = sting </li></ul><ul><li>%b = binary data </li></ul><ul><li>%% = percent sign </li></ul>
  29. 30. Avoid queries in loop <ul><li><?php </li></ul><ul><li>while (some condition) { </li></ul><ul><li>$res = db_query(”SELECT * FROM </li></ul><ul><li>{table} WHERE </li></ul><ul><li>something = something”); </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  30. 31. Bewre of QUERY HELL <ul><li>In some-page.tpl.php </li></ul><ul><li>$someone = user_load(array('uid' => $uid)); </li></ul><ul><li>call_some_function($uid = $user->uid); </li></ul><ul><ul><li>function call_some_function($uid){ </li></ul></ul><ul><ul><li>$someone = user_load(array('uid'=>$uid)); </li></ul></ul><ul><ul><li>} </li></ul></ul>Reloading user again
  31. 32. Modules and HTML <ul><li>MODULE != HTML </li></ul>Never output html directly from module functions BUT DON'T MAKE IT A RELIGION
  32. 33. Example of Module + HTML <ul><li>blog_user_page() </li></ul>Generates HTML directly Learn from it
  33. 34. One tip <ul><li>Never </li></ul><ul><li>Never </li></ul><ul><li>Never </li></ul>use print statement from inside functions
  34. 35. Themes <ul><li>You MUST be strong in </li></ul><ul><li>HTML </li></ul><ul><li>CSS </li></ul><ul><li>JavaScript </li></ul><ul><li>jQuery </li></ul>
  35. 36. Theme engines <ul><li>Available theme engines </li></ul><ul><li>PHPTemplate </li></ul><ul><li>Zengine psedo theme theme engine pased on PHPTemplate </li></ul><ul><li>Smarty </li></ul><ul><li>PHPTAL </li></ul><ul><li>Xtemplate DEPRECATED </li></ul><ul><li>Pure PHP themes </li></ul>
  36. 37. PHPTemplate <ul><li>The default theme engine since v4.7 </li></ul><ul><li>Simple to learn and use </li></ul><ul><li>Built for Drupal </li></ul><ul><li>Uses PHP for templating </li></ul><ul><li>No new language to learn </li></ul><ul><ul><li>DIE SMARTY DIE </li></ul></ul><ul><li>Powerfull, but sometimes dangerous </li></ul>
  37. 38. TPL Magic <ul><li>page.tpl.php </li></ul><ul><li>node.tpl.php </li></ul><ul><li>comment.tpl.php </li></ul><ul><li>block.tpl.php </li></ul><ul><li>box.tpl.php </li></ul><ul><li>etc etc </li></ul>
  38. 39. Learn the variables available to tpl page.tpl.php node.tpl.php $head $styles $scripts $head_title $is_front $footer ... ... $node $links $posted $$page ... ... ...
  39. 40. Override anything themeable theme_pager() phptemplate_pager()
  40. 41. Rule of Thumb is <ul><li>theme('pager'); </li></ul><ul><ul><ul><li>theme-name_pager(); </li></ul></ul></ul><ul><ul><ul><li>theme-engine_pager(); </li></ul></ul></ul><ul><ul><ul><li>theme_pager(); </li></ul></ul></ul>
  41. 42. Blocks/Regions are good <ul><li>Blocks are places where chunks of UI GO </li></ul><ul><li>Called ” regions” in developer language </li></ul><ul><li>Called ” blocks” user language </li></ul>
  42. 44. defining regions <ul><li>function mytheme_regions() { </li></ul><ul><li>return array( </li></ul><ul><li>'left' => t('left sidebar'), </li></ul><ul><li>'right' => t('right sidebar'), </li></ul><ul><li>'content_top' => t('content top'), </li></ul><ul><li>'content_bottom' => t('content bottom'), </li></ul><ul><li>'header' => t('header'), </li></ul><ul><li>'footer' => t('footer') </li></ul><ul><li>); </li></ul><ul><li>} </li></ul>
  43. 45. What we did not cover <ul><li>Upgrade and Maintenance </li></ul><ul><li>Localization and Internationalization </li></ul><ul><li>Deployment practices </li></ul>
  44. 46. But you know where to go <ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li>and off course </li></ul><ul><li> </li></ul>
  45. 47. Thank You for your time <ul><li>? </li></ul>Mir Nazim Xensoft Labs [email_address]