Search This Blog

Monday, April 12, 2010

Drupal Module Development

What does a module do?
‣ Core modules provides functionality
‣ Contributed modules extends functionality

Examples of modules
Core required modules:
‣ System, Filter, Node, Block, User
‣ Core optional modules:
‣ Taxonomy, Menu, Comment, Search
3rd party modules:
‣ Views, Panels, CCK, Devel

Hooks
‣ Can be thought of as internal Drupal events
‣ Allow modules to interact with the Drupal core
‣ Events include:
‣ Creation, deletion of nodes and users
‣ Logging in, logging out
‣ Hooks can also create and alter menus and
forms

Hook examples
‣ hook_user
‣ login, logout, insert, view, update
‣ hook_block
‣ view, save, configure, list
‣ hook_form_alter
‣ hook_insert
‣ hook_comment
‣ insert, update, view, publish, unpublish


hook_user - allows the module to react when operations are performed
hook_block - declares a block or set of blocks
hook_form_alter - performs alterations to a form or forms before it’s rendered

Useful modules
‣ Devel (drupal.org/project/devel)
‣ Admin Menu (drupal.org/project/admin_menu)
‣ Drush (drupal.org/project/drush)
‣ Any others?


Module builder - still in beta. For the lazy.
Drush - command line utility. Drupal Shell. Installing modules, clearing cache, run SQL commands on the database

Our first module
‣ ‘Hello World’ block module
‣ Two files required for a module:
‣ helloworld.info
‣ helloworld.module
‣ (Optionally) helloworld.install


What is a block? Blocks are snippets of text or functionality that usually live outside the main content area of a
web site.


Where should I put my module?
‣ Everyone elses:
‣ /sites/drupalsouth.local/modules/contrib
‣ Yours (in development):
‣ /sites/drupalsouth.local/modules/dev
‣ Yours (after development):
‣ /sites/drupalsouth.local/modules/boost


helloworld.info
; $Id$
name = Hello World
description = Creates a Hello World block
package = Dev
core = 6.x
9

helloworld.module
function helloworld_block($op = ‘list’, $delta = 0, $edit = array())
{
switch($op)
{
case ‘list’:
$block[0][‘info’] = t(‘Hello World’);
return $block;
break;
}
}


Only first Edit contains submitted form data from block config form.



case ‘view’:
$block[‘subject’] = t(‘Hello World!’);
$content = ‘Hello world!’;
$block[‘content’] = $content;
return $block;
break;


case ‘configure’:
$form[‘helloworld_count’] = array(
‘#type’ => ‘textfield’,
‘#title’=> t(‘Number of Hello Worlds to display’),
‘#default_value’ => variable_get(‘helloworld_count’, 1)
);
return $form;
break;

case 'save':
variable_set('helloworld_count', $edit['helloworld_count']);
break;


case ‘view’:
$block[‘subject’] = t(‘Hello World!’);
$count = variable_get(‘helloworld_count’, 1);
for ($i = 0; $i < $count; $i++)
{
$content .= '
  • Hello world!
  • ';
    }
    $block['content'] = $content;
    return $block;
    break;


    Theming a module
    ‣ Register a theme
    ‣ Implement a theme function
    ‣ Applying the theme
    ‣ Utilise external theme templates


    Registering a theme
    function helloworld_theme()
    {
    return array(
    ‘helloworld_show’ => array(
    ‘arguments’ => array(‘content’ => NULL),
    ),
    );
    }

    Implementing theme
    function theme_helloworld_show($content)
    {
    $output = ‘
      $content
    ’;
    return $output;
    }

    Applying the theme
    case 'view':
    $block['subject'] = t('Hello World!');
    $count = variable_get('helloworld_count', 1);
    for ($i = 0; $i < $count; $i++)
    {
    $text .= '
  • Hello world!
  • ';
    }
    $content = theme('helloworld_show', $text);
    $block['content'] = $content;
    return $block;
    break;

    External theme file
    function helloworld_theme()
    {
    return array(
    'helloworld_show' => array(
    'arguments' => array('content' => NULL),
    'template' => 'helloworld_show'
    ),
    );
    }

    helloworld_show.tpl.php


    This is being called from the external file.






    Using deltas
    function helloworld_block($op = ‘list’, $delta = 0, $edit = array())
    {
    switch($op)
    {
    case ‘list’:
    $blocks[0][‘info’] = t(‘Hello World’);
    $blocks[1][‘info’] = t(‘Goodbye Cruel World’);
    return $blocks;
    break;
    }
    }

    Delta - unique block id (per implementation of hook_block). Can be an integer or string.


    Using deltas
    case 'view':
    switch ($delta) {
    case 0:
    $block['subject'] = t('Hello World!');
    $content = 'Hi! :-)';
    break;
    case 1:
    $block['subject'] = t('Goodbye Cruel World!');
    $content = 'Bye... :-(';
    break;
    }
    $block['content'] = $content;

    2 comments:

    Macrosoft said...

    Thanks for sharing drupal development module. This is mostly helpful for me.

    Drupal Development Company

    Unknown said...

    Drupal is one of the best helpful application this completely fledged #application is extremely handled platform.

    Drupal Development Company