Skip to content

Commit

Permalink
[4.x] Allow using Global Set variables in form email configs (#8892)
Browse files Browse the repository at this point in the history
Co-authored-by: duncanmcclean <[email protected]>
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2023
1 parent 89bd9d5 commit b3e634f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Forms/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Email extends Mailable
protected $submissionData;
protected $config;
protected $site;
private $globalData;

public function __construct(Submission $submission, array $config, Site $site)
{
Expand Down Expand Up @@ -160,6 +161,10 @@ protected function getRenderableFieldData($values)

private function getGlobalsData()
{
if (! is_null($this->globalData)) {
return $this->globalData;
}

$data = [];

foreach (GlobalSet::all() as $global) {
Expand All @@ -172,7 +177,7 @@ private function getGlobalsData()
$data[$global->handle()] = $global->toAugmentedArray();
}

return array_merge($data, $data['global'] ?? []);
return $this->globalData = array_merge($data, $data['global'] ?? []);
}

protected function addresses($addresses)
Expand Down Expand Up @@ -203,7 +208,7 @@ protected function parseConfig(array $config)
return collect($config)->map(function ($value) {
$value = Parse::env($value); // deprecated

return (string) Antlers::parse($value, $this->submissionData);
return (string) Antlers::parse($value, array_merge($this->submissionData, $this->getGlobalsData()));
});
}
}
23 changes: 23 additions & 0 deletions tests/Forms/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Mockery;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Form;
use Statamic\Facades\GlobalSet;
use Statamic\Facades\Site;
use Statamic\Forms\Email;
use Statamic\Forms\Submission;
Expand Down Expand Up @@ -92,6 +93,12 @@ public function singleAddressProvider()
'single email with name using antlers' => ['{{ name }} <{{ email }}>', [
['address' => '[email protected]', 'name' => 'Foo Bar'],
]],
'single email from global set using antlers' => ['{{ company_information:email }}', [
['address' => '[email protected]', 'name' => null],
]],
'single email with name from global set using antlers' => ['{{ company_information:name }} <{{ company_information:email }}>', [
['address' => '[email protected]', 'name' => 'Example Company'],
]],
];
}

Expand Down Expand Up @@ -190,12 +197,28 @@ private function makeEmailWithSubmission(Submission $submission)

private function makeEmailWithConfig(array $config)
{
$globalSet = GlobalSet::make()->handle('company_information');
$globalSet->addLocalization($globalSet->makeLocalization('en')->data([
'name' => 'Example Company',
'email' => '[email protected]',
]));
$globalSet->save();

$formBlueprint = Blueprint::makeFromFields([
'name' => ['type' => 'text'],
'email' => ['type' => 'text'],
]);

$companyInformationBlueprint = Blueprint::makeFromFields([
'name' => ['type' => 'text'],
'email' => ['type' => 'text'],
]);

BlueprintRepository::shouldReceive('find')->with('forms.test')->andReturn($formBlueprint);
BlueprintRepository::shouldReceive('find')->with('globals.company_information')->andReturn($companyInformationBlueprint);

$form = tap(Form::make('test'))->save();

$submission = $form->makeSubmission()->data([
'name' => 'Foo Bar',
'email' => '[email protected]',
Expand Down

0 comments on commit b3e634f

Please sign in to comment.