Drupal is an open source content management platform powering millions of websites and applications. It’s built, used, and supported by an active and diverse community of people around the world.-Drupal.org
Modules are set of functions and hooks(read more about hooks here). As part of module development in Drupal we have to write functions to add additional features, sometimes we need to override the core functionalities using hooks.
We are going to create a sample module that lists all the movie contents in a block, in this tutorial we will cover hook_menu also. My idea is to create a block that we can call in any of existing pages and a menu item calls this block, this represents a page that calls a block.
Creating the module structure
Creating the module structure is important in Drupal. You need to create your modules in the directory sites/all/modules/[module_name]. But widely people are following another convention it’s easy separate our custom modules from the third party contributed modules.
So it can be two folders
- Contributed – modules written by the Drupal community and shared under the same GNU Public License (GPL) as Drupal.
- Custom – modules created by the developer team, it’s us.
So I am moving this module to custom folder, so our final path is sites/all/modules/custom/[module_name]
Now we create tow files module_name.info and module_name.module, these two files are minimum required files for drupal modules. You can have more files depending on your functionality for the particular website.
.info file contains the info of your module, it says what does your module do. And all the other functions and hooks will come inside the .module file.
Below you can see our module_name.info file, it’s just a simple info file, and you can have more params values in the .info file, just have a look in the Drupal website for more info. This Drupal info file is responsible for all the configurations of module.
core = "7.x"
description = "An sample module used to demonstrate Drupal module creation."
name = "Movie listing samples"
And here we starting with functionality. We start with a hook_help function to create a basic intro about the module, it will act as help page of this module. However this is not necessary to include this, but it comes under the best practices in programming.
Next we are creating a menu item using hook_menu, so you can access the url by calling http://yoursitename.com/movies/listing.
So we are done with the menu item, it means we got a web accessible link. Here we have some content to show on the template page. I have written a Drupal select query to get some data from article content type. Here you can see I am returning with theme function. So we need to define a hook_theme to define our template.
Next, is defining our theme and which location is our theme resides. In the return array, we are pointing a template to show our content.
Your theme path will in this case sites/all/modules/custom/[module_name]/theme/module_name_movie_listing.tpl.php
Creating a block
Actually, we are done with the page of movie listing. Next, we are moving to show how to create a block and assign to any page.
hook_block_view return a rendered or renderable view of a block.
$delta: Which block to render. This is a unique identifier for the block within the module, defined in hook_block_info().
Hurrah! You are done with your module, you can enable the module in the backend. Don’t forget to clear your cache in order to show the page in the frontend.
1) Module is listed in the module listing page
2) Our block is listed in the block listing page of admin side