From fd8a1e4b6aafda2ed462b59cd5d11c8989bd2451 Mon Sep 17 00:00:00 2001 From: Stephen Cameron Date: Fri, 4 Feb 2022 22:56:18 +0100 Subject: [PATCH] CMS backend improvements. WIP #4893 @1 --- src/app/Magic/Fields/CMSTemplate.php | 3 +++ src/app/Magic/Fields/Model.php | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/app/Magic/Fields/CMSTemplate.php b/src/app/Magic/Fields/CMSTemplate.php index 244e480..98cf450 100644 --- a/src/app/Magic/Fields/CMSTemplate.php +++ b/src/app/Magic/Fields/CMSTemplate.php @@ -22,6 +22,9 @@ class CMSTemplate extends SelectFromArray foreach ($templates as $name => $template) { $options[Str::snake($name)] = $template->getName(); } + + asort($options); // Alphabetically sort templates by their display names + $this->setAttribute('options', $options); } } diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index feec9cb..3af8c40 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -29,7 +29,10 @@ class Model extends Field $inst = new $modelClass(); $attr=$this->getAttribute('attribute'); - return $modelClass::orderBy($attr)->get()->pluck($attr, $inst->getPrimaryKey())->toArray(); + // Allow the attribute to be a custom accessor. + // For this to work, the query is done first, then the Collection object handles it + // Ref: https://stackoverflow.com/a/44475575 + return $modelClass::get()->sortBy($attr)->pluck($attr, $inst->getPrimaryKey())->toArray(); } public function getDatabaseType() -- 2.39.5