I have a content type in Drupal 8 with some fields referencing to taxonomy terms. I like to display these terms all together in one sorted array, so I merge the fields and sort them. But the result is always incompletely sorted, some terms are moved, but the others stay in the order they are saved. I tried different ways for sorting: sort function

{% for t in node.field_x|merge(node.field_y)|merge...|sort((a, b) => a.entity.name.value < b.entity.name.value ? -1 : (a.entity.name.value > b.entity.name.value ? 1 : 0)) %}
{% endfor %}

I tried it with strcmp

|sort((a, b) => strcmp(a.entity.name.value, b.entity.name.value)) 

and I tried it with a Twig extension function in a new module. It's always the same wrong result. So obviously I am wrong at some important point.

Can you help me?

  • A suggestion, with complex logic you can use process your logic in hook_preprocess. It's easy to process and easy to maintain. Remember twig is template engine. – Jonh Jun 17 at 14:20
  • Thanks @Jonh, I solved it with a theme_preprocess_node function. – krischanski Jun 17 at 20:16

Your Answer

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

Browse other questions tagged or ask your own question.