0

I wrote a module to store taxonomy page details to DB. My requirement is to store tid, term name, vid and UNIX timestamp to DB table on every load of taxonomy term page. Here is my code:

function reporting_api_taxonomy_term_load($terms) {
  $tid = $terms[arg(2)]->tid;
  $vid = $terms[arg(2)]->vid;
  $term = taxonomy_term_load($tid);
  $name = $term->name;
  db_insert('reporting_api')
    ->fields(array(
      'tid' => $tid,
      'name' => $name,
      'vid' => $vid,
      'timestamp' => REQUEST_TIME,
    ))
    ->execute();
}

But it creates unwanted rows with timestamp only. Can any one help to fix this. Is hook_taxonomy_term_load is the correct hook.

1

1 Answer 1

2

Taxonomy terms can also be loaded when you're not on a taxonomy page, so arg(2) will be empty, hence empty records.

To fix:

$term = menu_get_object('taxonomy_term', 2);
if ($term) {
  db_insert('reporting_api')
    ->fields(array(
      'tid' => $term->tid,
      'name' => $term->name,
      'vid' => $term->vid,
      'timestamp' => REQUEST_TIME,
    ))
    ->execute();
}
2
  • Thanks clive. I tried the modified code. But <?php function custom_rep_taxonomy_term_load($terms) { $term = menu_get_object('taxonomy_term', 2); echo '<pre>'; print_r($term); exit; giving error Fatal error: Maximum execution time of 30 seconds exceeded in /includes/database/log.inc on line 0. I am using D7 with oracle.
    – drup
    Jul 16, 2015 at 9:04
  • i rewrite my code. function custom_rep_taxonomy_term_view($term, $view_mode, $langcode) { if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2)) ) { $terms = taxonomy_term_load(arg(2)); db_insert('custom_rep') ->fields(array( 'tid' => $terms->tid, 'name' => $terms->name, 'vid' => $terms->vid, 'timestamp' => REQUEST_TIME, )) ->execute(); } } Works fine
    – drup
    Jul 16, 2015 at 10:42

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.