How To Create Module In Drupal 7
Hey guys,
In this post I'm going to show you how to create your own custom module in Drupal 7. Before you get started though, you should have basic knowledge of PHP and MySQL as this tutorial involves some coding.
Anyway, after you've installed Drupal 7, go to the directory named Sites/All/Modules and follow the steps below to create your custom module.
Step 1: Create Custom Folders
Create a new folder named 'Custom' under the root directory Sites/All/Modules. All Drupal 7 custom modules are created inside this folder.
As you know, a Drupal module contains many files, but at the basic level, you only need two files in order for the Drupal 7 module to functions. These files are:
- custom_module_name.info
- custom_module_name.module
Things to Keep In Mind before Creating the Drupal 7 module
When you create the custom drupal module folder (above), make sure to give it a name that is similar to your custom module's name. For example, if your custom module is called 'books .module' then your custom module's folder should also be called as 'books'. This will help in making your drupal development module much more convenient to develop.
Also, make sure that the name you pick for your custom Drupal module development is not similar to any existing Drupal 7 module names (both custom and contributed modules). Another thing to keep in mind is that you should use only alpha-number, lowercase letters and underscores in your custom module's name.
Now that we've clarified those points, let's get back to our tutorial.
Step 2: Create Empty Files
Inside the 'books' folder, create the following two empty files.
- books.info (this is the description of your module)
- books.module (this contains the actual code including hooks).
Now, open the books.info file and add the below code to it and hit Save.
name = books
description = "Manage Books"
core = 7.x
The 'name' above contains the name of the module or any custom text that you want to display on the module's configuration page. The 'description' describes what your module does and you can add any custom text for it that will also be displayed on the module's configuration page. The 'core' part refers to the version of Drupal that this particular module was written for.
There are more options that you can put in the .info file, but the fields above should be added.
Step 3: Insert PHP Open Tag
Open the books.module file in the text editor and insert a php open tag at the beginning of the file and save it.
Note : It is recommended that you don't include a php end tag in the above file.
Once you've done that, you can place your code into the books.module file, but first, you'll need to enable this module via the admin area.
To enable the module, just login to the Drupal admin area then click on the modules menu. Find your module by looking up its name in the grid displayed. Select your module and then click the save configuration button that you see at the bottom of the grid. Your module is now active and you can use it for further development.
Step 4: Define Module Setting
The next step is to define some setting in the books.module file so you can access it via a URL, to view or access its content. To do this, you need to perform the following steps.
- Define a path where you want to access the content from.
- Create the content
To make a path, you need to create a Drupal hook_menu function. 'Hook' indicates the name of the module, so in our case the function's name will be books_menu()
Now, open the books.module file and add the below code to it and hit Save.
/**
* implement hook_menu()
* create menu item
books_menu function will create a menu item which can be access with localhost/path/to/books
*/
function books_menu ( ) { $ items = array ( ) ; $ items [ 'books' ] = array ( 'title' => 'Books View' , //page title 'description' => 'Books Information' , //description show when mouse hover on link 'page callback' => 'books_list' , //callback function which is invoked when menu item is called. 'access callback' => true , //any user can access this page ) ; return $ items ; } |
Note
Make sure to clear the cache when you make any changes to a menu item or create a new menu item, as these might not be reflected unless you clear the cache. To clear the cache go to localhost/your/path/admin/config/development/performance
Step 5: Manage Themes
To manage the code better, you should create a separate file to manage themes. You can also do that by adding the above function into the books.module file. We'll use the former approach in this tutorial.
Create a new file named a'books.theme.inc' under the custom module folder i.e. sites\all\modules\custom\books\books.theme.inc
The above file basically allows you to display data through the theme template.
Next, you need to create a theme menu which can be used to load template files (tpl files) from the theme folder i.e. sites/all/themes/custom/{current_theme_name}/templates/books
This tpl file will house the HTML and styles of your content in whatever manner you wish to display it. Theme/Template is basically called when all your content has been fetched from the database and is ready to be displayed.
Next, open the 'books.theme.inc' in the text editor and add the below code to it and hit Save.
<?php
/*
* hook_menu to load tpl files from theme folder
* i . e sites/all/themes/custom/{ current_theme_name }/templates/books * current_theme_name = fiction */ function books_theme ( ) { $ path_to_theme = drupal_get_path ( 'theme' , variable_get ( 'theme_default' , 'fiction' ) ) ; $ themes = array ( ) ; $ themes [ 'books_list' ] = array ( 'template' => 'books-list' , 'path' => $ path_to_theme . '/templates/books/' , ) ; return $ themes ; } |
Step 6: Insert Themes File
Now, open the books.module file and include the books.theme.inc file in it, and also add the below code to it and hit Save.
require_once ('books.theme.inc'); // at the top of books.module
Code snippet starts here
Note
The codebelow should be added after books_menu() { }
// page callback function which is defined above in books menu item
function books_list ( ) { // select all rows from books table $ query = db_select ( 'books' , 'tr' ) -> fields ( 'tr' ) -> condition ( 'is_published' , 1 ) ; /* $query generates below query SELECT tr.* FROM books tr WHERE tr.is_published = 1 */ // execute above query to get the results from database table. $ result = $ query-> execute ( ) ; //send data to custom theme template //theme books_list created under sites\all\themes\custom\fiction\templates\books\books-list.tpl.php $ output = theme ( 'books_list' , array ( 'data'=> $ result ) ) ; return $ output ; } |
Code snippet ends here
Step 7: Code Insertion
Open the books-list.tpl.php file (sites\all\themes\custom\fiction\templates\books\books-list.tpl.php) in the text editor and add the below code to it and hit Save.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | < h2 > Create a Custom Module in Drupal 7 ( Part1 ) </h2 > < ? php if ( count ( $ data ) > 0 ) { echo "<table><tr><th>Id</th><th>Title</th><th>Author</th><th>Description</th></tr>" ; $ count = 1 ; foreach ( $ data as $ v ) { echo "<tr>" ; echo "<td>" . $ count . "</td>" ; echo "<td>" . $ v-> books_title . "</td>" ; echo "<td>" . $ v-> books_author . "</td>" ; echo "<td>" . $ v-> books_description . "</td>" ; echo "</tr>" ; $ count++; } echo "</table>" ; } else { echo "No records found" ; } |
And that's it!You've just created your custom module with a custom theme template. You can view it via the URL localhost/your/path/books.
Latest From Our Blog
0
Why the Daily Scrum is NOT a Status Meeting?
February 3, 2020
I am here today to debunk, The Myth "The Daily Scrum is the Status Meeting". This is a key misconception in the industry and often resu...
Read more
A Complete Guide to Custom Caching in Magento 2...
November 29, 2019
In this blog, I will explain how to create your own cache in Magento 2, and how to read and write data from that custom cache. I hope after reading...
Read more
React Hooks
September 3, 2019
React is a javascript framework for building interactive client side interfaces along with the integration with back-end server. React considers th...
Read more
How To Create Module In Drupal 7
Source: https://www.folio3.com/how-to-create-a-custom-module-in-drupal-7
Posted by: dipalmadight1942.blogspot.com
0 Response to "How To Create Module In Drupal 7"
Post a Comment