3

I applied the superSort plugin function to a list of entries and it works perfectly:

{% set entries = craft.entries.section('sectionName').type('entryType').order('entryTitle')|supersort('natsort') %}

Trying to apply it to an entry page with this code to get the previous and next entry. It does not work:

{% set arrowParams = craft.entries.section('sectionName').type('entryType').order('entryTitle')|supersort ('natsort') %}
{% set prevEntry = entry.getPrev(arrowParams) %}{% set nextEntry = entry.getNext(arrowParams) %}
  {% if prevEntry %}
    <a href="{{ prevEntry.url }}">{{ prevEntry.title }}</a>
  {% endif %}

  {% if nextEntry %}
    <a href="{{ nextEntry.url }}">{{ nextEntry.title }}</a>
  {% endif %}

Thanks for helping!

3

It won't work with getPrev and getNext, as they only work with ElementCriteriaModels and not with an array of Entry models.

But that is what you get returned from the plugin. Supersort converts your craft.entries ElementCriteriaModel into an array of Entry models. This is necessary so that it can do the sorting.

The only workaround I know of would be to get the IDs of your supersorted entries and pass them to the ID param of a new ElementCriteriaModel.

{% set entries = craft.entries.section('sectionName').type('entryType') %}
{% set entriesSorted = entries|supersort('natsortAs', '{title}') %}

{% set entryIds = [] %}
{% for entry in entriesSorted %}
    {% set entryIds = entryIds|merge([entry.id]) %}
{% endfor %}

{% set arrowCriteria = craft.entries.id(entryIds).fixedOrder(true) %}

Now you have an ElementCriteriaModel arrowCriteria, prepared to get entries in the desired sort order. So now you can use arrowCriteria with your entry methods.

{% set prevEntry = entry.getPrev(arrowCriteria) %}
{% set nextEntry = entry.getNext(arrowCriteria) %}

{% if prevEntry %}
    <a href="{{ prevEntry.url }}">{{ prevEntry.title }}</a>
{% endif %}

{% if nextEntry %}
    <a href="{{ nextEntry.url }}">{{ nextEntry.title }}</a>
{% endif %}
2

On natsort...

When you're sorting a list of entries, the items being sorted are EntryModel objects. The natsort filter won't be very helpful in that case, because natsort is expecting string inputs.

Instead you can use the natsortAs filter as described in the Scraper docs:

{% entries | supersort('natsortAs', '{someStringField}') %}

This way, SuperSort knows exactly which value to sort by, and your results will be consistent.

On getPrev() and getNext()...

The getPrev() and getNext() methods on an EntryModel relate to its order within a specific ElementCriteriaModel, based on the parameters you provided when you fetched the entries.

If you want to get the "previous" or "next" item in a sorted array, you can utilize Twig's loop variable.

loop.first and loop.last tell you whether you're at the beginning or end of the loop, and loop.index0 + 1 and loop.index0 - 1 will refer to the indexes of the next and previous items in the loop.

(c.f. http://twig.sensiolabs.org/doc/tags/for.html)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.