Drupal 7: Custom Module – Part 2

This is second and last part in Custom Module series. We will use custom module that we created in last tutorial and create a custom front page for a freshly installed Drupal installation

[b]Requirements:[/b] http://www.rattanpal.com/content/drupal-7-custom-module-part-1.aspx

Create a new template called [i]sites/all/theme/page–front.tpl.php[/i]. This template will override the default template and will be used for any page that we decide to use as front page

Edit [i]mymodule.info[/i]. We will ue this file to add custom page. Following code needs to be added to this file. Comments have been added to all code snippets which describe all the items in each function.

/**
* Implements hook_menu()
*
* This will create a URL that we can use to access this page
* This page then can be accessed using http://www.example.com/home
*
* 'page callback': Function to be called to get content when this page is accessed. Output returned by that function is used for "$content" value on that Drupal page
*
* 'access arguments': It adds a key which will be used by Drupal to store ermissions for this page
*
*/
function mymodule_menu() {
$items['home'] = array(
'title' => 'Home',
'page callback' => 'mymodule_home',
'access arguments' => array('access_mymodule_home_page'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_permission().
*
* 'access_mymodule_home_page': Its the same access key defined when 'home' menu item was defined in mymodule_menu()
*
* 'title' & 'description': These will appear on Drupal "Permissions" page to describe what these are for
*/
function mymodule_permission() {
return array(
'access_mymodule_home_page' => array(
'title' => t('Access mymodule Home Page'),
'description' => t('Use mymodule Home Page.'),
),
);
}

/**
* Implement home page
*
* Any string value returned by this function will be used by Drupal for "$content" variable as output of that page
* This can be anything. We could even use it to deliver RSS/JSON/JSONP output if needed
*/
function mymodule_home() {
$content = '';
return $content;
}

 

Clear Drupal cache for Drupal to read new changes to this file.

If you haven’t already then enable this module in admin/modules.

Now go to Site Information (admin/config/system/site-information) and add “home” in “Default front page”. This will ensure that this new custom home page will become our front page and that will also make Drupal use page–front.tpl.php as template

Before your front page can be accessible to anonymous users, you need to give permissions to anonymous users to access this new custom page. To allocate permissions, go to User Permissions (admin/people/permissions). Look for “Access mymodule Home Page” (title of the page). Now check permission box for anonymous user and save permissions.

Voila!. You have just changed your site’s front page to be a custom page. You can add your content directly in page–front.tpl.php or in mymodule_home() function.

This concludes our 2 part series on how to add Custom Module and Custom front page.

Leave a Reply