advanced moduledevelopment d6_slideshare

14
Advanced Module Development (D6) Case study: UC_Etranzact Ubercart contrib module Kayode Odeyemi Technical Architect, Opevel DrupalCamp Lagos 2011

Upload: opevel

Post on 01-Nov-2014

1.388 views

Category:

Technology


0 download

DESCRIPTION

Advanced module development featuring Ubercart uc_etranzact contrib module on Drupal 6. DrupalCamp Lagos 2011.

TRANSCRIPT

Page 1: Advanced moduledevelopment d6_slideshare

Advanced Module Development (D6)

Case study: UC_Etranzact Ubercart

contrib module

Kayode OdeyemiTechnical Architect, Opevel

DrupalCamp Lagos 2011

Page 2: Advanced moduledevelopment d6_slideshare

Where to start from?

There are various ways of starting a Drupal

module;

• Using Features module: Makes your Drupalmodule exportable and deployment easier.

• Using Module Builder module: auto-generates a skeleton or "scaffolding" for a module

Page 3: Advanced moduledevelopment d6_slideshare

Drupal Common Hooks

• Hook_menu• Hook_init• Hook_help• Hook_nodeapi• Hook_theme• Hook_install• Hook_uninstall• Hook_form• Hook_form_alter• Hook_form_submit• Hook_form_validate

Page 4: Advanced moduledevelopment d6_slideshare

Very useful Drupal APIs for everyday development

• Module_invoke• Module_load_include• Drupal_write_record• Function_exists• Call_user_func_array• Db_table_exists• Db_query• Db_result• Db_object_fetch_array and db_object_fetch_object• Drupal_set_message or dsm if using devel module• Db_create_table• Update_sql• Views_get_views_result• Db_add_field• Db_drop_field• Drupal_get_schema• Element_children• Views_get_default_view

Page 5: Advanced moduledevelopment d6_slideshare

Writing Drupal Hooks

There are different ways of writing Drupal hooks.

// Using module_implements inline within a non-hook function and using module_invoke

foreach(module_implements(‘hook_name') as $module) {

module_invoke($module, ‘hook_name', $args);

}

// Explicitly writing the hook as a function with module_implements alonefunction mymodule_hook($fees) {

foreach (module_implements(‘hook_name') as $module) {

$function = $module . '_hook_name';

$result = $function($fees);

if (isset($result) && is_array($result)) {

$return = array_merge($return, $result);

}

else if (isset($result)) {

$return[] = $result;

}

}

return $return;

}

Page 6: Advanced moduledevelopment d6_slideshare

drupal_http_request: Drupal's simple curl equivalent

// curl

$curl = curl_init(); $apiurl = variable_get('uc_etranzact_demo_mode', 1) ? UC_ETRANZACT_SERVICE_DEMO : UC_ETRANZACT_SERVICE; $apiurl = sprintf('%s?%s', $apiurl, $query); curl_setopt($curl, CURLOPT_URL, $apiurl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); $return = curl_exec($curl);

// drupal_http_request

$result = drupal_http_request($uri, $header, $method, http_build_query($data, '', '&'));

Page 7: Advanced moduledevelopment d6_slideshare

Test with SimpleTest

• When your project is starting to get robust and clumsy, test with SimpleTest

• Common SimpleTest APIs are:

– drupalGetNodeByTitle($title)

– drupalCreateNode($settings)

– drupalCreateContentType($settings) -drupalCreateUser($permissions)

– checkPermissions(array $permissions, $reset = FALSE): Check to make sure that the array of permissions are valid.

– drupalLogin(stdClass $user): Log in a user with the internal browser.

Page 8: Advanced moduledevelopment d6_slideshare

Test with SimpleTest

– drupalLogout

– assertNotEqual: Check to see if two values are not equal.

– assertEqual: Check to see if two values are equal.

– drupalPost: Execute a POST request on a Drupal page.

– drupalGet: Retrieves a Drupal path or an absolute path.

– assertTrue: Check to see if a value is not false (not an empty

string, 0, NULL, or FALSE)

– assertFalse: Check to see if a value is false (an empty string, 0,

NULL, or FALSE).

– assertNull: Check to see if a value is not NULL.

Page 9: Advanced moduledevelopment d6_slideshare

Administer Drupal faster with Drupal

• Drush is Drupal’s CLI language

• Administer modules quickly with Drush

• When working with multi-sites, Drush is your best friend

• Drupal cheat sheet

– Drush status

– Drush dl <modulename>

– Drush cc

– Drush eval “<php code>”

– Drush en <module name>

– Drush dis <module name>

– Drush up

– Drush upc <module name>

– … more on drupal.org

Page 10: Advanced moduledevelopment d6_slideshare

Debugging Drupal

• The devel module and theme developer modules are very handy tools for Debugging Drupal.

• There’s also a firebug Drupal extension for debugging Drupal

• Useful functions:

– Dsm

– Dpr

– dvm

Page 11: Advanced moduledevelopment d6_slideshare

Ubercart Payment Hooks and useful APIs

• Hook_payment_method: define a custom payment method

• Hook_order: define and process the order items

• Hook_checkout_pane: define a custom pane for your order and

style it

• uc_order_status_data: Check the status of an order

• uc_cart_empty(uc_cart_get_id()): Empty a cart

• uc_order_save($order): save an order to the database

• Hook_tapir_table_alter

• Hook_cart_item: alter the items in cart

• Hook_cart_pane: Override the cart pane

• Hook_product_description: modify the descriptions of items in cart

• Uc_cart_get_contents: get cart contents

Page 12: Advanced moduledevelopment d6_slideshare

Understanding ubercart cart process

• Add items to cart using uc_cart_add_items.

• Checkout your order and implement hook_checkout_pane to customize the order items.

• Review the order in different states namely:– New: only called if the order is new– Submit: Process the order such as integrating with a

payment gateway like etranzact.– Save: Save your order in a database– Load: called immediately an order is successfully

saved in the db

Page 13: Advanced moduledevelopment d6_slideshare

How we build it: uc_etranzact module

• Uc_etranzact module only requires a payment gateway implementation

• Ubercart Hooks that got the job done:

– Hook_payment_gateway

– Hook_order

– Callback functions to process the order to integrate with etranzact system

Page 14: Advanced moduledevelopment d6_slideshare

Questions

• Website - http://opevel.com

• Twitter : @opevel @drupal @acquia

• Opevel Services

– Drupal custom development

– Drupal site architecture

– Custom Application Development on Google App Engine

http://drupal.org/sandbox/charyorde/1310706

• Datasphir is hiring Drupal developers. – http://datasphir.com