Skip to content

Commit 321b153

Browse files
authored
MailBeforeRendering event (joomla#43529)
1 parent f1dbcdb commit 321b153

File tree

4 files changed

+183
-1
lines changed

4 files changed

+183
-1
lines changed

libraries/src/Event/CoreEventAware.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ trait CoreEventAware
201201
'onPageCacheSetCaching' => PageCache\SetCachingEvent::class,
202202
'onPageCacheGetKey' => PageCache\GetKeyEvent::class,
203203
'onPageCacheIsExcluded' => PageCache\IsExcludedEvent::class,
204+
// Mail
205+
'onMailBeforeRendering' => Mail\BeforeRenderingMailTemplateEvent::class,
204206
];
205207

206208
/**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* Joomla! Content Management System
5+
*
6+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\CMS\Event\Mail;
11+
12+
/**
13+
* Class for MailTemplate events
14+
* Example:
15+
* new BeforeRenderingMailTemplateEvent('onEventName', ['templateId' => 'com_example.template', 'subject' => $mailTemplateInstance]);
16+
*
17+
* @since __DEPLOY_VERSION__
18+
*/
19+
class BeforeRenderingMailTemplateEvent extends MailTemplateEvent
20+
{
21+
/**
22+
* The argument names, in order expected by legacy plugins.
23+
*
24+
* @var array
25+
*
26+
* @since __DEPLOY_VERSION__
27+
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
28+
*/
29+
protected $legacyArgumentsOrder = ['templateId', 'subject'];
30+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
/**
4+
* Joomla! Content Management System
5+
*
6+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\CMS\Event\Mail;
11+
12+
use Joomla\CMS\Event\AbstractImmutableEvent;
13+
use Joomla\CMS\Event\ReshapeArgumentsAware;
14+
use Joomla\CMS\Mail\MailTemplate;
15+
16+
// phpcs:disable PSR1.Files.SideEffects
17+
\defined('_JEXEC') or die;
18+
// phpcs:enable PSR1.Files.SideEffects
19+
20+
/**
21+
* Base class for MailTemplate events
22+
*
23+
* @since __DEPLOY_VERSION__
24+
*/
25+
abstract class MailTemplateEvent extends AbstractImmutableEvent
26+
{
27+
use ReshapeArgumentsAware;
28+
29+
/**
30+
* The argument names, in order expected by legacy plugins.
31+
*
32+
* @var array
33+
*
34+
* @since __DEPLOY_VERSION__
35+
* @deprecated __DEPLOY_VERSION__ will be removed in 6.0
36+
*/
37+
protected $legacyArgumentsOrder = [];
38+
39+
/**
40+
* Constructor.
41+
*
42+
* @param string $name The event name.
43+
* @param array $arguments The event arguments.
44+
*
45+
* @throws \BadMethodCallException
46+
*
47+
* @since __DEPLOY_VERSION__
48+
*/
49+
public function __construct($name, array $arguments = [])
50+
{
51+
// Reshape the arguments array to preserve b/c with legacy listeners
52+
if ($this->legacyArgumentsOrder) {
53+
$arguments = $this->reshapeArguments($arguments, $this->legacyArgumentsOrder);
54+
}
55+
56+
parent::__construct($name, $arguments);
57+
58+
if (!\array_key_exists('subject', $this->arguments)) {
59+
throw new \BadMethodCallException("Argument 'subject' of event {$name} is required but has not been provided");
60+
}
61+
62+
if (!\array_key_exists('templateId', $this->arguments)) {
63+
throw new \BadMethodCallException("Argument 'templateId' of event {$name} is required but has not been provided");
64+
}
65+
}
66+
67+
/**
68+
* Pre-Setter for the subject argument.
69+
*
70+
* @param MailTemplate $value The value to set
71+
*
72+
* @return MailTemplate
73+
*
74+
* @since __DEPLOY_VERSION__
75+
*/
76+
protected function onSetSubject(MailTemplate $value): MailTemplate
77+
{
78+
return $value;
79+
}
80+
81+
/**
82+
* Pre-getter for the subject argument.
83+
*
84+
* @param MailTemplate $value The value to set
85+
*
86+
* @return MailTemplate
87+
*
88+
* @since __DEPLOY_VERSION__
89+
*/
90+
protected function onGetSubject(MailTemplate $value): MailTemplate
91+
{
92+
return $value;
93+
}
94+
95+
/**
96+
* Pre-setter for the templateId argument.
97+
*
98+
* @param string $value The value to set
99+
*
100+
* @return string
101+
*
102+
* @since __DEPLOY_VERSION__
103+
*/
104+
protected function onSetTemplateId(string $value): string
105+
{
106+
return $value;
107+
}
108+
109+
/**
110+
* Pre-getter for the templateId argument.
111+
*
112+
* @param string $value The value to set
113+
*
114+
* @return string
115+
*
116+
* @since __DEPLOY_VERSION__
117+
*/
118+
protected function onGetTemplateId(string $value): string
119+
{
120+
return $value;
121+
}
122+
123+
/**
124+
* Getter for the subject argument.
125+
*
126+
* @return MailTemplate
127+
*
128+
* @since __DEPLOY_VERSION__
129+
*/
130+
public function getTemplate(): MailTemplate
131+
{
132+
return $this->getArgument('subject');
133+
}
134+
135+
/**
136+
* Getter for the templateId argument.
137+
*
138+
* @return string
139+
*
140+
* @since __DEPLOY_VERSION__
141+
*/
142+
public function getTemplateId(): string
143+
{
144+
return $this->getArgument('templateId');
145+
}
146+
}

libraries/src/Mail/MailTemplate.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Joomla\CMS\Mail;
1111

1212
use Joomla\CMS\Component\ComponentHelper;
13+
use Joomla\CMS\Event\Mail\BeforeRenderingMailTemplateEvent;
1314
use Joomla\CMS\Factory;
1415
use Joomla\CMS\HTML\HTMLHelper;
1516
use Joomla\CMS\Language\Text;
@@ -290,7 +291,10 @@ public function send()
290291
$useLayout = $params->get('disable_htmllayout', $useLayout);
291292
}
292293

293-
$app->triggerEvent('onMailBeforeRendering', [$this->template_id, &$this]);
294+
$app->getDispatcher()->dispatch('onMailBeforeRendering', new BeforeRenderingMailTemplateEvent(
295+
'onMailBeforeRendering',
296+
['templateId' => $this->template_id, 'subject' => $this]
297+
));
294298

295299
$subject = $this->replaceTags(Text::_($mail->subject), $this->data);
296300
$this->mailer->setSubject($subject);

0 commit comments

Comments
 (0)