I have an issue with the new comment tag always displays for answers when viewing the questions with associated answers page. I found that when one views a question and answers page the history table is updated only for the answers_question type, and not for the associated answers_answer type.

I added this code to answers module, answers_node_view()

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', $node->nid)
          ->execute()
          ->fetchAssoc();
        foreach($result as $row) {
          db_merge('history')
            ->key(array(
              'uid' => $user->uid,
              'nid' => $result,
            ))
            ->fields(array('timestamp' => REQUEST_TIME))
            ->execute();
        }
      }

It updates the history table for the answers_answer type correctly, yet the new comment marker now does not display at all. I suspect that I am on the right track, but that the history table should be updated later in the request.

Using hook_exit() 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();
      }
    }
  }
}

Comments

willowdigit’s picture

Issue summary: View changes
willowdigit’s picture

Issue summary: View changes

Is hook_exit the right way to do this?

chipcleary’s picture

Sorry for the long delay.

It looks like there was a helpful discussion of this on drupal.stackexchange.

http://drupal.stackexchange.com/questions/126955/what-hook-to-use-to-upd...

That uses hook_exit and a refined version of getting the same effect as above. Unless you suggest otherwise, I'll test that refinement to the module.

Thanks for the work on this!

  • chipcleary committed cc0047d on 7.x-4.x
    Issue #2323139 by WillowDigit, chipcleary: New Comment Tag on Answers...
chipcleary’s picture

Status: Active » Needs review
StatusFileSize
new2.34 KB

Hi thanks for this.

I've taken the refined version of what you've done from here:

http://drupal.stackexchange.com/questions/126955/what-hook-to-use-to-upd...

Plus I found I needed to eliminate a bug in that I used module_load_include in a global context. Needed to fix that after I implemented hook_exit. (Though still not sure *why* implementing hook_exit, even if empty, triggered a problem).

Patch attached and committed to 7.x-4.x-dev.

bluegeek9’s picture

Assigned: Unassigned » bluegeek9
Status: Needs review » Active

I removed answer_exit 7.x-4.x because it was causing issues. hook_exit gets called on every page, even cached pages and should be avoid if possible. I am looking for another solution to fix the new tags.

bluegeek9’s picture

I implemented hook_views_post_render to update the New tag.

/**
 * 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);
      }
    }
  }

}

  • bluegeek9 committed 0e97aa6 on 7.x-4.x
    Issue #2323139 by bluegeek9: New Comment Tag on Answers incorrect due to...
bluegeek9’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.