Is random order possible for module output?

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

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).

1 Like

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:
image

Out of interest are your items weighted? at all?

1 Like

face + palm

Yes @James, yes I did have weighting on those items. I removed it and now it works.

You wind the gold star :star:

Thank you. Thanks to all of you for your help :+1:

Yeah all good!
I remember chatter a little while ago that weighting has this weird authority on items in Treepl :slight_smile:

Glad its sorted!

1 Like