FIX Update CMS fields now that they're being scaffolded (#1315)

This commit is contained in:
Guy Sartorelli 2024-08-15 14:01:27 +12:00 committed by GitHub
parent 01dcf8ab1c
commit 8037b0fbe1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 92 additions and 65 deletions

View File

@ -49,6 +49,12 @@ class UserFormFieldEditorExtension extends DataExtension
'Fields'
];
private static array $scaffold_cms_fields_settings = [
'ignoreRelations' => [
'Fields',
],
];
/**
* Adds the field editor to the page.
*

View File

@ -15,10 +15,14 @@ use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\Forms\LiteralField;
@ -163,6 +167,46 @@ class EmailRecipient extends DataObject
return $fields;
}
public function scaffoldFormFieldForHasMany(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord,
bool &$includeInOwnTab
): FormField {
$includeInOwnTab = true;
return $this->scaffoldFormFieldForManyRelation($relationName, $fieldTitle, $ownerRecord);
}
public function scaffoldFormFieldForManyMany(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord,
bool &$includeInOwnTab
): FormField {
$includeInOwnTab = true;
return $this->scaffoldFormFieldForManyRelation($relationName, $fieldTitle, $ownerRecord);
}
private function scaffoldFormFieldForManyRelation(
string $relationName,
?string $fieldTitle,
DataObject $ownerRecord
): FormField {
$emailRecipientsConfig = GridFieldConfig_RecordEditor::create(10);
$emailRecipientsConfig->getComponentByType(GridFieldAddNewButton::class)
->setButtonName(
_t(UserDefinedForm::class . '.ADDEMAILRECIPIENT', 'Add Email Recipient')
);
$emailRecipientsConfig->getComponentByType(GridFieldDetailForm::class)
->setItemRequestClass(UserFormRecipientItemRequest::class);
return GridField::create(
$relationName,
$fieldTitle,
$ownerRecord->$relationName(),
$emailRecipientsConfig
);
}
/**
* Get instance of UserForm when editing in getCMSFields
*

View File

@ -3,15 +3,12 @@
namespace SilverStripe\UserForms;
use Colymba\BulkManager\BulkManager;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
@ -32,7 +29,6 @@ use SilverStripe\UserForms\Extension\UserFormFieldEditorExtension;
use SilverStripe\UserForms\Extension\UserFormValidator;
use SilverStripe\UserForms\Form\UserFormsGridFieldFilterHeader;
use SilverStripe\UserForms\Model\Recipient\EmailRecipient;
use SilverStripe\UserForms\Model\Recipient\UserFormRecipientItemRequest;
use SilverStripe\UserForms\Model\Submission\SubmittedForm;
use SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\View\Requirements;
@ -117,8 +113,8 @@ trait UserForm
* @var array
*/
private static $has_many = [
'EmailRecipients' => EmailRecipient::class,
'Submissions' => SubmittedForm::class,
'EmailRecipients' => EmailRecipient::class
];
private static $cascade_deletes = [
@ -135,6 +131,17 @@ trait UserForm
'ErrorContainerID' => 'Text'
];
private static array $scaffold_cms_fields_settings = [
'ignoreFields' => [
'OnCompleteMessageLabel',
'OnCompleteMessage',
'DisableSaveSubmissions',
],
'ignoreRelations' => [
'Submissions',
],
];
/**
* Error container selector which matches the element for grouped messages
*
@ -183,72 +190,42 @@ trait UserForm
{
Requirements::css('silverstripe/userforms:client/dist/styles/userforms-cms.css');
$this->beforeUpdateCMSFields(function ($fields) {
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->findTab('Root.EmailRecipients')
?->setName('Recipients')
?->setTitle(_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.RECIPIENTS', 'Recipients'));
$fields->dataFieldByName('EmailRecipients')?->setTitle('');
// remove
$fields->removeByName([
'OnCompleteMessageLabel',
'OnCompleteMessage',
'Fields',
'EmailRecipients'
]);
// define tabs
// Configuration options
$fields->findOrMakeTab('Root.FormOptions')->setTitle(_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.CONFIGURATION', 'Configuration'));
$fields->findOrMakeTab('Root.Recipients')->setTitle(_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.RECIPIENTS', 'Recipients'));
// text to show on complete
$onCompleteFieldSet = CompositeField::create(
$label = LabelField::create(
'OnCompleteMessageLabel',
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.ONCOMPLETELABEL', 'Show on completion')
),
$editor = HTMLEditorField::create(
'OnCompleteMessage',
'',
$this->OnCompleteMessage
)
);
$onCompleteFieldSet->addExtraClass('field');
$editor->setRows(3);
$label->addExtraClass('left');
// Define config for email recipients
$emailRecipientsConfig = GridFieldConfig_RecordEditor::create(10);
$emailRecipientsConfig->getComponentByType(GridFieldAddNewButton::class)
->setButtonName(
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.ADDEMAILRECIPIENT', 'Add Email Recipient')
);
// who do we email on submission
$emailRecipients = GridField::create(
'EmailRecipients',
'',
$this->EmailRecipients(),
$emailRecipientsConfig
);
$emailRecipients
->getConfig()
->getComponentByType(GridFieldDetailForm::class)
->setItemRequestClass(UserFormRecipientItemRequest::class);
$fields->addFieldToTab('Root.FormOptions', $onCompleteFieldSet);
$fields->addFieldToTab('Root.Recipients', $emailRecipients);
$fields->addFieldsToTab('Root.FormOptions', $this->getFormOptions()->toArray());
$submissions = $this->getSubmissionsGridField();
$fields->findOrMakeTab('Root.Submissions')->setTitle(_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SUBMISSIONS', 'Submissions'));
$fields->addFieldToTab('Root.Submissions', $submissions);
$fields->addFieldToTab(
'Root.FormOptions',
$fields->addFieldsToTab('Root.FormOptions', [
// text to show on complete
CompositeField::create(
$label = LabelField::create(
'OnCompleteMessageLabel',
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.ONCOMPLETELABEL', 'Show on completion')
),
$editor = HTMLEditorField::create(
'OnCompleteMessage',
'',
$this->OnCompleteMessage
)
)->addExtraClass('field'),
...$this->getFormOptions()->toArray(),
CheckboxField::create(
'DisableSaveSubmissions',
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SAVESUBMISSIONS', 'Disable Saving Submissions to Server')
)
);
]);
$editor->setRows(3);
$label->addExtraClass('left');
$submissions = $this->getSubmissionsGridField();
$fields->findOrMakeTab('Root.Submissions')->setTitle(_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SUBMISSIONS', 'Submissions'));
$fields->addFieldToTab('Root.Submissions', $submissions);
// Fix tab order - otherwise recipients comes too early due to being scaffolded
$fields->findTab('Root')->changeTabOrder(['Main', 'FormOptions', 'Recipients', 'Submissions']);
});
$fields = parent::getCMSFields();