Our Blog

Drupal 7 – Data Module

Oleg Videnov
by Oleg Videnov on Fri 14 December 2012 4 comments

Introduction

In a normal environment, Drupal is built around the concept of Nodes, which gives developers great flexibility to use other Drupal modules. Sometimes a custom database table needs to be created and later on manipulated easily.

The Drupal Data Module provides us with an elegant solution. In this article my aim is to introduce you to the basic usage of Data Module and several of its tricks and quirks.

Imagine that you have the following table structure:

Id / First Name /Last Name

You should go to http://localhost/drupal/admin/structure/data (where localhost/drupal is the URL of your domain) and create a new table:

creating a table in Drupal Data module

NOTE: while the module defines “custom_id” a primary key it is not an auto-increment!

Alternatively you can just create the custom table from PhpMyAdmin, flush the cache and the system will automatically try to find the custom table, with the possible option to “adopt” it.Once the table is adopted, it can be used with Views integration and take all the benefits.

Let’s fill the database with two records:

managing table fields in phpmyadmin

You can see them instantly in the website interface:

checking table fields changes in the Drupal user interface

The real power of the module is the quick and painless integration with Views

demonstrating views module integration for the module

Everyone who has used Views module (in or without conjunction with Context and Panels modules) can appreciate the possibilities the module provides.

Now, imagine for some reason you would like to change the structure of the database table:

changes seen in phpmyadmin

Like many other things in Drupal, if you want to see some real change “Flush the cache” (1-2-3 times 😉 ) .

cleaning the cache to see changes in Drupal

Click on “adjust” and you will see:

adjusting our results in Drupal backend

The tricky part is to update the cache once you update the schema information.

Tricks

You should be careful if you press the “Drop” button. It automatically deletes everything without confirmation.

If you have installed Data Entity API, in the Views Integration you can add “Edit Link” as a row field and thus make the Views $row editable.

Note: I found out that the support for MySQL DATETIME field to be missing. It is supposed to be a Schema module issue, however.

Conclusion

Data is the successor to the Table Wizard module, which has been heavily used in Drupal 6, but surprisingly, the module is not so popular in Drupal 7, and therefore a lot of open bugs and issues can be found in the Table Wizard module.

Anyhow, even with those bugs and issues the module is quite powerful and it is only a matter of time before it gets more actively maintained. The aim of this small article was to show you such a feature exists!

Oleg VidenovDrupal 7 – Data Module

Related Posts

Take a look at these posts

4 comments

Join the conversation
  • Pedro - Tue 20 August 2013 reply

    Is there any way to INSERT records with the data module? I have the data entity API installed, but I can not find any way to actually insert records out-of-the-box with this module. Am I missing something?

  • Stephen - Fri 4 October 2013 reply

    I am having trouble integrating my table in to views and cannot seem to find any information on google about this. When I go in to views I don’t see any fields that are listed under my database name. I see in your example that you have all your database columns listed in the views field area. How did you add these?

  • Stephen - Tue 8 October 2013 reply

    Stupid me, I didn’t realize that the data module created a custom view automatically.

  • Bob - Thu 19 December 2013 reply

    Thanks for this Oleg.
    I am attempting to used internal variables for product pricing. I will use the forms module to capture the information and then save it into these variables. So I would prefer using the data module to create a table with Part Number, Product Description and Price.
    Then the forms module calls these variables (in the default field) and allows the admin to update the relevant price.
    Users then enter a webform and select products using the webform module, then I do some PHP coding to add tax etc. and then the resulting quote is sent to the customer through th results email in Webform.
    The issue that I am having is just getting initializing the variables and then referencing them via PHP.
    Could you maybe supply an example of something like this to help me?

    Thanks
    Bob

Join the conversation