I'm about to write a custom module dealing with chemical hazards symbols. It's basically a 300-item dictionary of simple objects: a symbol, its description (around 100 chars) and in some cases a supplementary short information.

I'm still undecided whether I'd make my module a text filter, field or entity, but regardless (I think) of the way I choose, I'm clueless about what would be the Drupal way to install this simple dictionary, a PHP variable, a persistent variable, or a database table?

My guess would be that storing it as PHP variable would not burden the memory too much, it would provide quick lookup access, and would save database queries. Would there be any benefit to choose the table option?

What is limiting me in using taxonomy here is that some terms would have to be supplemented by a piece of information on a binding/instance level. For example, there would be a symbol (term) P0123: Store in temperatures below %1 C/ %2 F or P2345: Wash %1 thoroughly after handling. For each node referencing that term there should be a possibility to add the precise values that might be different per node. That is the reason I have been thinking of making it a text filter that would parse line:

P0123,10,50 (<- term with supplementary contextual values, %1 and %2)
P2345,en:hands|de:Hande|es:manos (<- i18n-savvy contextual string)
P3456 (<- term without additional info)

I have already filed a feature request for the Relation module, to provide labelling of relationships on instance level. We'll see if the authors consider it as worth implementing for a wider public. For now I'm trying to find a swift workaround.


If you were to use a PHP variable, you would need code that (for example) in hook_init() initializes that global variable, and that code would be executed at every page request.

If you were to use a persistent variable, you could initialize it during the installation of your custom module (via variable_set()). You would still have the problem of having the content of those Drupal variables loaded in memory every time Drupal bootstraps, which includes also the case Drupal is answering to an AJAX request. This should be avoided, especially if those data are necessary just on specific pages. Also, a module should generally limit the number of calls to variable_set(), since each calls clears the cache used to contain persistent variables, and it would cause any successive call to variable_get() to query the database again.

The only alternative you have is creating a database table when your custom module is installed.
With Drupal 7, that means to just implement hook_schema(), and put that function in the .install file of your module. With Drupal 6, that means to implement hook_schema(), but also hook_install() with code similar to the following one.

function mymodule_install() {
  // Create tables.

You can use a custom cache table to store the serialized form of the data more frequently loaded from the database; this is what Drupal core normally does.
In the case the data is requested more than once in the same page request, you could also store data in a global static variable created with drupal_static(). Then, the code for handling the data would be similar to the following one.

function archiver_get_info() {
  $archiver_info = &drupal_static(__FUNCTION__, array());

  if (empty($archiver_info)) {
    $cache = cache_get('archiver_info');
    if ($cache === FALSE) {
      // Rebuild the cache and save it.
      $archiver_info = module_invoke_all('archiver_info');
      drupal_alter('archiver_info', $archiver_info);
      uasort($archiver_info, 'drupal_sort_weight');
      cache_set('archiver_info', $archiver_info);
    else {
      $archiver_info = $cache->data;

  return $archiver_info;

The function first check if the static PHP variable has been initialized; if it is empty, then it looks for the data in the cache. Only in the case the cache is empty, invokes the module hooks to built its data.


In D7 I would make a Taxonomy out of such data. Just create a new taxonomy called Chemical Dictionary. Taxonomies already have:

  • a title
  • a description field

You just need to add a custom field for the image(s) using the Field UI. You could easily import these things from CSV or a remote database table using the Migrate module if your comfortable with code.

Taxonomy terms are stored in a database table by default in drupal, they are already entities.

While typically used for tagging things, you dont have to use it that way.

  • I have edited my question to provide more information that show why terms would be problematic. – Artur Jan 13 '13 at 12:52

I would go with a database table. This would allow you to easily integrate it with the views module too. The hook you want to do it is hook_schema. http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_schema/7

  • I'm not sure I will have to provide View-based browsing of the symbols, but this is a very good point to have in mind. Thanks! – Artur Jan 13 '13 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.