Drupal is a content management system that provides a flexible and a very strong platform for creating websites.
It also provides a very flexible framework to extend Drupal itself by adding more functionality. This can be achieved through modules. Drupal modules just hook into the hooks provided and add their own functionality to the system.
Drupal lets you define block modules which you can define from the admin, including where you want the block to display on your site. In this article we are going to create a block module in which you can define some text such as an announcement and display it on your site.
Creating the textblock module
To create our module, let’s first create a directory called textblock in sitesallmodulescustom. Inside the textblock directory we will create a file called textblock.info
. In this file we specify the name of the module and the description and the files of the module. This file tells the Drupal system that our module is present and to display it in the module list.
Now let’s create two files textblock.info
with the following content:
;$Id$
name = textblock
description = A module to display a block of text
core = 7.x
files[] = textblock.module
This creates a module called textblock, and then creates an empty file called textblock.module
. This will be the file where we will put the code to hook into the different hooks provided by Drupal which we will need to write for our textblock module.
Once we have done this, and if everything is correct, we should be able to see our module name in the Drupal admin in the modules list as shown. You can enable the text block module.
Adding the help link to the to textblock module
Once we have enabled our module, it does nothing as we have added nothing to our textblock.module
file. Let’s add some functionality to our module.
The first thing we add to our module is a help link. We are going to hook into the help hook provided by Drupal. We will have to write a function called as function textblock_help($path, $arg)
in our module, as follows:
function textblock_help($path, $arg) {
switch ($path) {
case "admin/help#textblock":
return '<p>'. ("This is a simple block module to display some text on screen") .'</p>';
break;
}
}
To add the help for our module we have to return the help text when the path in the help hook is admin/help#textblock
.
Once we add the code for the help hook we should be able to see an help link next to the module name as seen below.
Once we click on the help link the help will open with the text we provided as follows:
You can read more about the help hook at http://api.drupal.org/api/drupal/modules%21help%21help.api.php/function/hook_help/7
Adding the block of textblock
In a module, Drupal allows us to define one or more blocks for your module. This is done with the help of the block_info
hook. In this hook, our module has to return an associative array in which the array key is a unique identifier for the block. To implement the block_info
hook write the following function in the textblock.module
file.
function textblock_block_info() {
$blocks = array();
$blocks['text_block'] = array(
'info' => t('A block to display text of your choice'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
Once you add the above function you will be able to see the block in your block list as follows.
For more information about the block_info
hook, you can visit http://api.drupal.org/api/drupal/modules!block!block.api.php/function/hook_block_info/7
Adding configuration for the text block
Having added the block info, we will see how we can configure the block. For our textblock we want to take the text to be displayed on the site from the admin. To configure the block we have to hook into the hook_block_configure hook
. This hook is passed as the unique identifier which we gave for the block and this hook has to return a form in case we want extra fields in our configuration.
The implementation for our hook will be as follows:
function textblock_block_configure($delta = '') {
$form = array();
if ($delta == 'text_block') {
$form['text_block'] = array(
'#type' => 'fieldset',
'#title' => t('Please enter the text below'),
);
$form['text_block']['text_block_full_text'] = array(
'#type' => 'textarea',
'#title' => t('Text'),
'#default_value' => variable_get('text_block_full_text', ''),
);
}
return $form;
}
In this we check that the unique identifier is the one for our block. If that is the case, we create a form with one fieldset which just displays the text that enter the text below and the other as text area in which one can add the text which he wants to display on the site.
Once we add the form and click on the block configure button the following will appear, in which we can add the block title and the text.
To know more about block_configure
you can visit http://api.drupal.org/api/drupal/modules!block!block.api.php/function/hook_block_configure/7
Saving the configuration for the text block
Once we have added the form to configure the block, we have to ensure it saves the value of the text once the user enters the value and saves. To save that value we have to hook into hook_block_save
, get the value of the text area and save it to the Drupal database so that it can be retrieved later.
The code for hook_block_save
for our module is as follows:
function textblock_block_save($delta = '', $edit = array()) {
if ($delta == 'text_block') {
variable_set('text_block_full_text', $edit['text_block_full_text']);
}
}
In this function we just get the value of the textarea and then use the Drupal function variable_set
to store that in a variable called text_block_full_text
. Now once the user enters the text and clicks on save the configuration and text will be saved and the following message will be displayed.
Displaying the text of the front end
Once we have saved the text to be displayed on the front end using the block configuration we are going to see how we can display it. To display the content we will have to hook into hook_block_view
.
The code for this hook is as follows:
function textblock_block_view($delta = '') {
if ($delta == 'text_block') {
$content = variable_get('text_block_full_text', '');
$block = array(
'content' => $content,
);
return $block;
}
}
In the above function if the unique identifier for the block is text_block
, then we get the text which we saved in the database using the function variable_get
. Then we return that content for the block.
Once we have finished this hook we will be able to see our text on the site as seen below.
Conclusion
Drupal provides a flexible framework to extend and to create sites on the basic Drupal platform. The hook mechanism of Drupal makes it easy for module developers to hook into the system and create more functionality.
In this article we created a block module which helps user to display text on the site using the different hooks of Drupal with ease. Let me know how you go with it.
Abbas is a software engineer by profession and a passionate coder who lives every moment to the fullest. He loves open source projects and WordPress. When not chilling around with friends he's occupied with one of the following open source projects he's built: Choomantar, The Browser Counter WordPress plugin, and Google Buzz From Admin.