Skip to content

Commit 1431334

Browse files
committed
#11303 allow locale to be set on mailables
1 parent 1fb0ec1 commit 1431334

File tree

8 files changed

+24
-17
lines changed

8 files changed

+24
-17
lines changed

classes/mail/Mailable.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ class Mailable extends IlluminateMailable
9191
public const ATTACHMENT_SUBMISSION_FILE = 'submissionFileId';
9292
public const ATTACHMENT_LIBRARY_FILE = 'libraryFileId';
9393

94+
/** @var string|null The specific locale to send the mail of this Mailable */
95+
protected ?string $mailableLocale = null;
96+
9497
/** @var string|null Locale key for the name of this Mailable */
9598
protected static ?string $name = null;
9699

@@ -121,9 +124,6 @@ class Mailable extends IlluminateMailable
121124
/** @var string embedded footer of the email */
122125
protected string $footer;
123126

124-
/** @var bool define if email data has been set */
125-
protected bool $isDataSet = false;
126-
127127
public function __construct(array $variables = [])
128128
{
129129
if (!empty($variables)) {
@@ -229,7 +229,7 @@ public function getData(?string $locale = null): array
229229
public function setData(?string $locale = null): void
230230
{
231231
if (is_null($locale)) {
232-
$locale = Locale::getLocale();
232+
$locale = $this->getLocale() ?? Locale::getLocale();
233233
}
234234
foreach ($this->variables as $variable) {
235235
$this->viewData = array_merge(
@@ -239,16 +239,24 @@ public function setData(?string $locale = null): void
239239
}
240240

241241
$this->addFooter($locale); // set the locale for the email footer
242+
}
243+
244+
/**
245+
* Set the mailable locale
246+
*/
247+
public function setLocale(string $locale): static
248+
{
249+
$this->mailableLocale = $locale;
242250

243-
$this->isDataSet = true;
251+
return $this;
244252
}
245253

246254
/**
247-
* return if the data for this email has been set
255+
* Get the mailable locale
248256
*/
249-
public function hasDataSet(): bool
257+
public function getLocale(): ?string
250258
{
251-
return $this->isDataSet;
259+
return $this->mailableLocale;
252260
}
253261

254262
/**

classes/mail/Mailer.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ public function send($view, array $data = [], $callback = null)
115115
{
116116
if (is_a($view, Mailable::class)) {
117117
/** @var Mailable $view */
118-
if (!$view->hasDataSet()) {
119-
$view->setData();
120-
}
118+
$view->setData($view->getLocale());
121119
}
122120

123121
// Application is set to sandbox mode and will sent any emails to log

classes/mail/mailables/AnnouncementNotify.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function setData(?string $locale = null): void
7373
{
7474
parent::setData($locale);
7575
if (is_null($locale)) {
76-
$locale = Locale::getLocale();
76+
$locale = $this->getLocale() ?? Locale::getLocale();
7777
}
7878

7979
$request = Application::get()->getRequest();

classes/task/ReviewReminder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function sendReminder(
6868
->from($context->getData('contactEmail'), $context->getData('contactName'))
6969
->recipients([$reviewer]);
7070

71-
$mailable->setData($primaryLocale);
71+
$mailable->setLocale($primaryLocale);
7272

7373
$application = Application::get();
7474
$request = $application->getRequest();

controllers/grid/users/reviewer/form/ReviewerForm.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ protected function getMailable(): ReviewRequest
460460
]);
461461

462462
// Remove template variables that haven't been set yet during form initialization
463-
$mailable->setData(Locale::getLocale());
463+
$locale = Locale::getLocale();
464+
$mailable->setLocale($locale)->setData($locale);
464465
unset($mailable->viewData[ReviewAssignmentEmailVariable::REVIEW_DUE_DATE]);
465466
unset($mailable->viewData[ReviewAssignmentEmailVariable::RESPONSE_DUE_DATE]);
466467
unset($mailable->viewData[ReviewAssignmentEmailVariable::REVIEW_ASSIGNMENT_URL]);

controllers/grid/users/reviewer/form/ThankReviewerForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function execute(...$functionArgs)
125125

126126
Hook::call('ThankReviewerForm::thankReviewer', [$submission, $reviewAssignment, $mailable]);
127127
if (!$this->getData('skipEmail')) {
128-
$mailable->setData(Locale::getLocale());
128+
$mailable->setLocale(Locale::getLocale());
129129
try {
130130
Mail::send($mailable);
131131
$submissionEmailLogDao = DAORegistry::getDAO('SubmissionEmailLogDAO'); /** @var SubmissionEmailLogDAO $submissionEmailLogDao */

jobs/notifications/NewAnnouncementNotifyUsers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function handle()
8888
$mailable = $this->createMailable($context, $recipient, $announcement, $template)
8989
->allowUnsubscribe($notification);
9090

91-
$mailable->setData($this->locale);
91+
$mailable->setLocale($this->locale);
9292
Mail::send($mailable);
9393
}
9494
}

jobs/notifications/StatisticsReportMail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function handle()
9696
$mailable->from($context->getContactEmail(), $context->getContactName());
9797
$mailable->subject($template->getLocalizedData('subject', $locale));
9898
$mailable->body($template->getLocalizedData('body', $locale));
99-
$mailable->setData($locale);
99+
$mailable->setLocale($locale);
100100
$mailable->attach($filePath, ['as' => 'editorial-report.csv']);
101101
$mailable->allowUnsubscribe($notification);
102102

0 commit comments

Comments
 (0)