How to order articles by field value Topic is solved

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
GillianBPP
Joomla! Explorer
Joomla! Explorer
Posts: 396
Joined: Wed Oct 01, 2014 10:25 am

How to order articles by field value

Post by GillianBPP » Wed Mar 08, 2023 11:07 am

I have created an event diary that only uses custom fields for each article to enable a standard display format (location, date, time, etc).

(Hopefully) I will not be adding these events myself but will have a volunteer from the site owner's organisation to do this. There is a field for date and time which will display in the format yyyy-mm-dd hh:mm and I would like to order the articles using that field. That would then ensure that the events are displayed in the order in which they will take place. At the moment, I have set up some dummy articles to demonstrate this new feature to the site owner but I have added the date and time to the article title, e.g. "yyyy mm dd hh mm event name", so that the articles are ordered by title. However, I can pretty much guarantee that the volunteer editors will probably forget to do this and just add the event name, so I'd like to automate it as much as possible.

Is this possible?
Last edited by toivo on Thu Mar 09, 2023 4:05 am, edited 1 time in total.
Reason: mod note: moved from 4.x Administration

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17487
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: How to order articles by field value

Post by toivo » Thu Mar 09, 2023 4:43 am

Possible but it requires a bit of development. The class ArticlesModel can be extended so that the protected function ArticlesModel::getListQuery can be overridden, in other words, modified without altering the Joomla core. The purpose of the modification is to include the custom field in the SQL query that the category list is based on.

The technique how to override a function by extending its class is discussed in the 2021 forum topic extension override in joomla 4.

The proof of concept (POC) uses a custom field MyDate as the sort argument for the Category List. A modified version of components/com_content/src/Model/ArticlesModel.php of Joomla 4.2.8 is attached.

The modified and added lines are commented with today's date // 20230309, for example:

Code: Select all

// 20230309 start
					$db->quoteName('v.value', 'mydate'),
// 20230309 end	
This script is only an example for further testing and development of a proper plugin that extends the class ArticlesModel and overrides the method getListQuery().
You do not have the required permissions to view the files attached to this post.
Toivo Talikka, Global Moderator

GillianBPP
Joomla! Explorer
Joomla! Explorer
Posts: 396
Joined: Wed Oct 01, 2014 10:25 am

Re: How to order articles by field value

Post by GillianBPP » Tue Feb 13, 2024 6:34 pm

toivo wrote:
Thu Mar 09, 2023 4:43 am
Possible but it requires a bit of development.
Well, taking 11 months to respond to all your hard work is not good enough and I apologise profusely.

I'm now back to this, but using J5.

It works (thank you) when I put the code in the Joomla Core, replacing /components/com_content/src/Model/ArticlesModel.php. Obviously, that's not the right thing to do, but I'm not sure where this file should go - or should it have a new, unique name in the same location?

Whilst the articles are now sorted by the custom field date (hooray!) I would like to limit them to articles where the custom field date is in the future. In my test site I have four articles, one of which has a custom field date in the past. I modified your code as below but nothing changed and I still see four articles.

Code: Select all

$query->where($db->quoteName('v.id') . ' =  "1"' AND $db->quoteName('v.value') . ' > now()');
How do I link the new ArticlesModel.php with my blog override? One must refer to the other in some way. You mentioned something about a plugin. This is not something I have ever done and creating a plugin in Joomla Docs only goes up to J4 and gives an example of an existing plugin for the 'search' function. (https://docs.joomla.org/J4.x:Creating_a ... for_Joomla

I have copied the basic J5 fields.xml file as a start and tweaked it:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<extension type="plugin" group="content" method="upgrade">
	<name>plg_content_events</name>
	<author>Joomla! Project</author>
	<creationDate>2024-02-13</creationDate>
	<copyright>(C) 2017 Open Source Matters, Inc.</copyright>
	<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
	<authorEmail>[email protected]</authorEmail>
	<authorUrl>www.joomla.org</authorUrl>
	<version>5.0.2</version>
	<description>Events Blog</description>
	<namespace path="src">Joomla\Plugin\Content\Events</namespace>
	<files>
		<folder plugin="events">services</folder>
		<folder>src</folder>
	</files>
	<config>
		<fields name="params">
			<fieldset name="basic">
			</fieldset>
		</fields>
	</config>
</extension>
But when I looked further down the Joomla docs article I must admit that I am now feeling rather overwhelmed and don't know what to do next.

GillianBPP
Joomla! Explorer
Joomla! Explorer
Posts: 396
Joined: Wed Oct 01, 2014 10:25 am

Re: How to order articles by field value

Post by GillianBPP » Fri Feb 16, 2024 9:34 am

UPDATE: Yesterday I discovered that Nicholas and Crystal Dionysopoulos have already written a plugin to sort by field. See https://github.com/nikosdion/sortbyfield
It does exactly what I asked for in my original post.

I'm still working on the pre-filtering by <today's date.


Post Reply

Return to “Joomla! 4.x Coding”