Drupal add timestamp to Taxonomy term data

I had another request to alter the default taxonomy_term_data table and adding the timestamps; created and changed just like in the node database table.

Here's a little snippet that will do the trick.

You need to create a custom module for this to work. Remember to replace YOUR_MODULE with your custom module name.

YOUR_MODULE.install

<?php

/**
* Implements hook_schema_alter().
*/
function YOUR_MODULE_schema_alter(&$schema) {
  $schema['taxonomy_term_data']['fields']['created'] = array(
    'description' => 'Unix timestamp when the Taxonomy term was created.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  );
  $schema['taxonomy_term_data']['fields']['changed'] = array(
    'description' => 'Unix timestamp when the Taxonomy term was last modified.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  );
}

/**
* Implements hook_install().
*/
function YOUR_MODULE_install() {
  $schema = array();
  YOUR_MODULE_schema_alter($schema);
  // Altering the {taxonomy_term_data} table.
  db_add_field('taxonomy_term_data', 'created', $schema['taxonomy_term_data']['fields']['created']);
  db_add_field('taxonomy_term_data', 'changed', $schema['taxonomy_term_data']['fields']['changed']);
}

/**
* Implements hook_uninstall().
*/
function YOUR_MODULE_uninstall() {
  // Delete the additional columns from the {taxonomy_term_data} table.
  db_drop_field('taxonomy_term_data', 'created');
  db_drop_field('taxonomy_term_data', 'changed');
}

YOUR_MODULE.module

<?php

/**
* @file
* Alter the Taxonomy table to add timestamps.
*/

/**
* Implements hook_taxonomy_term_insert().
*/
function YOUR_MODULE_taxonomy_term_insert($term) {
  $term->created = REQUEST_TIME;
  $term->changed = REQUEST_TIME;
  drupal_write_record('taxonomy_term_data', $term, array('tid'));
}

/**
* Implements hook_taxonomy_term_update().
*/
function YOUR_MODULE_taxonomy_term_update($term) {
  $term->changed = REQUEST_TIME;
  drupal_write_record('taxonomy_term_data', $term, array('tid'));
}

For your convenience, I've provided this little custom module for download, see attachment below. Enjoy coding with Drupal !

16 comments for 'Drupal add timestamp to Taxonomy term data'

dalin's picture

With your method there could be race conditions and some data could be lost:
1) Thread A saves a taxonomy term.
2) Thread B saves the same taxonomy term with different data.
3) Thread B has the drupal_write_record() in your custom hook complete first.
4) Thread A has the drupal_write_record() in your custom hook complete, overwriting the change made by thread B.

This can be avoided by using hook_taxonomy_term_presave() instead.

admin's picture

Hi Dalin,

Thank you for your feedback. Yes you're right under exceptional case that might happen on high concurrent user websites.

Simply overwrite hook_taxonomy_term_insert with presave to avoid the problem.

/**
* Implements hook_taxonomy_term_presave().
*/
function YOUR_MODULE_taxonomy_term_presave($term) {
  $term->created = REQUEST_TIME;
  $term->changed = REQUEST_TIME;
}
blazindrop's picture

Dalin, I'm curious why there's a race condition with taxonomy but not with users (user_save starts a db transaction while taxonomy_term_save does not). A link to an issue queue is totally cool :)

koko's picture

Thank you very much about your post. It's very easy to understand and useful for me !
In my problem. I'm not only want to add field in taxonomy_term_data table but also display it as a form field to user can add/edit it.
Please help me, I'm just a newbie

admin's picture

Hi Koko,

In your case, you don't even need any custom module to do the trick, going to the Taxonomy vocabulary > Manage Fields page. From here you can add/edit custom fields to your taxonomy terms.

Let me know if you need further help on this.

gem's picture

ok , i very enjoy and like after writing this post. thanks

Chris's picture

Hi,
thanks for the smart advice. Do you have build a module on drupal.org that others can add this functionality easily without coding?

If not I can do this for you and the Community.
Cheers
Chris

Marcos's picture

Thank you for the very useful code! Is adding Views support a simple add-on to this small module?

Xiukun.zhou's picture

/**
* Implements hook_taxonomy_term_presave().
*/
function YOUR_MODULE_taxonomy_term_presave($term) {
  if($term->tid) {
    $term->changed = REQUEST_TIME;
  } else {
    $term->created = REQUEST_TIME;
    $term->changed = REQUEST_TIME;
  }
}

Rahul Seth's picture

**
* Implements hook_install().
*/
function taxonomy_timestamp_install() {
  $schema = drupal_get_schema('taxonomy_term_data');
  // Altering the {taxonomy_term_data} table.
  db_add_field('taxonomy_term_data', 'created', $schema['taxonomy_term_data']['fields']['created']);
  db_add_field('taxonomy_term_data', 'changed', $schema['taxonomy_term_data']['fields']['changed']);
}

Add new comment