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

Allow selecting field for email recipient without saving first #42

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions src/Model/ElementForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

namespace DNADesign\ElementalUserForms\Model;

use SilverStripe\UserForms\Control\UserDefinedFormController;
use SilverStripe\UserForms\UserForm;
use SilverStripe\Control\Controller;
use DNADesign\Elemental\Models\BaseElement;
use DNADesign\ElementalUserForms\Control\ElementFormController;
use SilverStripe\Control\Controller;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\UserForms\Control\UserDefinedFormController;
use SilverStripe\UserForms\UserForm;

class ElementForm extends BaseElement
{
use UserForm;
use UserForm {
getCMSFields as userFormGetCMSFields;
}

private static $table_name = 'ElementForm';

Expand All @@ -25,6 +31,51 @@ class ElementForm extends BaseElement

private static $inline_editable = false;

/**
* This is a workaround for "Email recipients" functionality. In the userforms module, this
* relies on accessing the current userforms instance (EmailRecipient::getFormParent()) by
* using the LeftAndMain.currentPage session variable. When using an elemental userform, that
* session variable points to the parent page instead of the elemental userform instance.
*/
public function getCMSFields()
{
$this->afterExtending('updateCMSFields', function (FieldList $fields) {
/** @var GridField $recipientsGridField */
$recipientsGridField = $fields->dataFieldByName('EmailRecipients');
/** @var GridFieldDetailForm $detailForm */
$detailForm = $recipientsGridField->getConfig()->getComponentByType(GridFieldDetailForm::class);

// Re-build the email recipients CMS fields with the "form parent" record populated
$detailForm->setItemEditFormCallback(function (Form $form) {
$record = $form->getRecord();

// EmailRecipient::getFormParent() will use these values if set, before falling back to the
// LeftAndMain.currentParent session variable (which won't work for the reasons above)
$record->FormID = $this->ID;
$record->FormClass = $this->ClassName;

// Re-build CMS fields
$form->setFields($record->getCMSFields());
// Re-populate the form
$form->loadDataFrom($record, $record->ID == 0 ? Form::MERGE_IGNORE_FALSEISH : Form::MERGE_DEFAULT);

// Everything below this point is copied from GridFieldDetailForm_ItemRequest::ItemEditForm()

if ($record->ID && !$record->canEdit()) {
// Restrict editing of existing records
$form->makeReadonly();
} elseif (!$record->ID && !$record->canCreate()) {
// Restrict creation of new records
$form->makeReadonly();
}

$form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet');
});
});

return $this->userFormGetCMSFields();
}

/**
* @return UserForm
*/
Expand Down