53
Module Development Ipsita Mishra Siva Kumar Epari http://melity.com

Drupal Module Development

Embed Size (px)

DESCRIPTION

This workshop was taken by Ipsita and Siva at OSI Days 2010 conference on 21st September.

Citation preview

Page 1: Drupal Module Development

Module Development

Ipsita MishraSiva Kumar Epari

http://melity.com

Page 2: Drupal Module Development

About Us

●Ipsita Mishra✔4 years of Drupal experience✔Tech lead✔Webadmin & Core member of Drupal Hyderabad

●Siva Kumar Epari✔2 years of Drupal experience✔Drupal Developer✔Webadmin & Core member of Drupal Hyderabad

Page 3: Drupal Module Development

Types of Modules

Core Modules

Contrib Modules

Custom Module

Page 4: Drupal Module Development

Core Modules

These are the modules that ships with a Standard Drupal Release. There are Core Optional and Core Required modules.

For example:NodeUserBlockSystemFilter

Page 5: Drupal Module Development

Contrib Modules

Over 5800 (as of July 20, 2010) free community-contributed modules, known as contrib modules, are available to alter and extend Drupal's core capabilities and add new features or customize Drupal's behavior and appearance.

Page 6: Drupal Module Development

Popular Contrib Modules

These statistics are incomplete; only Drupal websites using the Update Status module are included in the data.

Page 7: Drupal Module Development

Custom Module

A Custom module is one which is local to your Drupal Project. It's not yet contributed to the contrib repository.

Page 8: Drupal Module Development

Custom Module(When to write)

You need to answer few questions before writing a Custom Module1. What do you want to achieve by this custom module?2. Did you search the Contrib Repository to see if a module is already available for that feature?3. Have you enabled all possible Configurations of the Core & Contrib module to check if they offer you the feature you require?

If your answer is, “Yes, I have done enough research and am sure I have to write the code now”, then go ahead ....

Page 9: Drupal Module Development

Custom Module(Why to write)

I have 2 reasons to write a Custom module

1. I need a new feature which is not yet available in Drupal, and probably can be contributed as a Contrib Module.

2. I don't want to keep adding additional modules to my site for small tweaks, which I can manage in only one custom module for my website. Don't be too much dependent on Contribs.

Page 10: Drupal Module Development

A simple module directory structure

modulename

modulename.info

modulename.module

Page 11: Drupal Module Development

new.info

; $Id$

name = Module namedescription = “Module description”core = Drupal version (e.g.: 5.x, 6.x)package = Package name

; $Id$

name = Text Captchadescription = “Text Captcha”core = 6.xpackage = Osidays

modulename.info

new.info

Page 12: Drupal Module Development

new.module<?php

// $Id$

function new_form_alter(&$form, &$form_state, $formid){ if(substr($formid, -9) == 'node_form'){ $form['captcha'] = array( '#type' => 'textfield', '#title' => 'Captcha Question : What is 5 + 3?', ); $form['#validate'][] = '_new_validate'; $form['#submit'][] = '_new_submit'; } }

function _new_validate($form, &$form_state){ if($form_state['values']['captcha'] != '8'){

form_set_error('captcha', 'Your Captcha answer is wrong!So you are not a human or you don\'t know counting :D'); }}

function _new_submit($form, &$form_state){ drupal_set_message('You are a human!');}

new.module

Page 13: Drupal Module Development

Download examples module fromhttp://drupal.org/project/examples

Page 14: Drupal Module Development

http://drupal.org/project/examples

Page 15: Drupal Module Development

node_example module

Page 16: Drupal Module Development

hook_node_info()

Page 17: Drupal Module Development

hook_access()

Page 18: Drupal Module Development

hook_perm()

Page 19: Drupal Module Development

http://localhost/drupal-6.19/admin/user/permissions

hook_perm() Output

Page 20: Drupal Module Development

hook_form()

Page 21: Drupal Module Development

hook_form() Contd...

Page 22: Drupal Module Development

hook_validate()

Page 23: Drupal Module Development

hook_insert()

Page 24: Drupal Module Development

hook_schema()

Page 25: Drupal Module Development

hook_install() hook_uninstall()

Page 26: Drupal Module Development

Preventing SQL injection is easy; db_query provides a way to use parametrized queries. Drupal's database functions replace the sprintf-like placeholders with the properly escaped arguments in order of appearance:

%d - integers

%f - floats

%s - strings, enclose in single quotes

%b - binary data, do not enclose in single quotes

hook_update()

Page 27: Drupal Module Development

hook_nodeapi()

Page 28: Drupal Module Development

hook_delete()

Page 29: Drupal Module Development

hook_load()

Page 30: Drupal Module Development

hook_view()

Page 31: Drupal Module Development

hook_theme()

Page 32: Drupal Module Development

Theme function

Page 33: Drupal Module Development

Menu System

Page 34: Drupal Module Development

hook_menu()

Page 35: Drupal Module Development

Menu Item Types

"type": A bitmask of flags describing properties of the menu item. Many shortcut bitmasks are provided as constants in menu.inc:

* MENU_NORMAL_ITEM: Normal menu items show up in the menu tree and can be moved/hidden by the administrator. * MENU_CALLBACK: Callbacks simply register a path so that the correct function is fired when the URL is accessed. * MENU_SUGGESTED_ITEM: Modules may "suggest" menu items that the administrator may enable. * MENU_LOCAL_TASK: Local tasks are rendered as tabs by default. * MENU_DEFAULT_LOCAL_TASK: Every set of local tasks should provide one "default" task, that links to the same path as its parent when clicked.

If the "type" key is omitted, MENU_NORMAL_ITEM is assumed.

Page 36: Drupal Module Development

hook_menu_alter()

Page 37: Drupal Module Development

Drupal Blocks

Page 38: Drupal Module Development

hook_block()

A Block represents some auxiliary content along with the primary content of the page.

Page 39: Drupal Module Development

The output is themedcomment.module

Page 40: Drupal Module Development

Parameters

hook_block($op = 'list', $delta = 0, $edit = array())

Page 41: Drupal Module Development

Operation: list

Page 42: Drupal Module Development

Operation: configure

Page 43: Drupal Module Development

Operation: save

Page 44: Drupal Module Development

Operation: view

Page 45: Drupal Module Development

Developer's Tools

● Drush● Devel and Devel Themer● Coder● Admin Menu

Page 46: Drupal Module Development

DRUpal SHell

● drush dl modulename/themename● drush en modulename/themename● drush dis modulename/themename● drush pm-uninstall modulename/themename● drush cc

Resource:http://drupal.org/files/drush-cheat-sheet.pdf

Page 47: Drupal Module Development

Devel

● Devel: Helper functions for Drupal developers.● Generate content: Accelerate development of your site or module by quickly generating nodes, comments, terms, users, and more.● Node Access Summary: View the node access entries for the node(s) that are shown on a page.

Page 48: Drupal Module Development

Devel Themer

Page 49: Drupal Module Development

Coder

This module helps in Code Review

Page 50: Drupal Module Development

A Sample Analysis by Coder

Page 51: Drupal Module Development

Resources

IRC Channel#drupal#drupal-support#drupal-hyderabad

http://api.drupal.org

Page 52: Drupal Module Development
Page 53: Drupal Module Development

Thank You

@IpsitaMishra@siva_epari