From 1c9c35c7e583e537f322d8eeca2ea7809b936d0d Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 12 Nov 2024 16:43:11 +1300 Subject: [PATCH 1/2] FIX Fix reloading subsite selector when updating subsites When a new subsite is added or an existing on is renamed, the subsite selector needs to be reloaded. The code to do this broke at some stage. --- client/dist/js/LeftAndMain_Subsites.js | 2 +- client/src/js/LeftAndMain_Subsites.js | 22 ++++++++-------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/client/dist/js/LeftAndMain_Subsites.js b/client/dist/js/LeftAndMain_Subsites.js index 55de350a..4eb0d7a2 100644 --- a/client/dist/js/LeftAndMain_Subsites.js +++ b/client/dist/js/LeftAndMain_Subsites.js @@ -1 +1 @@ -!function(){"use strict";var t={669:function(t){t.exports=jQuery}},e={};var i,s=function i(s){var n=e[s];if(void 0!==n)return n.exports;var r=e[s]={exports:{}};return t[s](r,r.exports,i),r.exports}(669);(i=s).entwine("ss",(t=>{t("#SubsitesSelect").entwine({onadd(){this.on("change",(function(){window.location.search=t.query.set("SubsiteID",t(this).val())}))}}),t(".cms-container .cms-menu-list li a").entwine({onclick(e){t(".cms-container").loadFragment("admin/subsite_xhr","SubsiteList"),this._super(e)}}),t(".cms-container .SubsiteAdmin .cms-edit-form fieldset.ss-gridfield").entwine({onreload(e){t(".cms-container").loadFragment("admin/subsite_xhr","SubsiteList"),this._super(e)}}),t(".cms-container .tab.subsite-model").entwine({onadd(e){t(".cms-container").loadFragment("admin/subsite_xhr","SubsiteList"),this._super(e)}}),t("#Form_ItemEditForm_AccessAllSubsites").entwine({onmatch(){this.showHideSubsiteList();const e=this;t("#Form_ItemEditForm_AccessAllSubsites input").change((()=>{e.showHideSubsiteList()}))},showHideSubsiteList(){t("#Form_ItemEditForm_Subsites").parent().parent().css("display",t("#Form_ItemEditForm_AccessAllSubsites_1").is(":checked")?"none":"")}}),t(".cms-edit-form").entwine({getChangeTrackerOptions(){const e=void 0===this.entwineData("ChangeTrackerOptions");let i=this._super();return e&&(i=t.extend({},i),i.ignoreFieldSelector+=", input[name=IsSubsite]",this.setChangeTrackerOptions(i)),i}}),t(".cms-edit-form input[name=action_copytosubsite]").entwine({onclick(){this.closest("form").trigger("submit",[this])}})})),i.entwine("ss.preview",(t=>{t(".cms-preview").entwine({onafterIframeAdjustedForPreview(e,i){const s=t(i).find("meta[name=x-subsite-id]").attr("content");s&&(t(i).find("a").each((function(){const e=t(this).attr("href");void 0===e||e.match(/^http:\/\//)||t(this).attr("href",t.path.addSearchParams(e,{SubsiteID:s}))})),t(i).find("form").each((function(){const e=t(this).attr("action");void 0===e||e.match(/^http:\/\//)||t(this).append(``)})))}})}))}(); \ No newline at end of file +!function(){"use strict";var t={669:function(t){t.exports=jQuery}},e={};var i,s=function i(s){var n=e[s];if(void 0!==n)return n.exports;var r=e[s]={exports:{}};return t[s](r,r.exports,i),r.exports}(669);(i=s).entwine("ss",(t=>{t("#SubsitesSelect").entwine({onadd(){this.on("change",(function(){window.location.search=t.query.set("SubsiteID",t(this).val())}))}}),t(".cms-container .SubsiteAdmin .cms-edit-form fieldset.ss-gridfield").entwine({onreload(e){t(".cms-container").loadFragment("admin/subsite_xhr","SubsiteList"),this._super(e)}}),t("#Form_ItemEditForm").entwine({onaftersubmitform(e){this.find('[role="tab"].subsite-model').length>0&&t(".cms-container").loadFragment("admin/subsite_xhr","SubsiteList"),this._super(e)}}),t("#Form_ItemEditForm_AccessAllSubsites").entwine({onmatch(){this.showHideSubsiteList();const e=this;t("#Form_ItemEditForm_AccessAllSubsites input").change((()=>{e.showHideSubsiteList()}))},showHideSubsiteList(){t("#Form_ItemEditForm_Subsites").parent().parent().css("display",t("#Form_ItemEditForm_AccessAllSubsites_1").is(":checked")?"none":"")}}),t(".cms-edit-form").entwine({getChangeTrackerOptions(){const e=void 0===this.entwineData("ChangeTrackerOptions");let i=this._super();return e&&(i=t.extend({},i),i.ignoreFieldSelector+=", input[name=IsSubsite]",this.setChangeTrackerOptions(i)),i}}),t(".cms-edit-form input[name=action_copytosubsite]").entwine({onclick(){this.closest("form").trigger("submit",[this])}})})),i.entwine("ss.preview",(t=>{t(".cms-preview").entwine({onafterIframeAdjustedForPreview(e,i){const s=t(i).find("meta[name=x-subsite-id]").attr("content");s&&(t(i).find("a").each((function(){const e=t(this).attr("href");void 0===e||e.match(/^http:\/\//)||t(this).attr("href",t.path.addSearchParams(e,{SubsiteID:s}))})),t(i).find("form").each((function(){const e=t(this).attr("action");void 0===e||e.match(/^http:\/\//)||t(this).append(``)})))}})}))}(); \ No newline at end of file diff --git a/client/src/js/LeftAndMain_Subsites.js b/client/src/js/LeftAndMain_Subsites.js index 3c8b6098..a3d44624 100644 --- a/client/src/js/LeftAndMain_Subsites.js +++ b/client/src/js/LeftAndMain_Subsites.js @@ -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) */ @@ -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); } }); From bb0bbc7f383436298379f7bb090e7e57854eaca4 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 12 Nov 2024 16:44:15 +1300 Subject: [PATCH 2/2] API Make SubsiteXHRController a subclass of AdminController --- src/Controller/SubsiteXHRController.php | 46 ++++++++----------- src/Extensions/LeftAndMainSubsites.php | 15 ------ .../SilverStripe/Admin/LeftAndMain_Menu.ss | 1 + 3 files changed, 20 insertions(+), 42 deletions(-) diff --git a/src/Controller/SubsiteXHRController.php b/src/Controller/SubsiteXHRController.php index 04840749..57d1e0bf 100644 --- a/src/Controller/SubsiteXHRController.php +++ b/src/Controller/SubsiteXHRController.php @@ -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. @@ -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']); } diff --git a/src/Extensions/LeftAndMainSubsites.php b/src/Extensions/LeftAndMainSubsites.php index 94a1b8a7..ae2e4fa7 100644 --- a/src/Extensions/LeftAndMainSubsites.php +++ b/src/Extensions/LeftAndMainSubsites.php @@ -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; @@ -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) { @@ -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 - * @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 ?? '')) { diff --git a/templates/SilverStripe/Admin/LeftAndMain_Menu.ss b/templates/SilverStripe/Admin/LeftAndMain_Menu.ss index 095ce918..0f7d6918 100644 --- a/templates/SilverStripe/Admin/LeftAndMain_Menu.ss +++ b/templates/SilverStripe/Admin/LeftAndMain_Menu.ss @@ -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 %>