Download & Extend

Add jump to last post to topic list view

Project:Advanced Forum
Version:6.x-2.x-dev
Component:Views
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

The topic list view needs a little icon that will jump to the last post in a topic. I spent some time adding this in the style itself but ran into a problem because the last post column is a compound field. Will likely need to make a views field just for this so it can be added in the right spot in views itself.

Michelle

Comments

#1

hi Michelle - any progress on this?

#2

Nope. Haven't had much time for my modules in August. Working on getting the bugs out first before adding new features so I can release another alpha.

Michelle

#3

no sweat - we'll either put this off until version 1.1 of our site, or I'll put together some kind of ugly hack to make it work in the interim.

thanks!

#4

Category:task» feature request

#5

+1 I join request. It really would be useful!

#6

May be FIRST unread could work better?
http://drupal.org/node/999196

#7

There's already a link to the first unread. This is specifically for jumping to the last post in a topic, which is useful if you aren't logged in and are trying to follow a long discussion over multiple visits. Jumping to the last post and scrolling up a bit is easier than starting at the top and going down.

Michelle

#8

Very interested in this myself as it's a standard forum feature currently missing in AF.

Just in case this moves forward, be aware of the pagination issue for nodecomments I raised http://drupal.org/node/1402522

I may have to do some heavy lifting in the meantime to address this for my own use case.

#9

Okay, I have this working by adding a new views field handler based on the existing one in AF for the topic pager. It means some additions to your generic MYMODULE.module, a new hander file and some changes to the existing advanced_forum_topic_list view configuration. No hacks/AF/core modifications required. Works fine with multiple pages too (but do see note above in #8).

I'm not going to explain how, but just go through what I did. I don't know if it's 100%, but it works for the use case I have. More than happy for it to end up in AF if Michelle (or whoever holds that golden keyboard these day) wants it there.

It includes some options you can use to decide what the link should be: Field label / short teaser / short teaser + full teaser title. I didn't go as far as adding a fields for "how short", so that's hard-coded.

First, here are the additional functions required for MYMODULE.module

/**
* Implementation of hook_views_api().
*/
function MYMODULE_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'MYMODULE'),
  );
}

/**
* Implementation of hook_views_data_alter().
*/
function MYMODULE_views_data_alter(&$data) {
  // Last comment link.
  $data['node']['last_post'] = array(
    'title' => t('Last topic post link'),
    'help' => t('Links to the last topic post in Advanced Forum.'),
    'field' => array(
      'handler' => 'MYMODULE_handler_field_node_last_post',
    ),
  );
}

/**
* Implementation of hook_views_handlers() to register all of the basic handlers
* views uses.
*/
function MYMODULE_views_handlers() {
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'MYMODULE'),
    ),
    'handlers' => array(
      'MYMODULE_handler_field_node_last_post' => array(
        'parent' => 'views_handler_field',
      ),
    ),
  );
}

/**
* Implementation of hook_theme().
*/
function MYMODULE_theme(&$data) {

  $items['MYMODULE_last_post'] = array(
    'arguments' => array(
       'pagecount' => NULL,
       'topic' => NULL,
    )
  );

  return $items;

}

/**
* Helper function to give us a short bit of text for the link.
* node_teaser() doesn't seem to work with small sizes.
*/
function MYMODULE_short_teaser($text) {

  $length = 40;

  if ( strlen($text) > $length ){
    $text = wordwrap($text, $length,"\n" ,TRUE);
    $text = substr($text, 0, strpos($text, "\n"));
    $text.='…';
  }

  return $text;

}

