Drupal 7: Data module

David G - DrupalThe Data module is 1 of 2 modules in Drupal that attempt to let you manage and use database tables not created by Drupal. The other module is called Table Wizard (which appears abandoned now). By itself the Data module allows you to create SQL tables in Drupal using a basic admin UI (a baby version of phpmyadmin for example). It also lets you adopt previously defined sql tables in your database, which are not created or maintained by drupal initially.

After installing the Data module (and it’s dependency the Ctools module). You’ll then be able to create simple SQL tables that support an API to be interacted with from custom modules you can create.

Example Data admin page. In this example I have a previously created table being managed by Data.

Example Data admin page. In this example I have a previously created table being managed by Data.

Then you can easily create a new table by selecting the Create New Table option. This will walk you through a small wizard to make you’re new table. The other option is called Adopt Table which I’ll describe later. For example we can create a simple table Foo1 with some simple columns:

1st step of wizard form to create a new table managed by the Data module.

1st step of wizard form to create a new table managed by the Data module.

2nd step of the create table wizard. We can define fields within our table.

2nd step of the create table wizard. We can define fields within our table.

Out of the box the Data module supports the following fields: Integer, Serial, VarChar, Text, Float and Geometry (geometry my be an add-on field from other modules installed on my drupal site in this case).

Upon creating the table the Data module admin page then lists our custom table(s) with available actions on the table:

Successful creation of the custom table using the Data module. The Data admin page lists the table.

Successful creation of the custom table using the Data module. The Data admin page lists the table.

The actions include:

  • Edit:  Modify the table structure.
  • Disown: Have Data unregister the table from its management system (making the underlying API faster). But retain the data and table in the underlying DBMS.
  • Drop: Drop the table from the DBMS and Drupal.
  • Export: Export the table structure to code.
  • View Records: View the data in the table.
  • Edit View: Manage the Views page through the Views Admin UI for this custom table.

So thus far on the backend of our system Data has created a simple SQL table:

phpMyAdmin view of the table structure created by Data module for custom table foo1.

phpMyAdmin view of the table structure created by Data module for custom table foo1.

And ultimately we’ve created a View to display foo1 on our website:

Example display of view of our table foo1.

Example display of view of our table foo1.

The view of foo1 is just a drupal View, which can be tweaked as needed.

The view of foo1 is just a drupal View, which can be tweaked as needed.

Earlier I mentioned the Adopt Table option. This lets you adopt a non-drupal table from your Database and make it usable through the Data API and viewable by Views. For simplicity I will use phpMyAdmin to copy a table from a different project into my drupal database. Then Data can see it to be adopted:

Adopting a Bibliography table from an external project.

Adopting a Bibliography table from an external project.

In order to View Records for our new table we must first goto the Edit View page and define the Views handler mappings for the fields within the table:

Configure Views handlers for adopted table.

Configure Views handlers for adopted table.

Basically, I would accept the default values this form presents you. And then try to goto the View Records page and if it’s blank or gives you an error — then go back to Edit View and attempt to pick different field handlers than the defaults provided.

Then our data is visible in Drupal:

Final views display of adopted table.

Final views display of adopted table.

This is most of the functionality of the Data module. Also look at what it can do with Feeds and Entities from the project page; but those topics are more advanced and I will not cover them at this time.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

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