From e8e1bde28dd6634badeb3afa95b647a3034036c7 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 12 Nov 2024 16:44:15 +1300 Subject: [PATCH] API Make SubsiteXHRController a subsite of AdminController --- src/Controller/SubsiteXHRController.php | 41 +++++------ src/Extensions/LeftAndMainSubsites.php | 71 ++++++++++--------- .../SilverStripe/Admin/LeftAndMain_Menu.ss | 3 +- .../SubsiteXHRController_subsitelist.ss | 2 +- 4 files changed, 59 insertions(+), 58 deletions(-) diff --git a/src/Controller/SubsiteXHRController.php b/src/Controller/SubsiteXHRController.php index db344dcb..fccd61f4 100644 --- a/src/Controller/SubsiteXHRController.php +++ b/src/Controller/SubsiteXHRController.php @@ -2,20 +2,25 @@ namespace SilverStripe\Subsites\Controller; -use SilverStripe\Admin\LeftAndMain; +use SilverStripe\Admin\AdminController; +use SilverStripe\Control\HTTPRequest; +use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\PjaxResponseNegotiator; +use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\Security\Member; -use SilverStripe\Security\Permission; use SilverStripe\Subsites\Model\Subsite; /** - * Section-agnostic PJAX controller. + * Section-agnostic PJAX controller that renders the subsites swapper dropdown */ -class SubsiteXHRController extends LeftAndMain +class SubsiteXHRController extends AdminController { private static $url_segment = 'subsite_xhr'; - private static $ignore_menuitem = true; + public function index(HTTPRequest $request): HTTPResponse + { + return $this->getResponseNegotiator()->respond($request); + } /** * Relax the access permissions, so anyone who has access to any CMS subsite can access this controller. @@ -36,33 +41,21 @@ public function canView($member = null) } /** - * Allow access if user allowed into the CMS at all. + * Get a Pjax response negotiator for the subsite list */ - public function canAccess() - { - // Allow if any cms access is available - return Permission::check([ - 'CMS_ACCESS', // Supported by 3.1.14 and up - 'CMS_ACCESS_LeftAndMain' - ]); - } - public function getResponseNegotiator(): PjaxResponseNegotiator { - $negotiator = parent::getResponseNegotiator(); - - // Register a new callback - $negotiator->setCallback('SubsiteList', function () { - return $this->SubsiteList(); - }); - - return $negotiator; + return new PjaxResponseNegotiator([ + 'SubsiteList' => function () { + return $this->SubsiteList(); + }, + ]); } /** * Provide the list of available subsites as a cms-section-agnostic PJAX handler. */ - public function SubsiteList() + public function SubsiteList(): DBHTMLText { return $this->renderWith(['type' => 'Includes', SubsiteXHRController::class . '_subsitelist']); } diff --git a/src/Extensions/LeftAndMainSubsites.php b/src/Extensions/LeftAndMainSubsites.php index c6a1b74a..f519573c 100644 --- a/src/Extensions/LeftAndMainSubsites.php +++ b/src/Extensions/LeftAndMainSubsites.php @@ -23,12 +23,14 @@ use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\State\SubsiteState; use SilverStripe\Model\ArrayData; +use SilverStripe\Model\List\SS_List; use SilverStripe\View\Requirements; +use SilverStripe\View\TemplateGlobalProvider; /** * Decorator designed to add subsites support to LeftAndMain */ -class LeftAndMainSubsites extends Extension +class LeftAndMainSubsites extends Extension implements TemplateGlobalProvider { private static $allowed_actions = ['CopyToSubsite']; @@ -45,6 +47,42 @@ protected function onInit() Requirements::javascript('silverstripe/subsites:client/dist/js/LeftAndMain_Subsites.js'); } + public static function get_template_global_variables() + { + return [ + 'SubsiteSwitchList', + ]; + } + + /** + * Generates a list of subsites with the data needed to + * produce a dropdown site switcher + * @return SS_List + */ + public static function SubsiteSwitchList(): SS_List + { + $list = Subsite::all_accessible_sites(); + $currentSubsiteID = SubsiteState::singleton()->getSubsiteId(); + + if ($list == null || $list->count() == 1 && $list->first()->DefaultSite == true) { + return false; + } + + $output = ArrayList::create(); + + foreach ($list as $subsite) { + $currentState = $subsite->ID == $currentSubsiteID ? 'selected' : ''; + + $output->push(ArrayData::create([ + 'CurrentState' => $currentState, + 'ID' => $subsite->ID, + 'Title' => $subsite->Title, + ])); + } + + return $output; + } + /** * Set the title of the CMS tree */ @@ -139,37 +177,6 @@ public function Subsites() return Subsite::all_accessible_sites(); } - /** - * Generates a list of subsites with the data needed to - * produce a dropdown site switcher - * @return ArrayList - */ - public function ListSubsites() - { - $list = $this->Subsites(); - $currentSubsiteID = SubsiteState::singleton()->getSubsiteId(); - - if ($list == null || $list->count() == 1 && $list->first()->DefaultSite == true) { - return false; - } - - Requirements::javascript('silverstripe/subsites:client/dist/js/LeftAndMain_Subsites.js'); - - $output = ArrayList::create(); - - foreach ($list as $subsite) { - $currentState = $subsite->ID == $currentSubsiteID ? 'selected' : ''; - - $output->push(ArrayData::create([ - 'CurrentState' => $currentState, - 'ID' => $subsite->ID, - 'Title' => $subsite->Title, - ])); - } - - return $output; - } - public function alternateMenuDisplayCheck($controllerName) { if (!class_exists($controllerName ?? '')) { diff --git a/templates/SilverStripe/Admin/LeftAndMain_Menu.ss b/templates/SilverStripe/Admin/LeftAndMain_Menu.ss index 33be0ea1..0f7d6918 100644 --- a/templates/SilverStripe/Admin/LeftAndMain_Menu.ss +++ b/templates/SilverStripe/Admin/LeftAndMain_Menu.ss @@ -5,7 +5,8 @@ <% include SilverStripe\\Admin\\LeftAndMain_MenuLogo %> <% include SilverStripe\\Admin\\LeftAndMain_MenuStatus %> - <% if $ListSubsites.Count > 1 %> + <% if $SubsiteSwitchList.Count > 1 %> + <% require javascript('silverstripe/subsites:client/dist/js/LeftAndMain_Subsites.js') %> <% include SilverStripe\\Subsites\\Controller\\SubsiteXHRController_subsitelist %> <% end_if %> diff --git a/templates/SilverStripe/Subsites/Controller/Includes/SubsiteXHRController_subsitelist.ss b/templates/SilverStripe/Subsites/Controller/Includes/SubsiteXHRController_subsitelist.ss index 3b3960f6..8bf6693f 100644 --- a/templates/SilverStripe/Subsites/Controller/Includes/SubsiteXHRController_subsitelist.ss +++ b/templates/SilverStripe/Subsites/Controller/Includes/SubsiteXHRController_subsitelist.ss @@ -1,7 +1,7 @@