/**
* Theme function to return last post link.
*
* @param $options
*   Array from views and includes:
*   ['label']
*   ['link_text'] = label/teaser/teasertitle
* @param $topic
*   Topic object to create a pager for.
*   ->nid
*   ->type
*   ->num_comments
* @return
*   Assembled link wrapped in div for further styling.
*/
function theme_MYMODULE_last_post($options, $topic) {
  // Return nothing if no replies.
  $comment_count = $topic->num_comments;
  if ( ! $comment_count ) {
    return '';
  }

  $comments_per_page = _comment_get_display_setting('comments_per_page',$topic);
  $last_page = ceil($comment_count / $comments_per_page) - 1;
  $query = ($last_page > 0) ? "page=$last_page" : '';
  $last_comment_id = advanced_forum_last_post_in_topic($topic->nid);

  // See what text we want for the link.
  switch ($options['link_text']) {
    case 'label':
      $text = $options['label'];
      $title = '';
      break;
    case 'teaser':
      $comment = node_load($last_comment_id);
      $text = MYMODULE_short_teaser($comment->teaser);
      $title = '';
      break;
    case 'teasertitle':
      $comment = node_load($last_comment_id);
      $text = MYMODULE_short_teaser($comment->teaser);
      $title = $comment->teaser;
      break;
  }

  $link_options = array(
    'query'    => $query,
    'fragment' => 'comment-'.$last_comment_id,
  );
  $link = url('node/'.$topic->nid, $link_options);

  return '<div class="forum-last-post"><a href="'.$link.'" title="'.$title.'">'.$text.'</a></div>';

}

Then you need the handler, which will live in the same root directory as MYMODULE.module and is called

MYMODULE_handler_field_node_last_post.inc

<?php

/**
* @file
* Field handler to display the last post link.
*/

class MYMODULE_handler_field_node_last_post extends views_handler_field{
  function construct() {
    parent::construct();
    $this->additional_fields = array('nid' => 'nid', 'type' => 'type');
  }

  function option_definition() {
    $options = parent::option_definition();

    $options['link_text'] = array('default' => 'label');

    return $options;
  }

  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $form['link_text'] = array(
      '#type' => 'radios',
      '#title' => t('Text to use for link'),
      '#description' => t('Select the nature of the text for the last post link.'),
      '#options' => array(
        'label' => t('Field label'),
        'teaser' => t('Short teaser'),
        'teasertitle' => t('Short teaser and full teaser title'),
      ),
      '#default_value' => $this->options['link_text'],

    );
  }

  function query() {
    $this->ensure_my_table();
    $this->add_additional_fields();
    $this->field_alias = $this->table . '_' . $this->field;
  }

  function render($values) {
    // Make a fake topic object with the information that the core one has.
    $topic = new stdClass();
    $topic->nid = $values->nid;
    $topic->type = $values->node_type;
    $topic->num_comments = $values->node_comment_statistics_comment_count;

    return theme('MYMODULE_last_post', $this->options, $topic);
  }
}

Now you have all the files, it's probably worth clearing the cache! I seem to doing this all the time during dev.

Then you'll need to tweak that view
http://example.com/admin/build/views/edit/advanced_forum_topic_list

First, add a new field. Click the +, filter Groups by Node and you'll see a new "Node: Last topic post link" item. Check that and Add. You can then configure that field, but by default you'll see the link on your topic list from "Last topic post link". Click Update.

You'll need to re-order the fields, so this new field comes before "Node: Last comment author" and "Node: Last comment time". Click the re-order button next to the + next to Fields and drag it up.

Then you can restyle the output. Click on the gear next to Style: Forum topic list. Then in the Column drop down next to "Last topic post link", select "Last post"; that's the column it will display the field in.

That's about it for the basics. You can play around with the Style option about, to make any field in that view a little different and also use CSS to works its magic too.

I thought this was going to be easy until I worked out that I was going to have to get my hands dirty with handlers, but it only took a few hours and now I'm confident I can do something similar again if I need to, so it's always worth trying to understand a little of the code above, rather than just cut'n'paste. All part of the rich Drupal experience. Good luck.

#10

Here's a blurry screen shot to show the new link to last post. Note, nothing is shown if there are no replies.

AttachmentSize
af_lastpost.jpg 27.4 KB
nobody click here