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

NEW Move form schema logic into FormSchemaController #1850

Merged
merged 1 commit into from
Nov 19, 2024

Conversation

GuySartorelli
Copy link
Member

In a perfect world LeftAndMain would not be a subclass of the new FormSchemaController class - but so many of its subclasses rely directly on that logic, and refactoring those to spin off sub-controllers looks like way more work than it's really worth.

I thought of making the new class a trait instead, but if a trait has private static properties the subclass can't also have those private static properties.
We could implement getExtraMethodConfig() in the trait to work around that, but that's an ugly solution imo.

This PR still takes us closer to making LeftAndMain abstract (since it doesn't need to route Modals anymore) and makes it easier to have non-LeftAndMain form schema controllers, and those are the main goals for this card.

Issue

Comment on lines +45 to +56
if (!$this->schema) {
$this->schema = FormSchema::singleton();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This wasn't there in LeftAndMain but it's a little protection in case the dependency doesn't get injected (e.g. not using injector to instantiate this controller for some reason)

Copy link
Member Author

Choose a reason for hiding this comment

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

The code in this class is mostly directly copied in from LeftAndMain.

Note that there were no existing unit tests explicitly for the methods in this class - instead, this functionality is fairly heavily tested in behat (linkfield, elemental, history controller, WYSIWYG link modals all use it)

Comment on lines 72 to 91
// The getter can accept an ID where the main form action wouldn't
$formMethod = "get{$formName}";
if (!$this->hasMethod($formMethod)) {
$formMethod = $formName;
if (!$this->hasMethod($formMethod)) {
$this->jsonError(404, 'Form not found');
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Most usages of this method right now (e.g. see AssetAdmin) have two methods - one method is named the same as the form, and does not accept a parameter - the other prefixed with get and accepts a parameter.

The usages of ModalController used to go through methodSchema on LeftAndMain - but now we just go directly through schema on ModalController. Those methods do not have get prefixes.

I'm not going through and refactoring all of those - that's out of scope. Instead, I've just updated this method to account for both scenarios.

Copy link
Member

Choose a reason for hiding this comment

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

Could you put some the github comment info into the inline comment - by itself the existing comment "The getter can accept an ID where the main form action wouldn't" is pretty confusing

Copy link
Member Author

Choose a reason for hiding this comment

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

Happy to do so, but I'm not sure what you find confusing about it so I don't know what info you want in there. What would you like the comment to say? Or what about the existing comment is confusing?

Copy link
Member

Choose a reason for hiding this comment

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

Most usages of this method (e.g. AssetAdmin) have two methods - one method is named the same as the form, and does not accept a parameter - the other prefixed with "get" and accepts an $itemID parameter.

^ Maybe just the comment to this

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

* Get the form schema from a given method.
* The method must return a Form.
*/
public function methodSchema(HTTPRequest $request): HTTPResponse
Copy link
Member Author

@GuySartorelli GuySartorelli Nov 13, 2024

Choose a reason for hiding this comment

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

This method was just a bizarre way to avoid directly linking to ModalController.
It's not used anywhere now that we're linking to ModalController directly. If anyone's using it directly in their own projects or modules, the recommendation will be to directly link to the schema for the FormSchemaController that handles that form, instead of this indirection.

Comment on lines 41 to 47
protected function init()
{
$this->beforeExtending('onInit', function () {
// Set the members html editor config
if (Security::getCurrentUser()) {
HTMLEditorConfig::set_active_identifier(Security::getCurrentUser()->getHtmlEditorConfigForCMS());
}

// Set default values in the config if missing. These things can't be defined in the config
// file because insufficient information exists when that is being processed
$htmlEditorConfig = HTMLEditorConfig::get_active();
$htmlEditorConfig->setOption('language', TinyMCEConfig::get_tinymce_lang());
$langUrl = TinyMCEConfig::get_tinymce_lang_url();
if ($langUrl) {
$htmlEditorConfig->setOption('language_url', $langUrl);
}
});
parent::init();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Needed for controllers such as ElementalAreaController which may include WYSIWYG in their forms.

@@ -104,7 +94,6 @@ class LeftAndMain extends AdminController implements PermissionProvider
*
* @config
* @var string
* @deprecated 2.4.0 Will be renamed to model_class
Copy link
Member Author

Choose a reason for hiding this comment

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

unrelated - was missed in a merge-up

@GuySartorelli GuySartorelli marked this pull request as ready for review November 14, 2024 21:01
{
$this->beforeExtending('onInit', function () {
// Set the members html editor config
if (Security::getCurrentUser()) {
Copy link
Member

Choose a reason for hiding this comment

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

Looks kind of messy having all of this here around the top of the file since it's all TinyMCE related which is weird in a FormSchema class. Move it to a private method down the bottom of the class

Copy link
Member Author

Choose a reason for hiding this comment

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

TinyMCE is in forms, so form schema having form stuff doesn't seem weird to me. Done anyway though.

}

/**
* Get form schema helper
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Get form schema helper
* Get the form schema helper for this controller

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

}

/**
* Set form schema helper for this controller
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Set form schema helper for this controller
* Set the form schema helper for this controller

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

}

if (!$this->hasAction($formName)) {
$this->jsonError(401, 'Form not accessible');
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$this->jsonError(401, 'Form not accessible');
$this->jsonError(404, 'Form not accessible');

It's an invalid route, rather than an issue with a user not being logged in

Copy link
Member Author

Choose a reason for hiding this comment

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

Changing error codes is not in scope for this PR. This isn't new code.

Comment on lines 72 to 91
// The getter can accept an ID where the main form action wouldn't
$formMethod = "get{$formName}";
if (!$this->hasMethod($formMethod)) {
$formMethod = $formName;
if (!$this->hasMethod($formMethod)) {
$this->jsonError(404, 'Form not found');
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Could you put some the github comment info into the inline comment - by itself the existing comment "The getter can accept an ID where the main form action wouldn't" is pretty confusing

@emteknetnz emteknetnz merged commit d9c8726 into silverstripe:3 Nov 19, 2024
11 checks passed
@emteknetnz emteknetnz deleted the pulls/3/formschema branch November 19, 2024 04:43
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