Keyword

articles ordering by extra fields date value

  • Jerzy Z Gierzy
  • Jerzy Z Gierzy's Avatar Topic Author
  • Offline
  • New Member
More
7 years 6 months ago #158610 by Jerzy Z Gierzy
articles ordering by extra fields date value was created by Jerzy Z Gierzy
Hello
We use k2 items with various extra fields, some of which are date values ( such as event start or event end). I'm trying to alter the display of k2 items so they get sorted by k2 extra field date value, so item which
$item->extraFields->EVENT_END_ALIAS->value < $dateNow
display at the bottom of category .
I think i should create two arrays - one for regular items and one for archive items and then display one and the other into the template (with some additional 'archive' class).
Can this be acomplished with K2 at all?
K2 date fields values are already formated when they reach the template. How can i get an easy to parse date format?
At what point in the code is the best place for item sorting?

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 6 months ago #158616 by Krikor Boghossian
Replied by Krikor Boghossian on topic articles ordering by extra fields date value
Hello,

You will need a 3rd party extension in order to achieve this.
However, I cannot recall a specific one.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

More
7 years 6 months ago #158625 by Paul Adamson
Replied by Paul Adamson on topic articles ordering by extra fields date value
Hi, I have written a K2 plugin to implement this and I'm looking for testers. If you like, contact me at This email address is being protected from spambots. You need JavaScript enabled to view it. and we can discuss.

Sorry if I've broken any forum rules.

Cheers
Paul

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 6 months ago #158630 by Krikor Boghossian
Replied by Krikor Boghossian on topic articles ordering by extra fields date value
Not at Paul!
Kudos for your extension :)

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • Jerzy Z Gierzy
  • Jerzy Z Gierzy's Avatar Topic Author
  • Offline
  • New Member
More
7 years 6 months ago #158647 by Jerzy Z Gierzy
Replied by Jerzy Z Gierzy on topic articles ordering by extra fields date value
I got this working as desired.
I edited /components/com_k2/models/itemlist.php.
At the end of getData function i decided to sort items as soon as they come from the database.
Starting on line 361:
$rows = $db->loadObjectList();
		$dateNow = date('Y-m-d');
		$futureItems = array();
		$archiveItems = array();
		foreach($rows as $row){
			$extraFieldsById = array();
			$extraFieldsDecoded = json_decode($row->extra_fields, true);
			foreach ($extraFieldsDecoded as $field){
				$extraFieldsById[$field['id']] = $field['value'];	
			}
//in our case extra field with end date has id=33
			$row->{'dateValueFromEF'} = $extraFieldsById[33];
			if($extraFieldsById[33] >= $dateNow){
				$futureItems[]= $row;
			} else {
				$archiveItems[] = $row;
			}
			
		}
		usort($futureItems, function($a, $b){
			return strcmp($a->dateValueFromEF, $b->dateValueFromEF);
		});
		usort($archiveItems, function($a, $b){
			return strcmp($a->dateValueFromEF, $b->dateValueFromEF);
		});
		$rows = array_merge($futureItems, $archiveItems);
		return $rows;

the code still has to be improved to consider various categories and items without end date extra field, but hopefully its a starting point for someone else with similar situation.

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
7 years 6 months ago #158656 by Krikor Boghossian
Replied by Krikor Boghossian on topic articles ordering by extra fields date value
You should always keep a backup of the model, since it will be replaced when you update K2.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum