MG 2.1 Guide: How to add order by title (albums & images)

stan1226

Member
So I recently found out that XFMG does not support sorting order by title. I was shocked that this is a requested feature for years now without any real answer when we will get it.

I didn't know where to put this topic to, so I added it here

I just look into the XMFG files, tried a few things and found a very easy solution to add a order by title option to XFMG.

All you need is a simple textediot (I recommend notepad++) and knowledge how to upload the modified file to your server.

Always do a backup of your original files before modying them!

Modify the following files with the editor:
/src/addons/XFMG/Api/ControllerPlugin/MediaItem.php
Add the title value into the variables at line 47:
PHP:
        switch ($order)
        {
            case 'media_date':
            case 'comment_count':
            case 'rating_weighted':
            case 'reaction_score':
            case 'view_count':
            case 'title':
                $mediaFinder->order($order, $direction);
                return [$order, $direction];
        }

/src/addons/XFMG/Api/ControllerPlugin/Album.php
Same thing at line 37:
PHP:
        switch ($order)
        {
            case 'create_date':
            case 'media_count':
            case 'comment_count':
            case 'rating_weighted':
            case 'reaction_score':
            case 'view_count':
            case 'title':
                $albumFinder->order($order, $direction);
                return [$order, $direction];
        }

/src/addons/XFMG/ControllerPlugin/AbstractList.php
add the title value at the function at line 90. Don't forget to add the , behind 'view_count'
PHP:
    public function getAvailableSorts()
    {
        // maps [name of sort] => field in/relative to MediaItem entity
        return [
            'comment_count' => 'comment_count',
            'rating_weighted' => 'rating_weighted',
            'reaction_score' => 'reaction_score',
            'view_count' => 'view_count',
            'title' => 'title'
        ];
    }

Now we need to modify two template directly in your Xenforo Admin Template page.
xfmg_media_filters
Add the line
<xf:eek:ption value="title">{{ phrase('title') }}</xf:eek:ption>
to the filters at 35 and 43. The placement here defines at which position the title sort will appear in your sort list:
PHP:
                <xf:select name="order" value="{{ $filters.order ?: 'media_date' }}">
                    <xf:option value="media_date">{{ phrase('date') }}</xf:option>
                    <xf:option value="title">{{ phrase('title') }}</xf:option>
                    <xf:option value="comment_count">{{ phrase('xfmg_comments') }}</xf:option>
                    <xf:option value="rating_weighted">{{ phrase('rating') }}</xf:option>
                    <xf:option value="reaction_score">{{ phrase('reaction_score') }}</xf:option>
                    <xf:option value="view_count">{{ phrase('views') }}</xf:option>
                </xf:select>
            <xf:else />
                <xf:select name="order" value="{{ $filters.order ?: 'create_date' }}">
                    <xf:option value="create_date">{{ phrase('date') }}</xf:option>
                    <xf:option value="title">{{ phrase('title') }}</xf:option>
                    <xf:option value="media_count">{{ phrase('xfmg_media_count') }}</xf:option>
                    <xf:option value="comment_count">{{ phrase('xfmg_comments') }}</xf:option>
                    <xf:option value="rating_weighted">{{ phrase('rating') }}</xf:option>
                    <xf:option value="reaction_score">{{ phrase('reaction_score') }}</xf:option>
                    <xf:option value="view_count">{{ phrase('views') }}</xf:option>
                </xf:select>

xfmg_album_list_macros
Add a , to phrase('views') at line 23 and add a new line
'title': phrase('title')
PHP:
    <xf:set var="$sortOrders" value="{{ {
        'create_date': phrase('xfmg_create_date'),
        'media_count': phrase('xfmg_media_count'),
        'comment_count': phrase('xfmg_comments'),
        'rating_weighted': phrase('rating'),
        'reaction_score': phrase('reaction_score'),
        'view_count': phrase('views'),
        'title': phrase('title')
    } }}" />

And that's already it! Now you have successfully implemented a order by title function to your XFMG!

Here's a screenshot of the function working on my german forum. Since the addition is using the phrase('title') function, it should work in every XFMG language.xfmg_sortbytitle.jpg

Known problems:
Your forum will show an error in the backend, that you modified the 3 XFMG files.

I hope the XF might add this to the next version of XFMG. It's really just a really simple fix for a long requested feature
 
Last edited:
Back
Top Bottom