Drupal Module: Creating First Simple Module


Drupal Module Creating First Simple Module Tutorial. Now we will dive in and create our first module. Our first module will show simple text, format it, and display it as a block in the site's layout. We will cover the following steps:

  • Creating the .info and .module files
  • Creating a new module
  • Using basic hooks
  • Installing and configuring the module

There are two files that every module must have (though many modules have more). The first, the .info file, we examined above. The second file is the .module (dot-module) file, which is a PHP script file.

A Place for the Module

In Drupal, every module is contained in its own directory. To keep naming consistent throughout the module (a standard in Drupal), we will name our directory with the module name.

In this practice, we will create a module named "helloword". So we have to create a directory named "helloword". Place it within \sites\all\modules.

Drupal Module Creating Module Folder Tutorial

Creating a .info File

Drupal components use the information in this file for module management. The .info file is written as a PHP INI file, which is a simple configuration file format.

The .info file must follow the standard naming conventions for modules. It must be named .info. is the same as the directory name. Example helloword.info.

Drupal Module Create info file tutorial
;$Id$
name = "Hello Word!"
description = "Our first module in drupal module development"
core = 6.x
php = 5.2

A Basic .module File

Drupal Module Creating .module file tutorial

A Block Hook

The hook_block() function isn't just for displaying block contents, though. In fact, this function is responsible for displaying the block and providing all the administration and auxiliary functions related to this block

Write these code at helloword.module file:

<?php
// $Id$
/**
* @file
* Module for showing hello word
* @see http://www.phpeveryday.com
*/

/**
* Implementation of hook_block()
*/
function helloword_block($op='list' , $delta=0, $edit=array()) {
	switch ($op) {
		case 'list':
			$blocks[0]['info'] = t('Hello Word!');
			return $blocks;
		case 'view':
			$blocks['subject'] = t('Hello Word!');
			$blocks['content'] = t('This is our first module!');
		return $blocks;
	}
}

?>

Our module will display information inside a Drupal block. To do this, we need to implement the hook_block() function. When Drupal calls a hook_block() function, Drupal passes it as many as three parameters: $op, $delta, $edit

$op can have the following four possible values:

  • list: This is passed when the module should provide information about itself.
  • view: to provide content for displaying to the user.
  • configure: an administration form used to configure the block.
  • save: configuration information from the form data generated by configure needs to be saved.

The $delta parameter is set during a particular operation. The $edit parameter is used during configuration (when the save operation is called). We dont need in this example.

Installing a Module

Step #1: Enabling The Module

A module is enabled through the Drupal Administration interface. Once logged into Drupal, navigate to Administer | Site Building | Modules in the left-hand navigation

Drupal Module Administration Menu Tutorial

To activate the module, simply check the box under the Enabled heading, and then click the Save configuration button.

Drupal Module Activate the module tutorial

Displaying the Module's Content

Just as with enabling the module, this is done through the administration interface. Go to Administer | Site Building | Blocks to configure block placement. We will configure our module to appear in the right sidebar.

Drupal module block placement tutorial

Make sure to press the Save Blocks button at the bottom.

Drupal Module Showing Block Tutorial



Bookmark and Share Tag: Drupal, CMS, Drupal Module Category: PHP Framework Post : September 30th 2010 Read: 25,767

advertisements


blog comments powered by Disqus