Fixed Custom forum sort options don't behave as expected

DragonByte Tech

Well-known member
Affected version
2.2.1
Based off of XF\Repository\Thread :: getDefaultThreadListSortOptions, it should be possible to add custom thread list sorting options by using something like this in the ForumType:
PHP:
    public function getThreadListSortOptions(Forum $forum, bool $forAdminConfig = false): array
    {
        $options = parent::getThreadListSortOptions($forum, $forAdminConfig);
        $options['vote_score'] = 'vote_score';
        $options['custom_display_name'] = 'vendor_prefix_thread_entity_column';

        return $options;
    }

However, this is not actually what happens. Instead, when choosing custom_display_name in the default forum sort, you will receive the following error message on the forum:
InvalidArgumentException: Unknown column custom_display_name on XF:Thread in src/XF/Mvc/Entity/Finder.php at line 1640

I don't think the stack trace is relevant here, because the bug is that XF\Finder\Thread does not resolve the default_sort_order back to the actual entity name.

Current code:
PHP:
    public function applyForumDefaultOrder(\XF\Entity\Forum $forum)
    {
        $this->setDefaultOrder($forum->default_sort_order, $forum->default_sort_direction);

        return $this;
    }

Suggested fix:
PHP:
    public function applyForumDefaultOrder(\XF\Entity\Forum $forum)
    {
        $sortOrders = $forum->TypeHandler->getThreadListSortOptions($forum);
        $this->setDefaultOrder($sortOrders[$forum->default_sort_order], $forum->default_sort_direction);

        return $this;
    }
I have confirmed that this will work if the default_sort_order value is different from the thread entity column, due to storage size restrictions on the default_sort_order column.
 
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.2.2).

Change log:
Properly sort columns for forum default sort orders
There may be a delay before changes are rolled out to the XenForo Community.
 
Top Bottom