Programatically sorting entries of a structured collection by the CP order?

Hi everyone,

I'm looking on how to sort entries got from a structured collection like this one: Statamic\Facades\Collection::find('XYZ')->queryEntries()->get() according to the order I've arbitrary defined in control panel by drag'n dropping the entries. (Indeed I've enabled the mode "orderable" in the control panel for this collection).

I know there is a tree file created when I enable the mode "orderable" in the control panel (i.e.: the structure tree) and that there is a facade for using the structure contained in this tree. The tree file contains the ID of the collection's entries in the good order. However I didn't succeed in using this tree specifying the order of the entries to do what I want.

Sooner, I began to work on this problem at the collection's augmentation and collection's entries' augmentation steps (in particular in the context of Erin's statamic-blade plugin) but I think I was wrong. Now I actually think that working at this problematic at the augmentation step is not the solution. Instead I should rather work directly on the above SQL calls (queryEntries()->get().

Finally, Statamic\Facades\Collection::find('XYZ')->structure() returns only:

object(Statamic\Structures\CollectionStructure)#1974 (6) {
  ["title":protected]=>
  NULL
  ["handle":protected]=>
  string(6) "skills"
  ["trees":protected]=>
  array(0) {
  }
  ["collection":protected]=>
  NULL
  ["maxDepth":protected]=>
  NULL
  ["expectsRoot":protected]=>
  bool(false)
}

(weirdly as you can see "title" is NULL and "trees" is an empty array) (sorry I now see "handle" being not NULL nore empty, it's a good news. However I still don't understand why there is no tree in the array)

However, I would rather expect the entries in the arbitrary-cp-defined order (I mean, the entries indexed by the ID of the tree file, in the order defined by the tree file)

I've already looked at several functions related to structure , in the classes Collection , Entry, and Structure but didn't find any useful one.

Could you give me away, please, to programatically sort the entries got by the QueryBuilder::get() function (->queryEntries()->get()) of a structured collection, according to the order defined by a drag'n drop in the control panel of an orderable collection?

I thank you in advance. Best regards,

PS: my tree file located at trees/collections/XYZ.YML contains the following entries order:

tree:
  -
    entry: a764b668-353e-4fc7-99f2-5100562bcb7b
  -
    entry: f4cba8f3-9266-49dc-81d2-29f8592995db
  -
    entry: 7f2490fd-1ec3-44c5-a33f-ac693a67117e
>>>>>>> Unanswered <<<<<<<
1 Reply
2 Followers