Skip to content

Commit

Permalink
FIX Update CMS fields now that they're being scaffolded (#1315)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Aug 15, 2024
1 parent 01dcf8a commit 8037b0f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 62 deletions.
6 changes: 6 additions & 0 deletions code/Extension/UserFormFieldEditorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
44 changes: 44 additions & 0 deletions code/Model/Recipient/EmailRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
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;
Expand Down Expand Up @@ -163,6 +167,46 @@ public function summaryFields()
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
*
Expand Down
101 changes: 39 additions & 62 deletions code/UserForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +29,6 @@
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;
Expand Down Expand Up @@ -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 = [
Expand All @@ -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
*
Expand Down Expand Up @@ -183,72 +190,42 @@ public function getCMSFields()
{
Requirements::css('silverstripe/userforms:client/dist/styles/userforms-cms.css');

$this->beforeUpdateCMSFields(function ($fields) {

// remove
$fields->removeByName([
'OnCompleteMessageLabel',
'OnCompleteMessage',
'Fields',
'EmailRecipients'
]);
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->findTab('Root.EmailRecipients')
?->setName('Recipients')
?->setTitle(_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.RECIPIENTS', 'Recipients'));
$fields->dataFieldByName('EmailRecipients')?->setTitle('');

// 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
$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')
)
);

$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',
CheckboxField::create(
'DisableSaveSubmissions',
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SAVESUBMISSIONS', 'Disable Saving Submissions to Server')
)
);

// Fix tab order - otherwise recipients comes too early due to being scaffolded
$fields->findTab('Root')->changeTabOrder(['Main', 'FormOptions', 'Recipients', 'Submissions']);
});

$fields = parent::getCMSFields();
Expand Down

0 comments on commit 8037b0f

Please sign in to comment.