How to create a basic drupal 7 module for customtext block

How to create a basic drupal 7 module for customtext block

Objective of Custom Text Module: Display Google Ad (or any custom text ) somewhere on your drupal 7 page. Since this tutorial is created for a primary module developer so I have created this module text within code itself. So that a basic developer can at least got to know the skeleton for “How to create a basic drupal 7 module for customtext block ”.

Name of Drupal7 Module: CustomText

Note: Don’t start your module as numeric.

Directory Structure of Module:

For each new module we need to create a directory under directory path “sites/all/modules/”

So we will create new directory under above directory path. Below are some of the file we need to create.

Create Directory: sites/all/modules/cutehits

Create Directory: sites/all/modules/cutehits/CustomText

Create file: sites/all/modules/cutehits/CustomText/

Need to write below text into this file.

name = CustomText
description = CustomText module
core = 7.x
package = Cutehits

Create file: sites/all/modules/cutehits/CustomText/customtext.module

Need to write below text into this file. Here notable that in module file no need to end it with ?>

 * Implements hook_block_info.
function customtext_block_info() {
  $blocks['custom'] = array('info' =>t('Custom Text block'));
  return $blocks;

Create function that return output:

For this we again need to create a function that will return that custom text as output. For this we need to write another function into customtext.module file itself. The function will look like

 * Implements hook_block_view.
function customtext_block_view($delta = '') {
  global $user;
  $account = $user;
  $block['content'] = t('my custome text comes here');
  return $block;

How to enable module: Login to admin section and navigate to admin/modules.

Click on checkbox showing left side of Cutehits module and Click on “Save Configuration ” button showing at the end of page

Placing the contents of a block in any location:

How to insert a block into a node or template in Drupal 7:

/* Print custom block content */
//$block = module_invoke('block','block_view','1');
//In our case we will call it like
$block = module_invoke('customtext','block_view','cutehits-customtext');
//Showing block output here</p>
<p>print "Cutehits Module Text Shown from Block".render($block['content']); 

Parameters for the functions:

$module = module name (like block or views). This is the name of module . In our case we can see sites\all\modules\cutehits\CustomText\customtext.module file exists so our module name would be customtext
$hook = hook inside the module to invoke (like block_view)
$delta = identifier of the block (like test_view-block or 1). This argument is not mandatory

Note: You can write render code on any template file . For example you can write it under your page.tpl file under your theme folder.

You can download the source code from here.

This module is created for primary developers only in further tutorial about module we will see how to manage module text from admin section as well.

Some Knowledge facts about this module: While creating the module or going through above tutorial you might have some question in your mind.

Question1: What are the hooks in Drupal:

Answer: Hooks are basically PHP functions named like foo_bar() where “foo” is the name of the module and “bar” is the name of the hook. The purpose of hook to extend the existing functionality of drupal without touching core code base.

Question 2: Difference between hook_block_info and hook_block_view hook in drupal:

Answer: Hook_block_info: hook_block_info are basically used to specify initial block configuration settings.This hook is declares to Drupal that what blocks are provided by your module.

Hook_block_view: It basically return renderable view of the block. In other word this hook basically return the block content that will be shown on the page.

Question 3: What is the purpose of using t() in drupal content:

Function t() in drupal is used for sanitization of text. By passing your text as argument specify that you are going to use it as text only. If there is some dynamic value to be passed within this t() function then we need to pass it like text and their argument as well.

I hope you like the above tutorial. Thanks for your time.

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...