Repository: statamic/eloquent-driver (php)

Issue:
An orderable dated collection can't be custom sorted
When you have a dated orderable collection you can't custom sort it. Well you can in the UI but the order won't persist. 

Was talking about this on Discord and it turns out @ryanmitchell already made a PR.

Pull request description:
Update entry order when saving a tree and collections are stored in files
Fixes #139

This adds an event listener that does the exact same thing as the core's UpdateStructuredEntryOrder listener.

It'll update the `order` column of entries when you save a tree.

It will only do it if entries are configured to be in Eloquent, and Collections are configured to not be.

Patch:
diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php
index d7b05ee..becea3f 100644
--- a/src/ServiceProvider.php
+++ b/src/ServiceProvider.php
@@ -21,6 +21,7 @@ use Statamic\Eloquent\Entries\EntryQueryBuilder;
 use Statamic\Eloquent\Entries\EntryRepository;
 use Statamic\Eloquent\Forms\FormRepository;
 use Statamic\Eloquent\Globals\GlobalRepository;
+use Statamic\Eloquent\Listeners\UpdateStructuredEntryOrder;
 use Statamic\Eloquent\Revisions\RevisionRepository;
 use Statamic\Eloquent\Structures\CollectionTreeRepository;
 use Statamic\Eloquent\Structures\NavigationRepository;
@@ -28,6 +29,7 @@ use Statamic\Eloquent\Structures\NavTreeRepository;
 use Statamic\Eloquent\Taxonomies\TaxonomyRepository;
 use Statamic\Eloquent\Taxonomies\TermQueryBuilder;
 use Statamic\Eloquent\Taxonomies\TermRepository;
+use Statamic\Events\CollectionTreeSaved;
 use Statamic\Providers\AddonServiceProvider;
 use Statamic\Statamic;
 
@@ -41,6 +43,12 @@ class ServiceProvider extends AddonServiceProvider
         \Statamic\Eloquent\Updates\AddOrderToEntriesTable::class,
     ];
 
+    protected $listen = [
+        CollectionTreeSaved::class => [
+            UpdateStructuredEntryOrder::class,
+        ],
+    ];
+
     public function boot()
     {
         parent::boot();


Test patch:
diff --git a/src/Listeners/UpdateStructuredEntryOrder.php b/src/Listeners/UpdateStructuredEntryOrder.php
new file mode 100644
index 0000000..65c2d55
--- /dev/null
+++ b/src/Listeners/UpdateStructuredEntryOrder.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Statamic\Eloquent\Listeners;
+
+use Statamic\Eloquent\Collections\CollectionRepository;
+use Statamic\Events\CollectionTreeSaved;
+
+class UpdateStructuredEntryOrder
+{
+    public function handle(CollectionTreeSaved $event)
+    {
+        $tree = $event->tree;
+        $collection = $tree->collection();
+
+        if (config('statamic.eloquent-driver.entries.driver', 'file') !== 'eloquent') {
+            return;
+        }
+
+        if (config('statamic.eloquent-driver.collections.driver') === 'eloquent') {
+            // If the collections are configured to use Eloquent, then the entry
+            // order will be updated through the regular event/listener flow.
+            return;
+        }
+
+        $diff = $tree->diff();
+
+        $ids = array_merge($diff->moved(), $diff->added());
+
+        if (empty($ids)) {
+            return;
+        }
+
+        app(CollectionRepository::class)->updateEntryOrder($collection, $ids);
+    }
+}