Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

Module is a collection of files containing functionalities. In drupalto 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 drupalversion 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.




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_menufunction. 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 [email protected]





This post first appeared on Best Software Training Institute In Bangalore, please read the originial post: here

Share the post

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

×

Subscribe to Best Software Training Institute In Bangalore

Get updates delivered right to your inbox!

Thank you for your subscription

×