How To Create Custom Module in drupal ?

  0 comments

Creating Drupal modules is easy.In drupal, modules are plugin that extends drupal's functionality. In fact, the code in this blog is an illustration of how easy module coding can be. We are going to create our first module(mymodule) with only one directory and two small files.

Creating Module Directory

Go to drupal modules directory i.e /sites/all/modules and create a directory named 'custom' and inside custom create another directory named 'mymodule'. So final path of your custom module is /sites/all/modules/custom/mymodule.

Atleast two files needed to create a module. These file are .info and .module. So create these two files mymodule.info and mymodule.module in your module directory

files list image

Contents of .info file

mymodule.info

name=My Module description = "My Module" package = Custom core = 7.x version = 7.x ; ;These are additional fields of .info files. ; ;php = 5.6 ;dependencies[] = path

To know more about .info file please visit Drupal site

Write Module file In .module file we are going to implement hook_menu().Here, hook indicates your module name. So your function name will be mymodule_menu().

<?php // $Id: mymodule.module /** * implements hook_menu() */ function mymodule_menu(){ $items = array(); $items['mymodule'] = array( 'title' => "My Module", 'page callback' => "mymodule_details", // after visit drupal_test/mymodule, mymodule_details() function is called 'access callback' => true, // must return true, otherwise it will not visible as menu item 'type' => MENU_NORMAL_ITEM, // drupal's default menu type 'weight' => '10', // we want to display person link below in our nav menu ); return $items; // finally, do not forget to return $items array }

Afer implemetation of hook_menu()

,you need to create Callback Function. This function will be fired when you click mymodule link.

/** * callback function for person * */ function mymodule_details(){ $output = 'Company: DrupalChamp </br>'; $output .= 'Overview: We develop interactive world-class Drupal websites for all kind of businesses needs. </br>'; $output .= 'City: Delhi </br>'; $output .= 'Country: India </br>'; return $output; }

Enable Module

Now its time to enable your first module(mymodule) and check "mymodule" link.
Go to Administer >Modules (admin/modules) and enable the new "mymodule" module that you've created. You will see your module under 'custom module' fieldset. Click Save configuration button.

module_enable image

Now visit mymodule link you will see your desired output like:

result image

Add new comment