Sunday, August 7, 2011

Custom drupal module drupal 6 , drupal 7, drupal 8 - Check differences

Drupal follows the modular structure.Features of drupal can be extended and implemented by adding a module in it without touching drupal core script.

Steps to make drupal module drupal 6

1) Make a new file with name module_name.info (it contain following )
; $Id$
name = Test Module
description = Allows users to implement new features.
core = 6.x
package = Drupal Development

It has few line which contain information about module like module name, description of module , drupal version it supports, and package you want to keep this module
2) Make one installation directory . If you want to add some mysql table in
module than need of this file
It contain few installation function
a) function table_name_install(){
drupal_install_schema('table_name');
}
b) uninstall function , when module will uninstall than it delete table from
database
function annotate_uninstall() {
drupal_uninstall_schema('table_name');
}
c) Implementation of hook_schema()
function module_name_schema() {
$schema['test_description'] = array(
'description' => t('Stores node test_description that users write.'),
'fields' => array(
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('The {node}.nid to which the test_description applies.'),
),
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('The {user}.uid of the user who created the test_description.')
),
'note' => array(
'description' => t('The text of the test description.'),
'type' => 'text',
'not null' => TRUE,
'size' => 'big'
),
'primary key' => array(
'nid', 'uid'
),
);


return $schema;
}

3) create another file with name module_name.module
We need to read this first before making a new module
http://api.drupal.org/api/drupal/includes--module.inc/group/hooks/6
here it is a list of available drupal hooks

Allow modules to interact with the Drupal core.

Drupal's module system is based on the concept of "hooks". A hook is a PHP function that is named foo_bar(), where "foo" is the name of the module (whose filename is thus foo.module) and "bar" is the name of the hook. Each hook has a defined set of parameters and a specified result type.

To extend Drupal, a module need simply implement a hook. When Drupal wishes to allow intervention from modules, it determines which modules implement a hook and calls that hook in all enabled modules that implement it.

Now view the hook_menu which enable the menu in drupal
function module_name_menu() {
$items['admin/settings/annotate'] = array(
'title' => 'Annotation settings',
'description' => 'description of module menu',
'page callback' => 'drupal_get_form',// will set the drupal form which will be implemented later
'page arguments' => array('module_name_admin_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'module_name.admin.inc',
);

return $items;
}


Steps to make drupal module drupal 7

Create Custom Module

In your modules directory (sites/all/modules) create a folder for your module. For this example our module will be called anshulblockmodule, so we would create the directory sites/all/modules/anshulblockmodule. Your module can be called whatever you like, just make sure that when following this tutorial your replace "myblockmodule" with your modules name.

1. Create a .info file

All modules in Drupal require a .info file to let Drupal know that our module exists, what it's name is, as well as other information about our module. In your module folder (sites/all/modules/anshulblockmodule), create a file named anshulblockmodule.info. In myblockmodule.info enter the following:

name = AsnhulBlockModule 
description = Tutorial Modules for beginners 
package = Tutorial Modules 
core = 7.x 

The Explanation

This is how the above breaks down:
  • name is the human readable name of your module. This is required for all modules, It is shown on the modules administration page.
  • description is a short message explaining what this module does. This is also visible on the modules administration page.
  • core describes which version of Drupal this module works with. This is required and should be "7.x" for all Drupal 7 modules.
  • package is optional, but I recommend it. The main purpose of this is to define what category to list the module in on the module admin page.
There are other variables you can define in this file, but these are the main four I put in all modules I create. For more information on module .info files check out Writing module.info files.

2. Create a .module file

There are two files which every module must have. First is the .info file, which we already created. The second is the .module file. Despite the file's extension it is really a PHP script. This file is evaluated every time Drupal a Drupal page is load. The main purpose of this file is to define functions which implement hooks. Hooks are evaluated during different Drupal events and can be called by Drupal itself or other custom modules. (For further reading on hooks: Hooks 101)
For our module we will create a new file in our module's directory (sites/all/modules/anshulblockmodule) called anshulblockmodule.module. This file will implement two hooks, one to let Drupal know that we are defining a new block, and one to let Drupal know what the block's content is. The file will contain the following: 
<?php
/**
 * Declare what blocks are provided by this module.
 * Implements hook_block_info().
 */
function anshulblockmodule_block_info(){
    $block['static_content'] = array(
        'info' => t('Drupal tutorial'),
        'cache' => DRUPAL_NO_CACHE,
    );
    return $block;
}

/**
 * Define what our block is going to look like.
 * Implements hook_block_view().
 */
function anshulblockmodule_block_view($block_key){
    $block = array();

    if($block_key == 'static_content'){ //We only want to define the content of OUR block
        //This is the title of the block.
        $block['subject'] = t('lorem ipsum sit');

        //Define the block content.
        $block['content'] = t('lorem ipsum ').(strtotime("2015-10-21")-time()).t(' seconds.');
    }

    return $block;
}

 In this way can create custom block and can put anywhere in template.

It is very useful to fetch any content as block content from drupal instead od creating views everytime. it increase the performance of drupal website. you can set the cache_Get and cache_Set method for caching. we will those example later.

No comments:

Post a Comment