Breaking News
You are here : Home » Drupal » CRUD Operation in Drupal Module Development
Crud Operations In Drupal Module Development

CRUD Operation in Drupal Module Development

CRUD Operation in Drupal Module Development -

Hello Guys ! CRUD Operations in Drupal basically means to Interact with database in Drupal Custom Modules,The Operations Include Create ,Edit ,Delete and read.These Are Quite Simple If you carefully Learn and Understand Them.Drupal Is an Awesome CMS , but most people fear to use it due to lack of knowledge in its custom Module.On DesignAeon Blog Im Gonna Write This Article About Crud Operations in Drupal Custom Modules .

Before Moving On I Assume You have already Read these Posts in Module Development IN drupal.

  1. Learn Building Custom Modules in Drupal
  2. Variables in Drupal Custom Modules
  3. Demonstration Of Blocks In Drupal

[separator /]

CRUD Operation in Drupal Module Development : Tutorial -

Lets Move Towards The Tutorial to Demonstrate Crud Operations in the Drupal CMS.In this Tutorial You are going to Build a Module called ‘page manager’ ,which will include all Crud Operation in Custom Module.You will learn in this Tutorial – “How to Interact with database in Drupal”.

Drupal CRUD Operations : Requirements -

So Here are Few basic things i want you to do.Create a Folder Named ‘dapages’ in ‘sites/all/modules/custom/‘ Folder,So that we End up like Path ‘sites/all/modules/custom/dapages‘.The ‘dapages’ is the Folder where our Module Resides.Now Inside This Folder Create 3 Files named -

  • dapages.info(module Information)
  • dapages.install(the Install file)
  • dapages.module(the Module File)
After Creating THese Three Files we move to next step and add Code in files as described in tutorial also the code will be described in Next steps.

Creating The Info file : -

Now We Create The Info File For the our Module.The Module Info File Will Contain the Information about the module.Here we are creating page manager as said earlier .So Lets see its info File.

dapages.info -

[text]
name = Page Manager
description = This module provides the built-in functionality of static pages
package = Design Aeon
core = 7.x
version = 7.x
files[] = images/active.png
[/text]

[separator /]

Implement Installation & Schema Hooks to Create Table In database:

Now we have Created the info file,Now we move on and Create Install File.Yes ! That a new one for you.Firstly I want you to create ‘dapages.install’ File and Put the Code Given Below in it.

dapages.install

Now Here In above code we are implementing the Hook_install ,hook_unistall & hook_schema .Now Firstly The hook_schema is fired which will create a table in database named ‘da_pages’ and the fields defined in schema array.The hook_schema Returns Drupal a Schema array,which drupal translates into a database table.Also the hook_schema fired before hook_install by Drupal on install action performed by user.

So when we gonna activate our module the database table will be created.Also when we unistall the module the table created by hook_schema will automatically deleted by drupal.

Lets move to Next step,which is writting our Page manager module.

[separator /]

Drupal CRUD : Writing Page Manger Module :-

Now We are build Our page manager.So i want you to create file named ‘dapages.module’.In that paste the code given below.So i m gonna put whole module file code here and then explain it.

dapages.module-

Now After copying this code our module is complete.you can test it.but I gotta define the code you just pasted here.The module file code implements the hook_init ,hook_permission,hook_help,hook_menu and some call back functions of these hooks.Lets take a look at each hook we implemented.

[separator /]

Drupal Page Manager CRUD  operations: Code Definition -

Lets take a look at each hook one by one.we have Implemented following hooks in our drupal module which demonstrates the CRUD operations.

  • hook_init
  • hook_menu
  • hook_help
  • hook_permission

Hook_init -

This Hook Runs at the beginning of the page request.we have seen its working in earlier posts about drupal module development.This hook will not run on cached pages.The main motive of this is to setup global variables which we require later.

In this module dapages_init(){} is defined but not performs any task.

Hook_menu -

This hook is used to register paths for module.In case for this module we registered 5 paths .those paths are : “pages/add”,”pages/edit”,”pages/delete”,”pages/summary”,”pages/update”.the hook returns an array of these path.each path has callback function and access arguments.the Access Argument is ‘Access Page Manager’ .which we implement in the hook_permission,so gonna see that later.So firstly we gonna see callback functions and page arguments.

Hook_menu Callbacks -

1.pages/summary Callback Function : pages_summary() – This Function shows the pages summary in form of table.

2.pages/add callback function : page_add() – The Pages/add path has Page Argument as ‘page_add’ and callback is ‘drupal_get_form’. The Page Argument function will pass argument to drupal_get_form to rpageender the form.

3.pages/edit callback function : page_edit() – The pages/edit simillar to pages/add has Page Argument ‘Page_edit’ function which passes the form array to ‘drupal_get_form’ callback function.

4.pages/update callback function : page_update() – The page_update simply Toggle the ‘active’ field of the table to 1 to 0 and vice-versa . This is callback function of page/update path.It takes get parameter to alter particular entry.

5.pages/delete callback function : page_delete() – The page_delete callback deletes the particular entry and redirect to pages/summary path.

Form Submission handlers -

The Methods page_add_submit & page_edit_submit handles the submission of the form on page_add and page_edit function returned forms.

hook_permission -

This hook is used to supply permissions that module defines,So that permissions can be edited from admin panel to grant or revoke access and define access level.In this module it give drupal ‘access page manager’ permission for the module.

hook_help -

This is used to provide documentation about the module.

[separator /]

That is All ! Now You can Enable This module From admin panel.

crud operations in drupal CRUD Operation in Drupal Module Development

Drupal Module -Page Manager

Now After activating You can See your Page manafer in url http://localhost/your Drupal/pages/summary .here is how it looks like

Drupal Page manager module CRUD Operation in Drupal Module Development

Drupal Crud Page Manager

[separator /]

Working Screen Shots on Drupal 7 Page Manager CRUD :

Here are the Screen Shots or Drupal 7 Page Manager Module

[separator /]

Download Sample Page Manager Module : -

Download Sample Module Demonstrating Drupal Crud Operations.

[jbutton link="http://download.designaeon.com/dapages.rar" size="large" icon="download" newpage="yes"]Download Page manager[/jbutton]

[separator /]

That was all From Drupal Crud Operations ! Keep Visiting.

[separator /]

Tags : , , , , ,

3 Replies

  1. Mohamed Asik says:

    Good work Ramandeep! I will try this and to develop my custom drupal modules. Thanks for sharing the knowledge.

  2. Mahesh says:

    Drupal is such a beautiful platform. Your code rocks for drupal for using CRUD operations. There are some situations where a CRUD operation using drupal must make it very powerful

  3. david says:

    I’m new to drupal, and I saw your article which is very good. I downloaded and installed your module, but when trying to view the page http://localhost/your Drupal / pages / summary fails. In my case I’m putting http://crud.dev/pages/summary
    Am I doing something wrong?

Leave a Reply

%d bloggers like this: