So I'm creating a page of downloadable documents and am using the apostrophe-files-widgets
to render out the documents uploaded under the Files admin bar item. This works fine, but when rendered on the page, they show up in order they were uploaded. I rather have them being in alpha order of the tag names.
I know there is apostrophe-pieces-orderings-bundle
, but that feels like a lot of overhead. Maybe I'm wrong...
Is there a way to access the individual files, look a their tags, and then group them by those tags. For simplicity sake, assume that each file only has ONE tag associated.
You're displaying all of the files, so you can go ahead and group on the tags right in the template:
{# in widget.html #}
{% set grouped = apos.utils.groupBy(data.widget._pieces, 'tags') %}
{% for tag, pieces in grouped %}
<h3>{{ tag }}</h3>
{% for piece in pieces %}
<h4>{{ piece.title }}</h4>
{% endfor %}
{% endfor %}
If you were building an apostrophe-pieces-pages
module subclass here, with pagination, we'd need to fix this at the database sort level, which would involve some interesting denormalization. But in this case we needn't worry about that.