I have read so may article on 'How to make custom module'. There are lots of bloggers who write at at-least once about "How to make a module or custom module" that's why I have decided to write about module and custom modules. The module is used to extend the functionality of Drupal and there are more than 25000+ modules freely available for use. Different-2 modules are used for different-2 purposes like for e-commerce Drupal-commerce or Ubercart, for social business solution Drupal-Commons, for page view View, etc. These modules play a very vital role in website development.
Module type
- Core module
- Contributed module
- Custom module
Core module: Module which comes with the Drupal when you install Drupal first time. It is in the modules directory under your site's root folder.
Contributed module: Module which is contributed on the Drupal website and free to download. Anybody can download it from the Drupal.org and install it on their own instance or website.
How to install Contributed module?
There two way to install contributed module:
- First, you can copy the link of the required Drupal module from the module page on drupal.org. Log in as administrator and goto module->; install the new module and paste this link here and then click install. You can download the zip from Drupal site and provide an upload link it to module->install new module section.
- You can also extract this zip under sites->all->modules folder. After module upload, you have to enable this module under admin->modules.
Custom Module: Custom module is made by the user according to their need.
How to make a custom module? Now we will create a custom module for practice so create a folder at your Drupal installation path
sites->all->modules
on your root directory. For the custom module, we need 3 files. let's say your custom module name is custom_module then these 3 files will be
custom_module.info - Required and in this file you will declare module related information like module name, version, description, package, and core, etc.
custom_module.module - Required file and in this file, you will declare permission, path (at which your module runs), Drupal hooks (it a kind of function which is used to extend the functionality of predefined function) and also you can declare your custom function here.
custom_module.install - Optional file and this file is used to make your schema for your custom module and this file will run when you enable the module first time. In this file, you can define your own custom table in the Drupal database for saving data of your custom module.
First, create a folder named custom_module under sites->all->modules and create 2 files named custom_module.info, custom_module.module (here we will create only 2 files because we are not interacting with Drupal database that's why we have left 3rd file called custom_module.install).
Now open custom_module.info file and write this code in it.
name = Custom module
description = Create custom block for custom things.
package = Custom
core = 7.x
version = 7.x-1.0
After saving custom_module.info file now open custom_module.module file and write this code in it.
/**
*hook_permission()
*/
function custom_module_permission() {
return array( 'administer my custom module' => array(
'title' => t('Administer my module'),
'description' => t('Perform administration tasks for my module.'),
),
);
}
/**
*hook_menu()
*/
function custom_module_menu() {
$items['custom_module'] = array(
'title' => t('Custom module'),
'page callback' => 'drupal_get_form',
'page arguments' => array('custom_module_form'),
'access arguments' => array('administer my custom module'),
);
return $items;
}
//function return a form to a hook menu path
function custom_module_form($form, &$form_state) {
$form = array();
$form['name'] = array(
'#title' => 'username',
'#description' => 'choose a username',
'#type' => 'textfield',
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
- hook_permission: This function is used to define permission for access the page and its content. Different-2 have different-2 right to access the website content. By default, the administrator can access all the pages and content but for others, we have to define the permission.
- hook_menu This function is used to define the path at which our custom module will work.
- custom_module_form (a custom function which will work on your_site_name/custom_module path): This function is responsible for custom form for this custom module.
Now save your custom_module.module file and close. Now go to admin->modules and enable this module. Once the module is enabled on admin site then flush the cache under admin->config->development->performance. and check this path your_site_url.com/custom_module. you will see the form which we have created using above code.
So your custom module is ready.