I have a hero slider on a side and the client wants the user to see a random slide every time the page loads. Is it possible to order the custom module items randomly?
I searched slack and summon the wonderful @Adam.Wilson from the past:
Adam (Logo Pogo) Jan 30th at 5:54 AM
Documentation Update
2 new āComponent Typesā articles added (completing the full series here) for the āAuthorā module and the āPageā module.
Authors are pretty standard/boring but the Page module is an interesting one and not available in the Component Manager in the admin.
41
8 replies
Hope/Hope Stewart web designs 6 months ago
@Adam (Logo Pogo) Very helpful. Thanks for all your effort in creating the documentation.Under ārandomā for āPageā module, it says āDisplays the available items in a random order.ā This is not correct. It randomly selects items to display if not all items are to be retrieved. The randomly selected items will then be displayed in the defined (or default) sort order.
Adam (Logo Pogo) 6 months ago
Thanks Hope. Yes, this is on my radar to investigate further and Iāll be sure to adjust the Docs accordingly.
Iāll try to look into it todayā¦
Adam (Logo Pogo) 6 months ago
Hey @Hope/Hope Stewart web designs So Iāve revisited the random
issue here and as itās currently implemented and documented isnāt really wrong as such - although I agree itās not 100% clear.
When you use random
it will indeed retrieve a random selection of items. If you limit to 1 item it will be a random item. If you limit to 3 items they will be random items. List ALL items and they will still be retrieved randomly. However, the problem is that sortBy
is always applied and will restore a logical order to the list.
BUTā¦
I have found an easy way around this. Simply set the sortBy
value to an unused field. That way the system has no criteria to use for sorting and therefore doesnāt sort the list.
In my test case, I tried the āauthorā field as that wasnāt being used as well as āitemcategoriesā, āitemtagsā and āenabledā.
āenabledā is probably the best one to use actually even though it is technically in use by the system, but the retrieved items will all be the same value of ātrueā.Iāve updated the docs with this note (does it makes sense?): (edited)
Pasted image at 2019-02-01, 11:14 PM
Hope/Hope Stewart web designs 6 months ago
@Adam (Logo Pogo) This sounded so promising but itās not working for me. The sort order keeps defaulting to the Weighting values. This is the liquid code Iām using:
{% component source: āMyCustomModuleNameā, layout: āListā, sortBy: āAuthorā, limit: ā100ā, random: ātrueā, type: āmoduleā %}
I get the same ordered result whether I use āenabledā or another unused field. Interestingly, if I add in sortOrder: āASCā, it ignores it and still sorts by DESC weighting values ā unless I specify to sort by Weighting.
When you tested this, did your items have weighting values?
Adam (Logo Pogo) 6 months ago
Yes, youāre right. It seems weighting will always override this. Although any items not weighted will still be random.
So in this case, Iād suggest making your own custom field for weighting and remove the system field. That way you can still sortBy your custom weight field where you need to as well as use this random technique where you need random listings. I think that should work.
Adam (Logo Pogo) 6 months ago
@Vlad / Treepl Team I think what we need here is another option for the sortBy
parameter like: sortBy: "none"
so that no sorting at all can be forced if needed when used with the random
option. Being able to sort randomly retrieved items is still useful though so the current implementation is relevant. I think we just need to be able to force randomness/disable sorting as well. Your thoughts?
Vlad / Treepl Team 6 months ago
Not really understand the case. Random function works before sorting and choose several random items from full list of items. Than than result list sorted by weighting first than by sortBy sortValue scope.
So what behavior do you expect to achieve?
Adam (Logo Pogo) 6 months ago
In the case where you want to render your items in a random order.
Letās say you just have 5 items in a module and you just want those listed in a random order all the time.
This is random āsorting/orderingā as opposed to random āretrievalā which is why I think we either need an additional ārandomā sort type or a sort type that disables sorting so the random option can remain randomised.
Does that make sense?
So the answer seems to be that we sort by an unused field like author, or enabled, and make sure the items donāt have weighting.
Hmmā¦ Iām not able to get this to work. Has anybody had success doing this?
Iāve tried sorting by a non populated field in the module insertion tag and that didnāt seem to change anything. I thought perhaps it was because I was using the collection object type, so I tried @Adam.Wilson previously suggested method for sorting collecitons [Bug] If module items have weighting then sorting by other fields doesn't work
That didnāt seem to work for me either. Hereās my code inside the collection layout (Iām sorting by an unpopulated field in this case as well):
<div class="swiper-container">
<div class="swiper-wrapper">
{% assign sorted = this.items | sort: 'Author' %}
{% for slide in sorted %}
{% if slide.ItemTags contains 'home' %}
<div class="swiper-slide">
<div class="hero__image" style="background-image: url('{{slide['Image']}}');"></div>
<img class="hero__image-placeholder" src="{{slide['Image']}}" style="opacity: 0;">
<div class="hero__title-wrapper">
<p class="hero__title"><span class="hero__supertitle">{{slide['SuperTitle']}}</span> <br/>{{slide['Description']}}</p>
</div>
</div>
{% endif %}
{% endfor %}
</div>
<div class="swiper-pagination"></div>
</div>
@Alex_B_Centrifuge I did get this to work. There are probably other ways but this is what Iām using:
{% component source: "MyCustomModuleName", layout: "List", filterBy: "weighting", filterValue: "0", sortBy: "Author", limit: "100", random: "true", type: "module" %}
Hi @Alex_B_Centrifuge. I think youād have to use the sortBy: "Author/enabled/..."
trick in the component tag so that it forces the system to NOT sort at the component level.
Using the Liquid sort trick wont change anything and the system will still default sort the randomly retrieved items.
Can you try that way again and post your code if still not working.
I donāt think the object type would be an issue, but havenāt tested that eitherā¦
Alternatively, many slider plug-ins have a randomise option in their settings. If the one youāre using doesnāt, itās probably not too much work to switch it for one that does (if all else fails).
Sorry to take the long way round on this one. I still havenāt figure this out.
Here is my module insertion:
{% component source: "Hero", layout: "Home Slider", random: "true", sortBy: "Author", object: "collection", collectionVariable: "Slides", type: "module" %}
And here is the layout that itās calling. I removed the sort by author from the layout.
<div id="swiper-container" class="swiper-container" >
<div class="swiper-wrapper">
{% assign sorted = this.items %}
{% for slide in sorted %}
{% if slide.ItemTags contains 'home' %}
<div class="swiper-slide">
<div class="hero__image" style="background-image: url('{{slide['Image']}}');"></div>
<img class="hero__image-placeholder" src="{{slide['Image']}}" style="opacity: 0;">
<div class="hero__title-wrapper animate-staggered-in-up">
<p class="hero__title"><span class="hero__supertitle">{{slide['SuperTitle']}}</span> <br/>{{slide['Description']}}</p>
</div>
</div>
{% endif %}
{% endfor %}
</div>
<div class="swiper-pagination"></div>
</div>
The order still does not seem to be random. When I reload the page itās always the same slide to start.
Iām using it for the hero slider on this site:
Out of interest are your items weighted? at all?
face + palm
Yes @James, yes I did have weighting on those items. I removed it and now it works.
You wind the gold star
Thank you. Thanks to all of you for your help
Yeah all good!
I remember chatter a little while ago that weighting has this weird authority on items in Treepl
Glad its sorted!