1

What hook can I use to update the history table for my use case?

I am using the answers module. When viewing a question it appends a view of answers by format of view mode. The history table for the question type is updated, but not for the answer type. This has the effect that all comments on the answer are always tagged as new. I added code to the hook_node_view for the question type wherein I get all related answers and update the history table correctly for them. However, hook_node_view runs before hook_comment_view and now all comments do not get a tag at all. I probably need to alter the history table after hook_comment has run. Any suggestions?

1

You are looking for the node_tag_new() core function.

function node_tag_new($node) {
  global $user;
  if ($user->uid) {
    db_merge('history')->key(array(
      'uid' => $user->uid,
      'nid' => $node->nid,
    ))->fields(array('timestamp' => REQUEST_TIME))->execute();
  }
}

Normally you'd just pass the full node object to the function, but if you only have the nid, you can just pass a dummy object.

$node = new stdClass();
$node->nide = 1234;
node_tag_new($node);

Modifying the OP's own answer, this would be:

function answers_exit() {
  $router_item  = menu_get_object();
  if (isset($router_item) && $router_item->type == "answers_question") {
    if ($user->uid) {
      $results = db_select('field_data_answers_related_question', 'rq')
        ->fields('rq', array('entity_id'))
        ->condition('answers_related_question_target_id', $router_item->nid)
        ->execute();
      foreach($results as $row) {
        $node = new stdClass();
        $node->nid = $row->entity_id;
        node_tag_new($node);
      }
    }
  }
}

However you should probably be using EntityFieldQuery since it will handle all all the specifics about which tables/columns to use:

function answers_exit() {
  $router_item  = menu_get_object();
  if (isset($router_item) && $router_item->type == "answers_question") {
    $query = new EntityFieldQuery();
    $query->entityCondition('entity_type', 'node')
          ->entityCondition('bundle', 'answers')
          ->fieldCondition('answers_related_question', 'target_id', $router_item->nid);
    $result = $query->execute();
    if (isset($result['node'])) {
      foreach ($result['node'] as $answer) {
        node_tag_new($answer);
      }
    }
}
1
  • When I add this, I get the following error: Fatal error: Call to undefined function answers_question_locked_p() in answers\answers.module on line 178 The documentation for hook_exit says: If you implement this hook and see an error like 'Call to undefined function', it is likely that you are depending on the presence of a module which has not been loaded yet. It is not loaded because Drupal is still in bootstrap mode. Is there a way around this?
    – chipcleary
    Jan 1 '15 at 22:04
0

This seems to work:

function answers_exit() {
   $router_item  = menu_get_object();
  if (isset($router_item) && $router_item->type == "answers_question") {
    global $user;
    if ($user->uid) {
      $result = db_select('field_data_answers_related_question', 'rq')
        ->fields('rq', array('entity_id'))
        ->condition('answers_related_question_target_id', $router_item->nid)
        ->execute()
        ->fetchAssoc();
      foreach($result as $row) {
        db_merge('history')
          ->key(array(
            'uid' => $user->uid,
            'nid' => $result,
          ))
          ->fields(array('timestamp' => REQUEST_TIME))
          ->execute();
      }
    }
  }
}
0

hook_exit gets called on every call, even cached pages. This can cause problems with anonymous users and caching. hook_post_render only gets called when the view is rendered.

/**
* Implements hook_views_post_render().
*/
function answers_views_post_render(&$view, &$output, &$cache) {

  if ($view->name == 'question_answers') {
    if (count($view->result)) {
      foreach ($view->result as $object) {
        node_tag_new($object);
      }
    }
  }

}

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.