Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Sort/Order flex-objects by one of the objects’ properties?

Solved by Clive Beckett View solution

Started by Clive Beckett 6 years ago · 1 replies · 806 views
6 years ago

I am really struggling to make the plugin flex-objects work for a discography directory I need for a musician’s website.

Nevertheless, I am at the point where I can display a simple list from that directory in the frontend. But I cannot find a way to sort that list by one of the properties (release_year). No matter what I tried, the list will always be displayed in the order in which the entries have been stored in the YAML data file.

I have unsuccessfully tried to add options.order in several places in my /user/blueprints/flex-objects/discography.yaml because it does work for the backend listing:

YAML
config:
  admin:
    list:
      options:
        by: release_year
        dir: desc

I have also tried some Twig functions I had used with the old flex-directory plugin – but to no effect:
{% set collection = directory.collection.toArray()|sort_by_key(‘release_year’) %}
doesn’t do anything; just like
{% for object in collection.toArray()|sort_by_key(‘release_year’) if object.published %}

I can also drop .toArray() and nothing will change.

Then I found the Grav\Framework\Flex\FlexCollection->sort() and tried
{% set collection = directory.collection.sort({‘release_year’: 'DESC'}) %}
Which also did not do anything.

I’m completely out of my depths and very grateful about any help.

👍 2
6 years ago Solution

I found the answer. I had somehow used typographic single quotes (‘’) instead straight ones ('') for the property name. Alternatively it works without quotes as well.

(1) {% set collection = directory.collection.sort({'release_year': 'DESC'}) %}
(2) {% set collection = directory.collection.sort({release_year: 'DESC'}) %}

👍 2

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 46 1 week ago
Plugins · by Xavier, 4 weeks ago
2 55 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1181 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 49 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 73 2 months ago