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/customtext.info
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 ?>
<?php /** * 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
<?php /** * 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:
<?php /* 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_invoke($module,$hook,$delta):
block_load($module,$delta)
$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.
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments