Creating a basic module "hello world" with menu items and permissions

Module is a collection of files containing functionalities. In drupal to create a module, go to sites>all>modules and create a folder with the name custom. Inside that  folder create another folder  named as "Hello world" .

STEP 1: Creating .info file


 In the "hello world" create a file named as helloworld.info

helloworld.info file should contain the following things
name = hello world module,
description = module created by xyz  and
core = 7.x (if the drupal version is 7) 

STEP 2 : Creating .module file


Create another file inside hello world folder named as helloworld.module


STEP 3 : Enabling the module


Go to your Drupal Site. Click on the modules. Scroll down, you'll find your module click on the checkbox and save it. By now we have created a module named "hello world".


STEP 4 : Setting up the Permission


The next thing to do with is setting up the permission. Permission can be created using hook_permission() function. Only the users with permission granted will have access to the page. To set up the permission, we have to include the following php code in the .module file.



<?php  
function helloworld_permission(){  
$permission = array();  
$permission = array  
(  
'view welcome message'=>array  
(  
'title'=>t('view welcome message for helloworld module')  
)         
);  
return $permission;  
}  
This hook supplies the permission that the module defines. "title" provides the human-readable name of the permission to be shown on the page.

STEP 5 : Implementing menu items


To implement the menu items we should use  hook_menu function. We have to include the following code below the hook_permission() function.


 
function helloworld_menu()  
{  
$items = array(); 
 
$items['welcome_message'] = array  
(  
'title'=>t("Welcome"), 
 
'page callback'=>'helloworld_welcome', 
 
'access arguments'=>array('view helloworld welcome message'),
  
'type'=>MENU_NORMAL_ITEM,
  
'menu_name'=>'main-menu',  
);  
}  
$items['welcome_message'] = array( =>  This will create a URL welcome_message.

title => It is the page title

page callback => It is the function that will be called when the page is accessed
access argument => It is the key for an array which has permission for the current user to access the page. In this example welcome_message is the page.
type => MENU_NORMAL_ITEM =>  It is the normal menu item which is shown in menu and breadcrumbs.

STEP 6 : Create a function for 'helloworld_welcome'

function helloworld_welcome()  
{  
return "heya!! How are you?";  
} 

Finally clear the cache and you'll be able to see a menu coming up. When you click on the welcome you'll see the message heya!!How are you?


ZenRays provide the following to make you expert


  1. Fully practical and project based sessions from first class.
  2. Training by Experienced Consultants, not regular Trainers
  3. Friendly and enthusiastic faculty to clear your doubts 24X7
  4. Free Live project after the training to get you industry experience

If you want more details please visit us:

Zenrays.com Reach us at trainings@zenrays.com



Share this:

ABOUT THE AUTHOR

Ceyron Louis

Hello We are OddThemes, Our name came from the fact that we are UNIQUE. We specialize in designing premium looking fully customizable highly responsive blogger templates. We at OddThemes do carry a philosophy that: Nothing Is Impossible

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment