From 3d94a0e3d002da4ba76e677ff175a5907e887699 Mon Sep 17 00:00:00 2001
From: Steve Boyd <emteknetnz@gmail.com>
Date: Wed, 13 Dec 2023 14:15:44 +1300
Subject: [PATCH] ENH Use SearchableMultiDropdownField to select Members

---
 code/Model/SiteTree.php | 25 ++++++++++---------------
 composer.json           |  4 ++--
 2 files changed, 12 insertions(+), 17 deletions(-)

diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php
index 42b516a931..a17a0006ec 100755
--- a/code/Model/SiteTree.php
+++ b/code/Model/SiteTree.php
@@ -38,6 +38,7 @@
 use SilverStripe\Forms\ListboxField;
 use SilverStripe\Forms\LiteralField;
 use SilverStripe\Forms\OptionsetField;
+use SilverStripe\Forms\SearchableMultiDropdownField;
 use SilverStripe\Forms\Tab;
 use SilverStripe\Forms\TabSet;
 use SilverStripe\Forms\TextareaField;
@@ -2256,16 +2257,6 @@ public function getSettingsFields()
         $viewAllGroupsMap = $mapFn(Permission::get_groups_by_permission(['SITETREE_VIEW_ALL', 'ADMIN']));
         $editAllGroupsMap = $mapFn(Permission::get_groups_by_permission(['SITETREE_EDIT_ALL', 'ADMIN']));
 
-        // $membersMap is limited to 100 records specifically so that it does not crash the front-end
-        // if the website has a large number of Members, which is likely to happen if the website also
-        // uses the Member table for non-cms public users
-        // This limit should be removed if the ListboxField front-end component is switched out or
-        // modified so that it does not load all users at once and instead uses XHR to fetch a subset
-        // of users based on what the user types in
-        $membersMap = Member::get()
-            ->limit(100)
-            ->map('ID', 'Name');
-
         $fields = new FieldList(
             $rootTab = new TabSet(
                 "Root",
@@ -2296,11 +2287,13 @@ public function getSettingsFields()
                         _t(__CLASS__.'.VIEWERGROUPS', "Viewer Groups"),
                         Group::class
                     ),
-                    $viewerMembersField = ListboxField::create(
+                    $viewerMembersField = SearchableMultiDropdownField::create(
                         "ViewerMembers",
                         _t(__CLASS__.'.VIEWERMEMBERS', "Viewer Users"),
-                        $membersMap,
-                    ),
+                        Member::get(),
+                    )
+                        ->setIsLazyLoaded(true)
+                        ->setUseSearchContext(true),
                     $editorsOptionsField = new OptionsetField(
                         "CanEditType",
                         _t(__CLASS__.'.EDITHEADER', "Who can edit this page?")
@@ -2310,11 +2303,13 @@ public function getSettingsFields()
                         _t(__CLASS__.'.EDITORGROUPS', "Editor Groups"),
                         Group::class
                     ),
-                    $editorMembersField = ListboxField::create(
+                    $editorMembersField = SearchableMultiDropdownField::create(
                         "EditorMembers",
                         _t(__CLASS__.'.EDITORMEMBERS', "Editor Users"),
-                        $membersMap
+                        Member::get()
                     )
+                        ->setIsLazyLoaded(true)
+                        ->setUseSearchContext(true)
                 )
             )
         );
diff --git a/composer.json b/composer.json
index 0fd74df259..77464ade39 100644
--- a/composer.json
+++ b/composer.json
@@ -20,9 +20,9 @@
     ],
     "require": {
         "php": "^8.1",
-        "silverstripe/admin": "^2",
+        "silverstripe/admin": "^2.2",
         "silverstripe/campaign-admin": "^2",
-        "silverstripe/framework": "^5.1",
+        "silverstripe/framework": "^5.2",
         "silverstripe/reports": "^5",
         "silverstripe/siteconfig": "^5",
         "silverstripe/versioned": "^2",