Forum
Default sort on Related Content
I'm trying to figure out how to have the output of the related collection output in the order in which they appear in the control panel.
wheel_json.html (template/partial code) - I've tried with and without sort="true".
<perch:related id="productFeatures" collection="products" sort="true">
<perch:before>,"productFeatures":[</perch:before>
{"productReference":"<perch:content id="heading" type="text" />","productOverview":<perch:content id="abstract" type="textarea_json_encoded" html="true" markdown="true" editor="simplemde" required="false" imagewidth="800" />,"imageLink":"<perch:content type="image" output="filename" id="wholegoodImage" label="Hero Image" suppress="false" bucket="products" width="400" height="400" crop="true"/>"}
<perch:if exists="perch_item_last"><perch:else/>,</perch:if>
<perch:after>]</perch:after>
</perch:related>
and my wheel-json.php file (partial)
perch_collection('wheel', [
'category' => $myPath,
'template' => "wheel_json.html",
'filter' => 'active',
'match' => 'neq'
]);
If you're on Runway 3 you can just drag and drop reorder them.
That's the odd thing. I've sorted them
In Control Panel
As Rendered
Perch Runway: 3.0.9, PHP: 7.0.22, MySQL: 5.6.23, with PDO
If that matters any.
The order is good in the Control Panel and it stays that way (doesn't resort after saving or anything like that).
Does it make a difference if you don't filter?
No difference taking out the filter.
That should be all that's required - I don't see why that wouldn't work.
Is there a way to have the "<perch:related>" sort by a particular database column. If so, is there a default sort order column? I'm looking at having to query the database directly - which I not looking forward to since I have 3 different collections that I need to have related items sorting properly on.
I did a quick test with two barebones test collections - one with a related area referencing the other.
https://youtu.be/f0RZGO6JyyA
CONTAINER-Collection (named testwithrelated) CONTENTS-Collection (named test)
Template for CONTAINER
For CONTENTS, I just used the standard Article template.
From what I can determine, the order in which the related items (CONTENTS) are displayed in (CONTAINER) are governed by the order in which they are created in (CONTENTS).
For example, in CONTENTS I created two items "TESTb" and "TESTa" (created in that order). In CONTAINER, I add them as "TESTa" and "TESTb". When i render the collection, the order is "TESTb" then "TESTa". Dragging and dropping a new order has no effect either.
I looked into the database itself, and I can see that the CONTENTS references are in the JSON field in the correct order I selected (both as entered and when drag/drop in control panel). My speculation is that when the system grabs the data from the JSON file referencing the CONTENTS, it is sorting it by the ID (which should increment with each new item) - instead of keeping the order in which they appear.
What's the
sort="true"
doing?It did nothing.
I ended up querying the database directly and generated my pages that way. After much effort, I was able to get the output I needed. Luckily this is a project where I'm simply outputting some JSON files for an iPad application and doesn't need to be super quick.
Did you decide against using the dedicated API for this?
I reverted to what I knew, which is PHP and Sql queries. Deadline loomed. I'm just glad everything was put into the database correctly.