ts drupal6 module development v0.2

31
Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

Upload: confiz

Post on 06-May-2015

429 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ts   drupal6 module development v0.2

Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

Page 2: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide Furqan Razzaq| Drupal Mentor

Page 3: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide Topics covered in the presentation

• What is Module

• Collaboration Over Competition

• Telling Drupal About Your

Module

• Hooks

• Block Content

• Other Files

Furqan Razzaq | Drupal Mentor

Page 4: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

What is a Module

Furqan Razzaq | Drupal Mentor

A module simply is a collection of procedures that are

logically combined in a group of files.

These procedures can he hooks, menu callbacks,

forms, themes or your custom, or even

jquery/javascript snippets.

Page 5: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Kinds of Module

Furqan Razzaq | Drupal Mentor

There are three kinds of Drupal Modules

1. Core

2. Contributes

3. Custom

Page 6: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Core Modules

Furqan Razzaq | Drupal Mentor

• Core modules are the ones that are shipped with Drupal install and

are approved by the core developers and the community.

• The location of these modules is under [installation directory/modules]

• There are also a bunch of include files that these modules use.

Include files are located under [installation directory/includes]

Page 7: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Custom Module Development

Furqan Razzaq | Drupal Mentor

)

• Before we start, following are some helpful links to guide you through

the development process:

• http://drupal.org/node/326 [working with Drupal API]

• http://api.drupal.org/api/drupal [Drupal API reference]

• http://drupal.org/node/7765 [Best Practices on creating and maintaining

projects]

• http://drupal.org/coding-standards [coding standards]

• http://drupal.org/writing-secure-code [writing secure code]

We know its a lot to process in one go, but you will get to it eventually.

Page 8: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Collaboration Over Competition

Furqan Razzaq | Drupal Mentor

• Module Duplication is a growing concern with in Drupal community,

which values joining forces on improving one awesome project rather

than building several sub-standard ones that overwhelm end users

with choices.

• So what to do? Search existing modules before you start embarking

on your own quest.

Page 9: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Let’s Jump Over Module Development

Furqan Razzaq | Drupal Mentor

• So, what do you need:

• Basic PHP knowledge (of course ) including syntax and concept of

PHP Objects

• Basic understanding of database tables, fields, records and SQL

statements

• A working Drupal installation

• Webserver access (in our case, its any set of Apache/PHP/MySql)

Page 10: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Let’s Develop a Single Module

Furqan Razzaq | Drupal Mentor

• Module Name

• Telling Drupal about your module

• Declaring block content

Page 11: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Getting Started

Furqan Razzaq | Drupal Mentor

• Create Following files

• .info

• .module

Page 12: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Telling Drupal About Your Module

Furqan Razzaq | Drupal Mentor

1. How to let Drupal know the module exists?

2. Drupal hook described: hook_help

Page 13: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

How to Let Drupal Know That Module Exists

Furqan Razzaq | Drupal Mentor

• Tell Drupal about your module in modulename.info file. File content

should be like…

• Name (Required) = Color

• Description (Required) = Allows the user to change the color scheme

of certain themes.

• package = Core - optional

• Core (Required) = 6.x

• version = "6.20"

• project = "drupal"

• datestamp = "1292447788"

Page 14: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Telling Drupal About Your Module

Furqan Razzaq | Drupal Mentor

1. How to let Drupal know the module exists?

2. Drupal hook described: hook_help

Page 15: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Drupal Hooks

Furqan Razzaq | Drupal Mentor

Drupal Hook???

Page 16: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Apparently Not

Furqan Razzaq | Drupal Mentor

• Drupal's module system is based on the concept of "hooks".

• A hook is a PHP function.

• Hooks allow modules to interact with the Drupal core.

• Each hook has a defined set of parameters and a specified result

type.

• A module need simply implement a hook.

Page 17: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

How to Declare Hooks?

Furqan Razzaq | Drupal Mentor

