Forum posts in date order

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
Hi & Happy new year,

Build: NOP 4.1

I have had a few customers complain that the forum posts are displayed with the oldest ones at the top, so if they wish to see the latest they need to scroll all the way to the bottom of the page and click on the last page and scroll again. This provides poor user experience, so apart from editing the core source code is there an alternative?

Paul.
4 years ago
I found a solution to order the latest forum posts to the top, so the customer can see the replies as soon as they visit the forum topic, not sure if it is the best solution, but it works.

Nop.Web - BaseController - Topic


public virtual IActionResult Topic(int id, int pageNumber = 1)
{
    if (!_forumSettings.ForumsEnabled)
    {
        return RedirectToRoute("HomePage");
    }

    var forumTopic = _forumService.GetTopicById(id);
    if (forumTopic == null)
    {
        return RedirectToRoute("Boards");
    }

    var model = _forumModelFactory.PrepareForumTopicPageModel(forumTopic, pageNumber);

   var posts = model.ForumPostModels.Reverse().ToList();
    model.ForumPostModels = posts;


    //if no posts loaded, redirect to the first page
    if (!model.ForumPostModels.Any() && pageNumber > 1)
        return RedirectToRoute("TopicSlug", new { id = forumTopic.Id, slug = _forumService.GetTopicSeName(forumTopic) });

    //update view count
    forumTopic.Views += 1;
    _forumService.UpdateTopic(forumTopic);

    return View(model);
}
4 years ago
Your solution should work for the requirement but if you want to always use the sorting from newest to oldest
Or, you can change the ascSort argument in the GetAllPosts method in the forum service from true to false and apply the reversed sorting mechanism throughout your application:

This is the default implementation.

public virtual IPagedList<ForumPost> GetAllPosts(int forumTopicId = 0,
            int customerId = 0, string keywords = "",
            int pageIndex = 0, int pageSize = int.MaxValue)
        {
            return GetAllPosts(forumTopicId, customerId, keywords, true,
                pageIndex, pageSize);
        }


You can reverse the sorting by changing the method to
public virtual IPagedList<ForumPost> GetAllPosts(int forumTopicId = 0,
            int customerId = 0, string keywords = "",
            int pageIndex = 0, int pageSize = int.MaxValue)
        {
            return GetAllPosts(forumTopicId, customerId, keywords, false,
                pageIndex, pageSize);
        }
4 years ago
Thanks, I was just about to post an update as I found this method, changed the parameter to false and it works perfect. :-)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.