Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Make SubsiteXHRController a subclass of AdminController #611

Merged
merged 2 commits into from
Nov 19, 2024

Conversation

GuySartorelli
Copy link
Member

When I did silverstripe/silverstripe-admin#1761 I thought SubsiteXHRController needed form schema logic, but it turns out it doesn't, so it can be a direct subclass of AdminController.

There's two PRs here:

  1. Fixes a bug where the subsite selector didn't update when creating or updating subsites (found when trying to test this PR). Not doing as a patch because it moves logic into different selectors and who knows what weird nonsense someone might be doing that relies on that. Easier to just fix in 6 since that's where I'm already working.
  2. Make SubsiteXHRController a subclass of AdminController

Issue

/*
* Reload subsites dropdown when links are processed
*/
$('.cms-container .cms-menu-list li a').entwine({
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only is this selector wrong (should be .cms-menu__list) but this just straight up isn't necessary. Clicking on the links in the left menu reloads the whole left menu anyway.

Comment on lines -37 to 35
$('.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);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was trying to reload the selector any time it saw the root.main tab in the subsite edit form.
The selector was wrong, but the approach was wrong too - we should only reload the selector after the form is submitted, not just any time the form loads.

Comment on lines +20 to +25
public function index(HTTPRequest $request): HTTPResponse
{
return $this->getResponseNegotiator()->respond($request);
}
Copy link
Member Author

@GuySartorelli GuySartorelli Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary because we're using loadFragment in js rather than directly hitting the SubsiteList method as a controller action

Comment on lines +48 to +54
return new PjaxResponseNegotiator([
'SubsiteList' => function () {
return $this->SubsiteList();
},
]);
Copy link
Member Author

@GuySartorelli GuySartorelli Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the only fragment this controller ever cares about.
Using new because that's what's done everywhere that uses it right now and it's not an Injectable class.

Comment on lines 50 to 62
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<Subsite>
*/
public static function SubsiteSwitchList(): SS_List
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since SubsiteXHRController isn't a subclass of LeftAndMain anymore, it doesn't get access to the methods in this extension. This method is only needed in templates anyway so I've just made it always available in templates.

Since I had to break the method signature to make it static I took the liberty of renaming it to make it much clearer what its purpose is.

The alternative would be to make a separate extension with just this logic and apply it to both LeftAndMain and the xhr controller. Happy to do that if you prefer that approach.

@@ -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') %>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the require code here instead of in the method since we don't need to re-require it for the xhr requests and because frankly a require call in an arbitrary PHP method is just gross

*/
public function canAccess()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

canAccess() is a non-standard method that's only applied to LeftAndMain classes via the LeftAndMainSubsites extension. It no longer applies here, so we use required_permission_codes instead set to the less restrictive of these since canView() is handling the main access check for us.

@GuySartorelli GuySartorelli force-pushed the pulls/4/formschema branch 2 times, most recently from 3a401ed to 9e10169 Compare November 14, 2024 20:56
@GuySartorelli GuySartorelli marked this pull request as ready for review November 14, 2024 21:01
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.
@GuySartorelli
Copy link
Member Author

rebased

@emteknetnz emteknetnz merged commit fd17856 into silverstripe:4 Nov 19, 2024
9 checks passed
@emteknetnz emteknetnz deleted the pulls/4/formschema branch November 19, 2024 04:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants