2

I currently have numerous Super Tables with date fields inside and I am trying to get all table items to list themselves together in date order depending on what the user inputs. I have tried using the Super Sort plugin but haven't had any luck.

Not sure if there is somewhere below where I have gone wrong or misunderstood the documentation. The plugin I am trying to use can be found here

Does anyone know if this is possible with Super Tables or Matrix fields?

The error I'm currently getting is:

Object of class Craft\ElementCriteriaModel could not be converted to string

protected function doDisplay(array $context, array $blocks = array())   
{
  echo $this->getAttribute($this->getAttribute(($context["object"] ?? $this->getContext($context, "object")), "tourDates", array()), "tourDate", array());
}

if (version_compare(PHP_VERSION, '7', '>=') && $code === 2 && 
strpos($message, 'should be compatible with') !== false)
{
    return;
}
parent::handleError($code, $message, $file, $line);

protected function displayWithErrorHandling(array $context, array 
$blocks = array())
{
    try {
        $this->doDisplay($context, $blocks);
    } catch (Twig_Error $e) {
        if (!$e->getSourceContext()) {
            $e->setSourceContext($this->getSourceContext());
        }

This is the code I current have:

{% set artists = craft.entries.section('artists') %}
{% set entries = artists | supersort('sortAs', '{ tourDates.tourDate }') %}

{% for entry in entries %}

    {% if entry.tourDates %}
        {% for row in entry.tourDates %}
            {{ row.tourDate|date("d/m/Y") }}
        {% endfor %}
    {% endif %}

{% endfor %}

I have also tried:

{% set entries = artists | supersort('sortAs', '{ tourDate }') %}

and

{% set entries = artists | supersort('sortAs', '{{ artists.tourDates.tourDate }}') %}
  • Never used Super Sort, but can you enabled devMode, then edit the original question and share the full stack trace leading up to that error? – Brad Bell Jun 8 '17 at 22:05
  • Added the rest of the error :) – Amy Jun 9 '17 at 7:11
  • 1
    Didn't a colleague of you ask this allready in this thread? craftcms.stackexchange.com/questions/20720/… – Tom De Smet Jun 9 '17 at 8:30
  • Sadly this solution doesn't solve my issue, as you can see above I have tried all 3 examples shown in the documentation. – Amy Jun 9 '17 at 9:09
  • What about my comment in that previous thread? Looping over your entries, fetching all data you need and adding it to a new array. Sorting that new array with SuperSort and than displaying all info by looping over the new array. – Tom De Smet Jun 9 '17 at 10:39
2

I did something like this as a workaround: Loop over entries, fetching all data you need and adding it to an array. Sorting that array with SuperSort and than displaying all info by looping over the array.

{% set tourDates = [] %}
{% set artists = craft.entries.section('artists') %}
{% for artist in artists %}

    {% if artist.tourDates %}
        {% for row in artist.tourDates %}
            {% set tourDates = tourDates|merge([ {
                'tourDate' : row.tourDate|date("d/m/Y")
            } ]) %}
        {% endfor %}
    {% endif %}

{% endfor %}

{% set tourDates = tourDates | supersort('sortAs', '{{ object.tourDate }}') %}

{% for tourDate in tourDates %}
    {{ tourDate.tourDate }}
{% endfor %}
  • Ah ok, I see where you're going with this. All this is doing at the moment is ordering them based on the day, so 1-31 regardless of what month it is, not the actual date itself. Do you by any chance know how to get it to display the order based on the date? – Amy Jun 9 '17 at 12:54
  • Maybe with {% tourDates = tourDates | supersort('sortAs', '{{ object.tourDate }}', SORT_LOCALE_STRING) %} ? Or you could write a Twig extension that uses usort() with a custom sort function? stackoverflow.com/questions/40462778/… – Tom De Smet Jun 9 '17 at 13:43
1

I used the preparse plugin to save a date value from a SuperTable field into a static field as 'yyyymmdd'. I could then sort my entries based on that static field.

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.