Skip to content

Commit

Permalink
Merge pull request silverstripe#611 from creative-commoners/pulls/4/f…
Browse files Browse the repository at this point in the history
…ormschema

API Make SubsiteXHRController a subclass of AdminController
  • Loading branch information
emteknetnz authored Nov 19, 2024
2 parents 487860a + bb0bbc7 commit fd17856
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 57 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/LeftAndMain_Subsites.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 8 additions & 14 deletions client/src/js/LeftAndMain_Subsites.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@
}
});

/*
* Reload subsites dropdown when links are processed
*/
$('.cms-container .cms-menu-list li a').entwine({
onclick(e) {
$('.cms-container').loadFragment('admin/subsite_xhr', 'SubsiteList');
this._super(e);
}
});

/*
* Reload subsites dropdown when the admin area reloads (for deleting sites)
*/
Expand All @@ -32,11 +22,15 @@
});

/*
* Reload subsites dropdown when subsites are added or names are modified
* Reload subsites dropdown when subsites are added or modified
*/
$('.cms-container .tab.subsite-model').entwine({
onadd(e) {
$('.cms-container').loadFragment('admin/subsite_xhr', 'SubsiteList');
$('#Form_ItemEditForm').entwine({
onaftersubmitform(e) {
// Only load the fragment if this form is the subsite form.
// We can't add a selector to the form itself so check for the tab we added a specific class to.
if (this.find('[role="tab"].subsite-model').length > 0) {
$('.cms-container').loadFragment('admin/subsite_xhr', 'SubsiteList');
}
this._super(e);
}
});
Expand Down
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: 0 additions & 15 deletions src/Extensions/LeftAndMainSubsites.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Extension;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Model\List\ArrayList;
use SilverStripe\ORM\DataObject;
Expand Down Expand Up @@ -69,8 +68,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 +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<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
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 fd17856

Please sign in to comment.