0

I am trying to do a query (using EntityFieldQuery) to get taxonomy terms created or modified in the last day.

for nodes, I use $query->propertyCondition('changed',$time_interval, '>='); but I can't find the corresponding property for a taxonomy term.

Any suggestion ? I need to be able to detect creation and modification of terms.

1 Answer 1

1

That data isn't stored for taxonomy terms, if you want to add it you'll need to write a small custom module to do so. You could look at implementing hook_entity_info_alter() or hook_field_extra_fields_alter() to actually add the extra property to the taxonomy term entity, but I haven't had much luck using that method before.

Instead I would define a table using hook_schema() in the .install file of your module:

function MYMODULE_schema() {
  $schema['taxonomy_term_extra'] = array(
    'fields' => array(
      'tid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'created' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE
      ),
      'modified' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE
      )
    ),
    'primary key' => array('tid')
  );

  return $schema;
}

Then in your module file implement the taxonomy CRUD hooks:

function MYMODULE_taxonomy_term_insert($term) {
  db_insert('taxonomy_term_extra')->fields(array(
    'tid' => $term->tid,
    'created' => REQUEST_TIME,
    'modified' => REQUEST_TIME
  ))
  ->execute();
}

function MYMODULE_taxonomy_term_update($term) {
  db_update('taxonomy_term_extra')
    ->fields(array('modified' => REQUEST_TIME))
    ->condition('tid', $term->tid)
    ->execute();
}

function MYMODULE_taxonomy_term_load($terms) {
  $result = db_query('SELECT * FROM taxonomy_term_extra WHERE tid IN (:tids)', array(':tids' => array_keys($terms)));
  foreach ($result as $record) {
    $terms[$record->tid]->created = $record->created;
    $terms[$record->tid]->modified = $record->modified;
  }
}

function MYMODULE_taxonomy_term_delete($term) {
  db_delete('taxonomy_term_extra')->condition('tid', $term->tid)->execute();
}

Now whenever you have a loaded taxonomy term you'll have access to $term->created and $term->modified. You can also use a normal db_query(), which is much faster than an EntityFieldQuery to get your filtered terms:

$args = array(':time' => strtotime('-1 day'));
$tids = db_query('SELECT tid FROM {taxonomy_term_extra} WHERE modified >= :time', $args)->fetchCol();

$terms = taxonomy_term_load_multiple($tids);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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