Update default email_template_directory

Also if the overridded directory is empty - don’t display the template dropdown as this will cause a validation error preventing the user from saving the page.
This commit is contained in:
Will Rossiter 2017-10-22 07:47:17 +13:00 committed by Robbie Averill
parent 5b59732c08
commit e82dc5f121
2 changed files with 31 additions and 20 deletions

View File

@ -37,6 +37,8 @@ use SilverStripe\UserForms\UserForm;
use SilverStripe\View\Requirements;
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Core\Config\Config;
/**
* A Form can have multiply members / emails to email the submission
@ -348,11 +350,6 @@ class EmailRecipient extends DataObject
'Send email as plain text? (HTML will be stripped)'
)
),
DropdownField::create(
'EmailTemplate',
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILTEMPLATE', 'Email template'),
$this->getEmailTemplateDropdownValues()
)->addExtraClass('toggle-html-only'),
HTMLEditorField::create(
'EmailBodyHtml',
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILBODYHTML', 'Body')
@ -366,6 +363,18 @@ class EmailRecipient extends DataObject
LiteralField::create('EmailPreview', $preview)
]);
$templates = $this->getEmailTemplateDropdownValues();
if ($templates) {
$fields->insertBefore(
DropdownField::create(
'EmailTemplate',
_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILTEMPLATE', 'Email template')
)->addExtraClass('toggle-html-only'),
'EmailBodyHtml'
);
}
$fields->fieldByName('Root.EmailContent')->setTitle(_t(__CLASS__.'.EMAILCONTENTTAB', 'Email Content'));
// Custom rules for sending this field
@ -546,13 +555,20 @@ class EmailRecipient extends DataObject
$finder = new FileFinder();
$finder->setOption('name_regex', '/^.*\.ss$/');
$templateDirectory = UserDefinedForm::config()->get('email_template_directory');
// Handle cases where "userforms" might not be the base module directory, e.g. in a Travis build
if (!file_exists(BASE_PATH . DIRECTORY_SEPARATOR . $templateDirectory)
&& substr($templateDirectory, 0, 10) === 'userforms/'
) {
$templateDirectory = substr($templateDirectory, 10);
$parent = $this->getFormParent();
if (!$parent) {
return [];
}
$templateDirectory = ModuleResourceLoader::resourcePath(
$parent->config()->get('email_template_directory')
);
if (!$templateDirectory) {
return [];
}
$found = $finder->find(BASE_PATH . DIRECTORY_SEPARATOR . $templateDirectory);
foreach ($found as $key => $value) {
@ -562,14 +578,6 @@ class EmailRecipient extends DataObject
strlen(BASE_PATH) + 1
);
$defaultPrefixes = ['userforms/templates/', 'templates/'];
foreach ($defaultPrefixes as $defaultPrefix) {
// Remove default userforms folder if it's provided
if (substr($templatePath, 0, strlen($defaultPrefix)) === $defaultPrefix) {
$templatePath = substr($templatePath, strlen($defaultPrefix));
break;
}
}
$templates[$templatePath] = $template['filename'];
}

View File

@ -36,6 +36,7 @@ use SilverStripe\UserForms\Model\Recipient\UserFormRecipientItemRequest;
use SilverStripe\UserForms\Model\Submission\SubmittedForm;
use SilverStripe\UserForms\Model\EditableFormField;
use SilverStripe\View\Requirements;
use SilverStripe\Core\Config\Configurable;
/**
* Defines the user defined functionality to be applied to any {@link DataObject}
@ -43,6 +44,8 @@ use SilverStripe\View\Requirements;
*/
trait UserForm
{
use Configurable;
/**
* Built in extensions required by this page.
*
@ -61,7 +64,7 @@ trait UserForm
/**
* @var string
*/
private static $email_template_directory = 'userforms/templates/email/';
private static $email_template_directory = 'silverstripe/userforms:templates/email/';
/**
* Should this module automatically upgrade on dev/build?