From ad7ceee8a9c4e25ae58c270e187df97b5354bf0b Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Mon, 30 Oct 2023 12:05:45 +0000 Subject: [PATCH 1/3] Allow using Global Set data in form configs --- src/Forms/Email.php | 2 +- src/Forms/SendEmails.php | 2 ++ tests/Forms/EmailTest.php | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Forms/Email.php b/src/Forms/Email.php index 1c6f429d8c..18143c5b2a 100644 --- a/src/Forms/Email.php +++ b/src/Forms/Email.php @@ -203,7 +203,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/src/Forms/SendEmails.php b/src/Forms/SendEmails.php index 9dc93479f1..c1f07fc488 100644 --- a/src/Forms/SendEmails.php +++ b/src/Forms/SendEmails.php @@ -6,6 +6,8 @@ use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Bus; use Statamic\Contracts\Forms\Submission; +use Statamic\Facades\Antlers; +use Statamic\Facades\GlobalSet; use Statamic\Sites\Site; class SendEmails 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', From d6d522eb52f2d697c301a8e2f1a42617211663f9 Mon Sep 17 00:00:00 2001 From: duncanmcclean Date: Mon, 30 Oct 2023 12:07:31 +0000 Subject: [PATCH 2/3] Fix styling --- src/Forms/SendEmails.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Forms/SendEmails.php b/src/Forms/SendEmails.php index c1f07fc488..9dc93479f1 100644 --- a/src/Forms/SendEmails.php +++ b/src/Forms/SendEmails.php @@ -6,8 +6,6 @@ use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Bus; use Statamic\Contracts\Forms\Submission; -use Statamic\Facades\Antlers; -use Statamic\Facades\GlobalSet; use Statamic\Sites\Site; class SendEmails From a715bb3a8b743dd6eaeb538626cda555fd55af56 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 30 Oct 2023 14:50:34 -0400 Subject: [PATCH 3/3] prevent getting globals over and over --- src/Forms/Email.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Forms/Email.php b/src/Forms/Email.php index 18143c5b2a..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)