Sarkari Naukri and Naukri

Thursday, 4 April 2013

Create custom block drupal 7

Some easy steps to create block through module :
Step 1 :
Create a folder (give a folder name e.g., mymodule) in the modules directory of your Drupal site.  The name which you are giving as a folder name will become your module name. To create a module there must have two files inside the module folder named as:
( a) mymodule.info (b) mymodule.module
mymodule.info file contains the general information of the module you created and mymodule.module file contains the main coding which creates the block.
# We are considering the module name as customblock and hence filenames are customblock.info and customblock.module
Step 2 :
Copy the code given below and paste it inside the customblock.info file.
; $Id: customblock.info,v 1.4 2009/02/18 22:02:46 dries Exp $
name = Custom Block
description = Creating Custom Block to demonstrate the block creation through module.
package = Diadem
version = VERSION
; Information added by drupal.org packaging script on 2008-10-08
version = “5.11″
project = “drupal”
datestamp = “1223496909″
This is the general information about the module and description field reflect the purpose of creating the module . Give the correct version which you are using for your drupal site. You need to change this information as per your requirement.
Step 3 :
Always remember that all the function name should start with the module name, e.g.  modulename_node_info(), modulename_perm(), etc. Copy the code given below and paste it inside the customblock.module file. You will observe that there has no php end tag at the end of the file. It is recommended in drupal that we donot need to end the php tag in the module pages.
<?php
/**
* Implementation of hook_node_info().
*/
function customblock_node_info() {
return array(
‘customblock’ => array(
‘name’ => t(‘Custom Block’),
‘module’ => ‘customblock’,
‘description’ => t(‘How to create a custom Block.’),
)
);
}
/**
* Access Permission of this module by hook_perm();
*/
function customblock_perm() {
return array(‘access customblock content’);
}
/**
* Implementation of hook_block().
*/
function customblock_block($op=’list’, $delta=0) {
// listing of blocks, such as on the admin/block page
if ($op == “list”) {
$block[0]["info"] = t(‘Custom Block’);
return $block;
}
else if($op == ‘view’)
{
$block_content=”;
$block_content.=’<div style=”border:1px solid #D6D6D6; background-color:#F2F2F2; padding: 5px;”>’;
$block_content.=’<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Why do we use it? <br />
<a href=”http://drupal.org/” target=”_blank”>Click Here</a> – Drupal.Org
</div>’;
$block_content.=’</div>’;
$block['subject'] = ‘Custom Block’;
$block['content'] = $block_content;
return $block;
}
}

No comments:

Post a Comment