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 SingleRecordAdmin class for editing one record at a time #1842

Draft
wants to merge 1 commit into
base: 3
Choose a base branch
from

Conversation

GuySartorelli
Copy link
Member

@GuySartorelli GuySartorelli commented Oct 25, 2024

Note that there may be some methods still on LeftAndMain that will be migrated into this class, such as save() which isn't used by CMSMain or ModelAdmin - but moving those will be handled by #1763 once the bulk of the other refactoring has been completed. This is to reduce the chance of regressions in areas that still need to be refactored.

TODO

  1. Unit tests for the new class
  2. Possibly behat test using frameworktest fixtures, depending on how thorough I can make the unit tests
  3. Docs
  4. CMS 5 PR to deprecate SiteConfigLeftAndMain::save_siteconfig() and LeftAndMain.tree_config

Issue

Comment on lines -23 to +19
private static $tree_class = Member::class;
private static $model_class = Member::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.

See change in LeftAndMain

private static $tree_class = null;
private static $model_class = null;
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 subclasses of LeftAndMain which use this config are not trees - and even with CMSMain which is the only tree structure, this is getting refactored to be more generic anyway.

Makes sense to update this to be named something that all the classes using it can respect.

Comment on lines -1227 to -1240
if ($record->hasMethod('getCMSCompositeValidator')) {
// As of framework v4.7, a CompositeValidator is always available form a DataObject, but it may be
// empty (which is fine)
$form->setValidator($record->getCMSCompositeValidator());
} elseif ($record->hasMethod('getCMSValidator')) {
// BC support for framework < v4.7
$validator = $record->getCMSValidator();

// The clientside (mainly LeftAndMain*.js) rely on ajax responses
// which can be evaluated as javascript, hence we need
// to override any global changes to the validation handler.
if ($validator) {
$form->setValidator($validator);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

All of this boils down to $form->setValidator($record->getCMSCompositeValidator()), because all DataObject classes have that method, and $record will always be a DataObject.

Comment on lines -1241 to -1243
} else {
$form->unsetValidator();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

That method just sets it null anyway so even if somehow getCMSCompositeValidator() returned null (which it can't) the result would be the same.

Copy link
Member Author

Choose a reason for hiding this comment

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

Anything removed from this class was either not necessary (likely related to legacy CMS 3 or older code that no longer exists) or is already being done in one of the superclasses.

{
parent::init();
if (!$this->getResponse()->isRedirect()) {
$this->setCurrentPageID(Security::getCurrentUser()->ID);
Copy link
Member Author

Choose a reason for hiding this comment

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

Moving this into init instead of getEditForm so it takes affect for all actions on this controller (including any that might be added through extensions).

Skipping for redirects because at that point we're not doing anything in this controller anyway.

private static $tree_class = Group::class;
private static $model_class = Group::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.

I strongly suspect this config doesn't actually do anything for this class, but checking that is out of scope for this PR. I'm just updating it to the config that LeftAndMain expects.

Comment on lines +13 to +19
/**
* Determines if there should be a single record in the database that this admin edits.
*
* If this is not true, you need to provide a mechanism to tell the form which record it should be editing.
* This could be an action (e.g. edit/$ID), or could be based on something about the current member, etc.
*/
private static bool $only_one_record = true;
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 is for SiteConfig style functionality - but since CMSProfileController is explicitly for the current member and not the one member in the database, it needs to be configurable.

/**
* If no record exists, allow a new one to be created
*/
private static bool $allow_new_record = true;
Copy link
Member Author

Choose a reason for hiding this comment

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

For almost all controllers, we'll want to be able to create a record if there isn't one. CMSProfileController is the exception. Even with SiteConfig, if the record in the DB gets somehow deleted, you'll want to be able to create a new one.

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.

1 participant