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:
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:
You can see them instantly in the website interface:
The real power of the module is the quick and painless integration with Views
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:
Like many other things in Drupal, if you want to see some real change “Flush the cache” (1-2-3 times 😉 ) .
Click on “adjust” and you will see:
The tricky part is to update the cache once you update the schema information.
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.
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!