Skip to main content
Home
Blog

Main navigation

  • Home
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Forums
  3. Knowledge Base
  4. How-To

How to create a block module?

By ongetc , 26 February, 2017

"Hello world" is a very common practice for anyone want to gain a basic understanding of any new programming language, system, CMS etc.

This article is intent to just show a very basic coverage of how to create a basic Drupal block module.

Drupal module is an essentially a hook calling to Drupal hooks. There are many hooks and you can find out more about all Drupal hooks at api.drupal.org.

But for this article, you will need to have a folder in modules call "hello" and in this folder, you should have at least two files: hello.info and hello.module. You can have another file like README.txt, LICENSE.txt, hello.install, etc.

File: hello.info
Note: This file will provide the basic information about our little hello block module


; $Id: Hello Exp $
name = Hello Module
description = Hello world in Drupal Module.
core = 6.x
package = Hello

File: hello.module
Note: This file will contain the actual code that you call to Drupal hook

<?php
// $Id: hello module Exp $

// keep the module name consistent throughout
global $moduleName;
$moduleName = "hello";

// call menu hook to create an administration menu to create content for your hello module
function hello_menu() {
global $moduleName;
$items = array();
$items['admin/settings/'.$moduleName] = array(
'title' => ucfirst($moduleName),
'description' => 'Set '.$moduleName.' code',
'page callback' => 'drupal_get_form',
'page arguments' => array($moduleName.'_admin'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}

// call admin hook to let you administrate the content of this hello block
function hello_admin() {
global $moduleName;
$form = array();
$form[$moduleName.'_show_title'] = array(
'#type' => 'checkbox',
'#title' => t('Show title'),
'#default_value' => variable_get($moduleName.'_show_title', '1'),
'#required' => TRUE,
);
$form[$moduleName.'_number'] = array(
'#type' => 'textfield',
'#title' => t('How many blocks'),
'#default_value' => variable_get($moduleName.'_number', '1'),
'#maxlength' => 3,
'#required' => TRUE,
);
$n = (int)variable_get($moduleName.'_number',1);
for( $i=1;$i<$n+1;$i++ ) {
($i<2) ? $setreq = TRUE : $setreq = FALSE;
$form[$moduleName.'_'.$i] = array(
'#type' => 'textarea',
'#title' => t(ucfirst($moduleName).' Block '.($i)),
'#default_value' => variable_get($moduleName.'_'.$i, ''),
'#maxlength' => 800,
'#required' => $setreq,
);
}
return system_settings_form($form);
}

// call block hook and show in Drupal block you assign
function hello_block($op = 'list', $delta = 0) {
global $moduleName;
$block = array();
if($op == 'list') {
for($i=1;$i

You can copy and paste this code and create in the respective file in your modules folder and have working Drupal block module. You can use this just for playing with hello concept or you can use this to create something more practical like a simple banner block that you can assign the banner to show in right, left, header or footer of your Drupal site. Just copy and paste your banner ads code in the Textarea and assign the position then it will show.

I hope you will find this little tutorial helpful!

Thanks

Forums
How-To

Reseller hosting (free)

Hosting24 (w/ ssh)

A2 Hosting

Monthly archive

  • September 2009 (14)
  • October 2009 (11)
  • January 2010 (2)
  • June 2010 (3)
  • October 2011 (1)
  • March 2012 (1)
  • July 2012 (1)
  • February 2017 (564)
  • March 2017 (5)
  • April 2017 (2)

Pagination

  • 1
  • Next page

Popular content

Last viewed:

  • SPIP
  • PHP Nuke Evolution Xtreme
  • SAPID
  • The Regex Coach
  • How do redirect www using mod_rewrite?
  • LitePublisher
  • Jease
  • phpWebSite
  • How to do normal URL with Zend Framework?
  • b2evolution

User login

  • Reset your password