How to Expose custom module table to views in drupal

Drupal Views module is a Simple Visual Query Builder. It reduces the Need for writing the Custom code. Now with CCK and Views we can almost Store any Data as Content Type and list them in various ways in site with the Help of Views.

I decided to explore and learn the views module coding, In the Learning Process i found that most of the core modules have implemented the Hooks Provided By Views Module and integrated their tables and data to Show up in Views List.
The Below Article will Show you how to integrate your custom module with Views.

Note: We can create any content type and display them using views without custom tables(in most of the cases we can do this with node id). But in some cases we may have data that are not of type node in such cases integrating our data with views module will be helpful.

Compatability: Drupal 7, Views 3

STEP 1:

I begin with my mydons_learnviews.info, Apart from the regular .info directives like module name,desc,version etc, we need to specify the dependencies with views module, and the .inc file that implements the views hook.

STEP 2:

Our module will create the custom table automatically when the module is enabled from admin. For this purpose we must have our .install file which contains the schema information.

STEP 3:

The below code shows .module file of our custom module. Here i just Specified the Views API version and path of the views.inc file.

STEP 4:

Below is the code for our views/mydons_learnviews.views.inc file,

STEP 5:

Now Enable the Module in drupal admin and clear the performance caches. After enabling the module our custom table is created in the DB.
STEP 6:

Now we enter some sample data for our Custom table.
STEP 7:

Now we are in the final part , navigate to admin/structure/view and create a new view with name mydons a new option called Mydons_Customtable will be populated in the Show dropdown

STEP 8:

After configuring our views Final display looks like this.


  1. Sriraman.v says:

    Super boss …………..:)

  2. Manny says:

    I have searched high and low for an article like this! THANK YOU SO MUCH!

  3. Mike says:

    Hey — I am not able to get this to run. The install creates the table, but I dont have it as a filter option in the views interface. I put a watchdog message in the .inc flle, views_data() hook, and this has never stored any data in the reports. Does this indicate that the inc file is not being reached?

  4. ishi says:

    what should I do if it is used two fields together as a primary key?

  5. Tim says:

    Wow Ramesh. Great job. Simple, easy to follow code. I can bet there are many people looking for something like this, even if it’s just to get them started in the right direction. Thanks much for taking the time to put this together.

  6. Your example saved me a lot of time, thx a lot!

  7. ftkr says:

    goog job!
    what should i do if it is used more than one table?

  8. Josh Sommers says:

    This post was exactly what I was hoping to find! Thank you for the examples!

  9. ftkr says:

    please help,
    i has rewrite your mydons_learnviews
    but my module doesn’t appear on views UI, what happen?
    custom module that appear only “Mydons Customtable”

    thank’s..

    • Shankar says:

      Hello !

      Can you explain more about your problem like

      what’s the name of your module ?
      Till which step you are able to get through.

  10. An excellent tutorial — but what to do if one of the custom table fields is a multivalue?

  11. vinoth kumar says:

    Hello Friend

    I have Follow all the steps, but step 7 “a new option called Mydons_Customtable will be populated in the Show dropdown” not appear for me.

    What mistake I have done? I don’t know

    Kindly help me to fix this problem.

    thanks in advance.

Leave a Comment

Your email address will not be published. Required fields are marked *