Skip to content

Commit

Permalink
API Make SubsiteXHRController a subclass of AdminController
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 14, 2024
1 parent 2fee81a commit 3a401ed
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 42 deletions.
46 changes: 19 additions & 27 deletions src/Controller/SubsiteXHRController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

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\Dev\Deprecation;
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;
private static string $required_permission_codes = 'CMS_ACCESS';

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.
Expand All @@ -37,35 +43,21 @@ public function canView($member = null)
}

/**
* Allow access if user allowed into the CMS at all.
* @deprecated 3.4.0 Will be removed without equivalent functionality to replace it.
* Get a Pjax response negotiator for the subsite list
*/
public function canAccess()
{
Deprecation::noticeWithNoReplacment('3.4.0');
// 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']);
}
Expand Down
15 changes: 1 addition & 14 deletions src/Extensions/LeftAndMainSubsites.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;

Expand Down Expand Up @@ -69,8 +70,6 @@ public static function SubsiteSwitchList(): SS_List
return false;
}

Requirements::javascript('silverstripe/subsites:client/dist/js/LeftAndMain_Subsites.js');

$output = ArrayList::create();

foreach ($list as $subsite) {
Expand Down Expand Up @@ -180,18 +179,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<Subsite>
* @deprecated 3.4.0 Will be removed without equivalent functionality to replace it.
*/
public function ListSubsites()
{
Deprecation::notice('3.4.0', 'Use SubsiteSwitchList() instead.');
return static::SubsiteSwitchList();
}

public function alternateMenuDisplayCheck($controllerName)
{
if (!class_exists($controllerName ?? '')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Service/ThemeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ThemeResolver
* @param Subsite $site
* @return array
*/
public function getThemeList(Subsite $site)
public function getThemeList(Subsite $site): array
{
$themes = array_values(SSViewer::get_themes() ?? []);
$siteTheme = $site->Theme;
Expand Down
1 change: 1 addition & 0 deletions templates/SilverStripe/Admin/LeftAndMain_Menu.ss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<% include SilverStripe\\Admin\\LeftAndMain_MenuStatus %>

<% if $SubsiteSwitchList.Count > 1 %>
<% require javascript('silverstripe/subsites:client/dist/js/LeftAndMain_Subsites.js') %>
<% include SilverStripe\\Subsites\\Controller\\SubsiteXHRController_subsitelist %>
<% end_if %>
</div>
Expand Down

0 comments on commit 3a401ed

Please sign in to comment.