From 8037b0fbe1340701bfe06d3c0a8f6bdd969ecf04 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Thu, 15 Aug 2024 14:01:27 +1200 Subject: [PATCH] FIX Update CMS fields now that they're being scaffolded (#1315) --- .../UserFormFieldEditorExtension.php | 6 + code/Model/Recipient/EmailRecipient.php | 44 +++++++ code/UserForm.php | 107 +++++++----------- 3 files changed, 92 insertions(+), 65 deletions(-) diff --git a/code/Extension/UserFormFieldEditorExtension.php b/code/Extension/UserFormFieldEditorExtension.php index 805aa7f..4ea34e6 100644 --- a/code/Extension/UserFormFieldEditorExtension.php +++ b/code/Extension/UserFormFieldEditorExtension.php @@ -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. * diff --git a/code/Model/Recipient/EmailRecipient.php b/code/Model/Recipient/EmailRecipient.php index 237a504..d0ceef3 100644 --- a/code/Model/Recipient/EmailRecipient.php +++ b/code/Model/Recipient/EmailRecipient.php @@ -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 * diff --git a/code/UserForm.php b/code/UserForm.php index 244d022..fae5120 100644 --- a/code/UserForm.php +++ b/code/UserForm.php @@ -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();