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

[4.x] Allow using Global Set variables in form email configs #8892

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
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
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
Loading