• modulename_hookname()

• color_help()

Page 18: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Where/When Hooks are Used?

Furqan Razzaq | Drupal Mentor

• Drupal determines which modules implement a hook and calls that

hook in all enabled modules that implement it.

Page 19: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Common Hooks

Furqan Razzaq | Drupal Mentor

• hook_help()

• hook_perm()

• hook_init()

• hook_theme()

• hook_block()

• hook_menu()

Page 20: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Help Hooks – a Module File Entry

Furqan Razzaq | Drupal Mentor

/**

* Implementation of hook_help

*/

function modulename_help($path, $arg) {

switch ($path) {

case 'admin/help#color':

$output = '<p>'. t('The color module allows a site administrator to

quickly and easily change the color scheme of certain

themes.’ ).'</p>';

return $output;

}

}

Page 21: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Specify the Available Permissions

Furqan Razzaq | Drupal Mentor

• Tell Drupal who can use your module.

/**

* Implementation of hook_perm

*/

function modulename_perm() {

return array('access site-wide ', 'administer colors');

}

Page 22: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Hook_init ()

Furqan Razzaq | Drupal Mentor

• This hook is run at the beginning of the page request.

1. Add CSS or JS that should be present on every page.

2. Set up global parameters which are needed later in the request

Page 23: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Cont..Hook_init ()

Furqan Razzaq | Drupal Mentor

function modulename_init() {

$path = drupal_get_path('module', ‘modulename');

drupal_add_js($path . '/filename.js');

drupal_add_css($path . ‘/filename.css', 'module', 'all', FALSE);

}

http://api.drupal.org/api/drupal/developer!hooks!core.php/function/hook_i

nit/6

Page 24: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Hook_Theme ()

Furqan Razzaq | Drupal Mentor

function modulename_theme($existing, $type, $theme, $path) {

}

• Write theme funtions

Page 25: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Declaring Block Content

Furqan Razzaq | Drupal Mentor

/** * Implementation of hook_block(). * @param string $op one of "list", "view", "save" and "configure" * @param integer $delta code to identify the block * @param array $edit only for "save" operation */

function modulename_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) {

case 'list':

$block = array();

$block[0]["info"] = t('assets');

return $block;

break;

Page 26: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Declaring Block Content Cont..

Furqan Razzaq | Drupal Mentor

case 'view':

$block['subject'] = 'assets';

$block['content'] = get_block_content();

return $block;

break;

}

}

http://api.drupal.org/api/drupal/developer!hooks!core.php/function/hook_

block/6

Page 27: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Hook Menu

Furqan Razzaq | Drupal Mentor

• Define menu items and page callbacks.

• This hook enables modules to register paths in order to define how URL

requests are handled.

• This hook is rarely called (for example, when modules are enabled), and

its results are cached in the database.

Page 28: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Hook Menu Cont..

Furqan Razzaq | Drupal Mentor

function modulename_menu() {

$items['abc/def'] = array(

'page callback' => 'mymodule_abc_view',

'type' => MENU_CALLBACK,

'access callback' => true,

);

return $items;

}

http://api.drupal.org/api/drupal/developer!hooks!core.php/function/hook_

menu/6

Page 29: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

Useful Links

Furqan Razzaq | Drupal Mentor

• hook_form_alter(&$form, &$form_state, $form_id)

http://api.drupal.org/api/drupal/developer!hooks!core.php/function/hook_form_alter/6

• hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) http://api.drupal.org/api/drupal/developer!hooks!core.php/function/hook_nodeapi/6

• hook_user($op, &$edit, &$account, $category = NULL) http://api.drupal.org/api/drupal/developer!hooks!core.php/function/hook_user/6

Page 30: Ts   drupal6 module development v0.2

Drupal6 Module Development Guide

.Uninstall File

Furqan Razzaq | Drupal Mentor

• Remove all tables that a module defines.

• http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_

uninstall_schema/6