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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

friolator’s picture

hi Michelle - any progress on this?

Michelle’s picture

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

friolator’s picture

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!

Michelle’s picture

Category: task » feature
snupy’s picture

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

__Sander__’s picture

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

Michelle’s picture

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

dunx’s picture

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.

dunx’s picture

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 to above Last comment author.

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.

dunx’s picture

FileSize
27.4 KB

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

Michelle’s picture

Status: Active » Needs review

Setting this to "needs review" though it's not actually a patch. If you are able to create a patch, that would make this a lot easier to review.

Thanks!

Michelle

Ingumsky’s picture

Thanks dunx. I'll try your solution.

Ingumsky’s picture

OK. I've finally tested it. Thank you dunx - it really works.
I tested it on my latest 6.x-2.x-dev install. I added lines needed to advanced_forum.module, /includes/theme.inc, /includes/views/advanced_forum.views.inc and created /includes/views/advanced_forum_handler_field_node_last_post.inc. The code was written by dunx in general but I made some improvements to it. First of all it seems to me that dunx uses node comment module and that's why some of the display options for the field didn't work for me at first. I rewrote his ...short_teaser function to make it possible to use its code with drupal built-in comments. And I changed display modes slightly by adding two more choices to its options list.
You can find my patch attached to this comment. If you want to roll it, place it to your custom modules directory and then type:
cd path/to/your/website/htdocs/directory/sites/all/modules
patch -p0 < advanced_forum_last_post.patch
Then clear your site cache and change the view as dunx mentioned in his #9 post.
Please review and test this code. Hopefully it will be added to the next advanced_forum release.

dunx’s picture

Thanks for patchifying Ingumsky. I'll have a play later as your tweaks sound sensible. Can I add "Drupal Developer" to my CV yet? :)

Ingumsky’s picture

Can I add "Drupal Developer" to my CV yet? :)

Yes, you most definitely can :)

mcdruid’s picture

Assigned: Unassigned » Ingumsky

Thank dunx and Ingumsky.

I'd like to get this into beta1 which we're hoping to release soon.

However, in order to do that we'd need to update the default view definition well as adding the new handler and the other stuff that's in the patch (which I've not read, I'm just assuming the view definition is not being patched as you mention updating the view through the UI).

Ingumsky - assigning this to you - let me me know if you need any help with updating the view (or anything else).

Ingumsky’s picture

OK. Thanks mate. I'll change default view in a day or two.