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<p>/**<br> * Implements hook_schema_alter().<br> */<br>function YOUR_MODULE_schema_alter(&$schema) {<br> $schema['taxonomy_term_data']['fields']['created'] = array(<br> 'description' => 'Unix timestamp when the Taxonomy term was created.',<br> 'type' => 'int',<br> 'not null' => TRUE,<br> 'default' => 0,<br> );<br> $schema['taxonomy_term_data']['fields']['changed'] = array(<br> 'description' => 'Unix timestamp when the Taxonomy term was last modified.',<br> 'type' => 'int',<br> 'not null' => TRUE,<br> 'default' => 0,<br> );<br>}</p><p>/**<br> * Implements hook_install().<br> */<br>function YOUR_MODULE_install() {<br> $schema = array();<br> YOUR_MODULE_schema_alter($schema);<br> // Altering the {taxonomy_term_data} table.<br> db_add_field('taxonomy_term_data', 'created', $schema['taxonomy_term_data']['fields']['created']);<br> db_add_field('taxonomy_term_data', 'changed', $schema['taxonomy_term_data']['fields']['changed']);<br>}</p><p>/**<br> * Implements hook_uninstall().<br> */<br>function YOUR_MODULE_uninstall() {<br> // Delete the additional columns from the {taxonomy_term_data} table.<br> db_drop_field('taxonomy_term_data', 'created');<br> db_drop_field('taxonomy_term_data', 'changed');<br>}</p>
YOUR_MODULE.module
<?php<p>/**<br> * @file<br> * Alter the Taxonomy table to add timestamps.<br> */</p><p>/**<br> * Implements hook_taxonomy_term_insert().<br> */<br>function YOUR_MODULE_taxonomy_term_insert($term) {<br> $term->created = REQUEST_TIME;<br> $term->changed = REQUEST_TIME;<br> drupal_write_record('taxonomy_term_data', $term, array('tid'));<br>}</p><p>/**<br> * Implements hook_taxonomy_term_update().<br> */<br>function YOUR_MODULE_taxonomy_term_update($term) {<br> $term->changed = REQUEST_TIME;<br> drupal_write_record('taxonomy_term_data', $term, array('tid'));<br>}</p>
For your convenience, I've provided this little custom module for download, see attachment below. Enjoy coding with Drupal !
Attachment: taxonomy_timestamp-7.x-dev.zipTags: drupaldrupal 7taxonomytermtimestampschema