diff --git a/src/Forms/Email.php b/src/Forms/Email.php index 1c6f429d8c..ec4d3b2eda 100644 --- a/src/Forms/Email.php +++ b/src/Forms/Email.php @@ -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) { @@ -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) { @@ -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) @@ -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())); }); } } diff --git a/tests/Forms/EmailTest.php b/tests/Forms/EmailTest.php index b942bc9ec7..4eeaaf94b6 100644 --- a/tests/Forms/EmailTest.php +++ b/tests/Forms/EmailTest.php @@ -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; @@ -92,6 +93,12 @@ public function singleAddressProvider() 'single email with name using antlers' => ['{{ name }} <{{ email }}>', [ ['address' => 'foo@bar.com', 'name' => 'Foo Bar'], ]], + 'single email from global set using antlers' => ['{{ company_information:email }}', [ + ['address' => 'info@example.com', 'name' => null], + ]], + 'single email with name from global set using antlers' => ['{{ company_information:name }} <{{ company_information:email }}>', [ + ['address' => 'info@example.com', 'name' => 'Example Company'], + ]], ]; } @@ -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' => 'info@example.com', + ])); + $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' => 'foo@bar.com',