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

Drupal Best Practices

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