Back to top

HOWTO Use Drupal nodes to display information from other sources

I've been asked several times:

"I have data in a database and want to display it in Drupal and probably in a Drupal node. How do I do that?"

Well, here are some thoughts on that question. The answer may depend on the kind of data that you've got. Is it static or do you occasionally update it? Do you want users to be able to edit that data inside of Drupal and have the changes saved back to your database? Do you need Drupal to handle access control?

Just use a node

One solution is to simply use Drupal nodes. Write a script that takes your data and creates a new node - record for record - from your current data. If you have a few columns of data that are selections from a limited set you could convert those to taxonomies: e.g. you have a list of restaurant names with descriptions, a ranking (good/bad/fair) and a restaurant genre (Thai, Chinese, French, B-B-Q) then you could map the name into the title, the description into the body, the ranking into either a taxonomy category or as votes using the vote api, and the genre into a taxonomy category. This is ideally suited to relatively simple data.

Use Flexinode/CCK

Flexinode/CCK allow you to create somewhat more complex nodes where you create the necessary fields and Flexinode/CCK create the underlying database and HTML forms to be able to edit those fields. Then you create a few test nodes manually through the Drupal interface to see how fields get stored in the database and finally write a script to take your data and insert it into the database. It might make sense to make use of the fields-to-taxonomy system described above in addition to Flexinode/CCK.

Qcodo

QCodo is a code generation tool that can be used to create the "CRUD" (Create, Retrieve, Update, Delete). I know of someone working on a way to integrated Qcodo into Drupal. If you're interested in that idea, let me know.

Synchronizing

If it is updated occasionally, you could keep a reference table from your data to the node ID value and create a script that not only creates the nodes but also can update them as necessary. The main concern in that script would be that someone had edits the node - how should you handle that? What if someone edits the node and the underlying source data also changes? "Synchronizing" the data can be a tricky problem, but it can also be simple. Maybe you just don't care about the source data once you've got it in Drupal - make the Drupal version the important version that everything else draws from.

Category: 
People Involved: 
timeline: