Skip to content

Commit 6a51830

Browse files
Fediklaoneo
andauthored
[5.3] [Events] Use event classes for Content plugins (#43426)
* Events: ConfirmConsent * Content Events: Contact * Content Events: Fields * Content Events: Finder * Content Events: Joomla * Content Events: LoadModule * Content Events: PageBreak * Content Events: PageNavigation * Content Events: Vote * test update * test update --------- Co-authored-by: Allon Moritz <[email protected]>
1 parent f31d78a commit 6a51830

File tree

10 files changed

+329
-149
lines changed

10 files changed

+329
-149
lines changed

plugins/content/confirmconsent/src/Extension/ConfirmConsent.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
namespace Joomla\Plugin\Content\ConfirmConsent\Extension;
1212

13-
use Joomla\CMS\Form\Form;
13+
use Joomla\CMS\Event\Model\PrepareFormEvent;
1414
use Joomla\CMS\Plugin\CMSPlugin;
15+
use Joomla\Event\SubscriberInterface;
1516

1617
// phpcs:disable PSR1.Files.SideEffects
1718
\defined('_JEXEC') or die;
@@ -22,17 +23,8 @@
2223
*
2324
* @since 3.9.0
2425
*/
25-
final class ConfirmConsent extends CMSPlugin
26+
final class ConfirmConsent extends CMSPlugin implements SubscriberInterface
2627
{
27-
/**
28-
* Load the language file on instantiation.
29-
*
30-
* @var boolean
31-
*
32-
* @since 3.9.0
33-
*/
34-
protected $autoloadLanguage = true;
35-
3628
/**
3729
* The supported form contexts
3830
*
@@ -45,22 +37,39 @@ final class ConfirmConsent extends CMSPlugin
4537
'com_privacy.request',
4638
];
4739

40+
/**
41+
* Returns an array of events this subscriber will listen to.
42+
*
43+
* @return array
44+
*
45+
* @since __DEPLOY_VERSION__
46+
*/
47+
public static function getSubscribedEvents(): array
48+
{
49+
return [
50+
'onContentPrepareForm' => 'onContentPrepareForm',
51+
];
52+
}
53+
4854
/**
4955
* Add additional fields to the supported forms
5056
*
51-
* @param Form $form The form to be altered.
52-
* @param mixed $data The associated data for the form.
57+
* @param PrepareFormEvent $event The event instance.
5358
*
5459
* @return boolean
5560
*
5661
* @since 3.9.0
5762
*/
58-
public function onContentPrepareForm(Form $form, $data)
63+
public function onContentPrepareForm(PrepareFormEvent $event)
5964
{
65+
$form = $event->getForm();
66+
6067
if ($this->getApplication()->isClient('administrator') || !\in_array($form->getName(), $this->supportedContext)) {
6168
return true;
6269
}
6370

71+
$this->loadLanguage();
72+
6473
// Get the consent box Text & the selected privacyarticle
6574
$consentboxText = (string) $this->params->get(
6675
'consentbox_text',

plugins/content/contact/src/Extension/Contact.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010

1111
namespace Joomla\Plugin\Content\Contact\Extension;
1212

13+
use Joomla\CMS\Event\Content\ContentPrepareEvent;
1314
use Joomla\CMS\Language\Multilanguage;
1415
use Joomla\CMS\Plugin\CMSPlugin;
1516
use Joomla\CMS\Router\Route;
1617
use Joomla\Component\Contact\Site\Helper\RouteHelper;
1718
use Joomla\Database\DatabaseAwareTrait;
1819
use Joomla\Database\ParameterType;
19-
use Joomla\Registry\Registry;
20+
use Joomla\Event\SubscriberInterface;
2021

2122
// phpcs:disable PSR1.Files.SideEffects
2223
\defined('_JEXEC') or die;
@@ -27,30 +28,45 @@
2728
*
2829
* @since 3.2
2930
*/
30-
final class Contact extends CMSPlugin
31+
final class Contact extends CMSPlugin implements SubscriberInterface
3132
{
3233
use DatabaseAwareTrait;
3334

35+
/**
36+
* Returns an array of events this subscriber will listen to.
37+
*
38+
* @return array
39+
*
40+
* @since __DEPLOY_VERSION__
41+
*/
42+
public static function getSubscribedEvents(): array
43+
{
44+
return [
45+
'onContentPrepare' => 'onContentPrepare',
46+
];
47+
}
48+
3449
/**
3550
* Plugin that retrieves contact information for contact
3651
*
37-
* @param string $context The context of the content being passed to the plugin.
38-
* @param mixed &$row An object with a "text" property
39-
* @param mixed $params Additional parameters. See {@see PlgContentContent()}.
40-
* @param integer $page Optional page number. Unused. Defaults to zero.
52+
* @param ContentPrepareEvent $event The event instance.
4153
*
4254
* @return void
4355
*/
44-
public function onContentPrepare($context, &$row, $params, $page = 0)
56+
public function onContentPrepare(ContentPrepareEvent $event)
4557
{
58+
$context = $event->getContext();
59+
$row = $event->getItem();
60+
$params = $event->getParams();
61+
4662
$allowed_contexts = ['com_content.category', 'com_content.article', 'com_content.featured'];
4763

4864
if (!\in_array($context, $allowed_contexts)) {
4965
return;
5066
}
5167

5268
// Return if we don't have valid params or don't link the author
53-
if (!($params instanceof Registry) || !$params->get('link_author')) {
69+
if (!$params->get('link_author')) {
5470
return;
5571
}
5672

plugins/content/fields/src/Extension/Fields.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
namespace Joomla\Plugin\Content\Fields\Extension;
1212

13+
use Joomla\CMS\Event\Content\ContentPrepareEvent;
1314
use Joomla\CMS\Plugin\CMSPlugin;
1415
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
16+
use Joomla\Event\SubscriberInterface;
1517

1618
// phpcs:disable PSR1.Files.SideEffects
1719
\defined('_JEXEC') or die;
@@ -23,22 +25,36 @@
2325
*
2426
* @since 3.7.0
2527
*/
26-
final class Fields extends CMSPlugin
28+
final class Fields extends CMSPlugin implements SubscriberInterface
2729
{
30+
/**
31+
* Returns an array of events this subscriber will listen to.
32+
*
33+
* @return array
34+
*
35+
* @since __DEPLOY_VERSION__
36+
*/
37+
public static function getSubscribedEvents(): array
38+
{
39+
return [
40+
'onContentPrepare' => 'onContentPrepare',
41+
];
42+
}
43+
2844
/**
2945
* Plugin that shows a custom field
3046
*
31-
* @param string $context The context of the content being passed to the plugin.
32-
* @param object &$item The item object. Note $article->text is also available
33-
* @param object &$params The article params
34-
* @param int $page The 'page' number
47+
* @param ContentPrepareEvent $event The event instance.
3548
*
3649
* @return void
3750
*
3851
* @since 3.7.0
3952
*/
40-
public function onContentPrepare($context, &$item, &$params, $page = 0)
53+
public function onContentPrepare(ContentPrepareEvent $event)
4154
{
55+
$context = $event->getContext();
56+
$item = $event->getItem();
57+
4258
// If the item has a context, overwrite the existing one
4359
if ($context === 'com_finder.indexer' && !empty($item->context)) {
4460
$context = $item->context;

plugins/content/finder/src/Extension/Finder.php

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
namespace Joomla\Plugin\Content\Finder\Extension;
1212

1313
use Joomla\CMS\Event\Finder as FinderEvent;
14+
use Joomla\CMS\Event\Model;
1415
use Joomla\CMS\Plugin\CMSPlugin;
1516
use Joomla\CMS\Plugin\PluginHelper;
17+
use Joomla\Event\SubscriberInterface;
1618

1719
// phpcs:disable PSR1.Files.SideEffects
1820
\defined('_JEXEC') or die;
@@ -23,7 +25,7 @@
2325
*
2426
* @since 2.5
2527
*/
26-
final class Finder extends CMSPlugin
28+
final class Finder extends CMSPlugin implements SubscriberInterface
2729
{
2830
/**
2931
* Flag to check whether finder plugins already imported.
@@ -34,74 +36,87 @@ final class Finder extends CMSPlugin
3436
*/
3537
protected $pluginsImported = false;
3638

39+
/**
40+
* Returns an array of events this subscriber will listen to.
41+
*
42+
* @return array
43+
*
44+
* @since __DEPLOY_VERSION__
45+
*/
46+
public static function getSubscribedEvents(): array
47+
{
48+
return [
49+
'onContentBeforeSave' => 'onContentBeforeSave',
50+
'onContentAfterSave' => 'onContentAfterSave',
51+
'onContentAfterDelete' => 'onContentAfterDelete',
52+
'onContentChangeState' => 'onContentChangeState',
53+
'onCategoryChangeState' => 'onCategoryChangeState',
54+
];
55+
}
56+
3757
/**
3858
* Smart Search after save content method.
3959
* Content is passed by reference, but after the save, so no changes will be saved.
4060
* Method is called right after the content is saved.
4161
*
42-
* @param string $context The context of the content passed to the plugin (added in 1.6)
43-
* @param object $article A \Joomla\CMS\Table\Table\ object
44-
* @param bool $isNew If the content has just been created
62+
* @param Model\AfterSaveEvent $event The event instance.
4563
*
4664
* @return void
4765
*
4866
* @since 2.5
4967
*/
50-
public function onContentAfterSave($context, $article, $isNew): void
68+
public function onContentAfterSave(Model\AfterSaveEvent $event): void
5169
{
5270
$this->importFinderPlugins();
5371

5472
// Trigger the onFinderAfterSave event.
5573
$this->getDispatcher()->dispatch('onFinderAfterSave', new FinderEvent\AfterSaveEvent('onFinderAfterSave', [
56-
'context' => $context,
57-
'subject' => $article,
58-
'isNew' => $isNew,
74+
'context' => $event->getContext(),
75+
'subject' => $event->getItem(),
76+
'isNew' => $event->getIsNew(),
5977
]));
6078
}
6179

6280
/**
6381
* Smart Search before save content method.
6482
* Content is passed by reference. Method is called before the content is saved.
6583
*
66-
* @param string $context The context of the content passed to the plugin (added in 1.6).
67-
* @param object $article A \Joomla\CMS\Table\Table\ object.
68-
* @param bool $isNew If the content is just about to be created.
84+
* @param Model\BeforeSaveEvent $event The event instance.
6985
*
7086
* @return void
7187
*
7288
* @since 2.5
7389
*/
74-
public function onContentBeforeSave($context, $article, $isNew)
90+
public function onContentBeforeSave(Model\BeforeSaveEvent $event)
7591
{
7692
$this->importFinderPlugins();
7793

7894
// Trigger the onFinderBeforeSave event.
7995
$this->getDispatcher()->dispatch('onFinderBeforeSave', new FinderEvent\BeforeSaveEvent('onFinderBeforeSave', [
80-
'context' => $context,
81-
'subject' => $article,
82-
'isNew' => $isNew,
96+
'context' => $event->getContext(),
97+
'subject' => $event->getItem(),
98+
'isNew' => $event->getIsNew(),
8399
]));
84100
}
85101

86102
/**
87103
* Smart Search after delete content method.
88104
* Content is passed by reference, but after the deletion.
89105
*
90-
* @param string $context The context of the content passed to the plugin (added in 1.6).
91-
* @param object $article A \Joomla\CMS\Table\Table object.
106+
* @param Model\AfterDeleteEvent $event The event instance.
92107
*
93108
* @return void
94109
*
95110
* @since 2.5
96111
*/
97-
public function onContentAfterDelete($context, $article): void
112+
public function onContentAfterDelete(Model\AfterDeleteEvent $event): void
98113
{
99114
$this->importFinderPlugins();
100115

101116
// Trigger the onFinderAfterDelete event.
102117
$this->getDispatcher()->dispatch('onFinderAfterDelete', new FinderEvent\AfterDeleteEvent('onFinderAfterDelete', [
103-
'context' => $context,
104-
'subject' => $article,
118+
'context' => $event->getContext(),
119+
'subject' => $event->getItem(),
105120
]));
106121
}
107122

@@ -111,23 +126,21 @@ public function onContentAfterDelete($context, $article): void
111126
* from outside the edit screen. This is fired when the item is published,
112127
* unpublished, archived, or unarchived from the list view.
113128
*
114-
* @param string $context The context for the content passed to the plugin.
115-
* @param array $pks A list of primary key ids of the content that has changed state.
116-
* @param integer $value The value of the state that the content has been changed to.
129+
* @param Model\AfterChangeStateEvent $event The event instance.
117130
*
118131
* @return void
119132
*
120133
* @since 2.5
121134
*/
122-
public function onContentChangeState($context, $pks, $value)
135+
public function onContentChangeState(Model\AfterChangeStateEvent $event)
123136
{
124137
$this->importFinderPlugins();
125138

126139
// Trigger the onFinderChangeState event.
127140
$this->getDispatcher()->dispatch('onFinderChangeState', new FinderEvent\AfterChangeStateEvent('onFinderChangeState', [
128-
'context' => $context,
129-
'subject' => $pks,
130-
'value' => $value,
141+
'context' => $event->getContext(),
142+
'subject' => $event->getPks(),
143+
'value' => $event->getValue(),
131144
]));
132145
}
133146

@@ -136,23 +149,21 @@ public function onContentChangeState($context, $pks, $value)
136149
* Method is called when the state of the category to which the
137150
* content item belongs is changed.
138151
*
139-
* @param string $extension The extension whose category has been updated.
140-
* @param array $pks A list of primary key ids of the content that has changed state.
141-
* @param integer $value The value of the state that the content has been changed to.
152+
* @param Model\AfterCategoryChangeStateEvent $event The event instance.
142153
*
143154
* @return void
144155
*
145156
* @since 2.5
146157
*/
147-
public function onCategoryChangeState($extension, $pks, $value)
158+
public function onCategoryChangeState(Model\AfterCategoryChangeStateEvent $event)
148159
{
149160
$this->importFinderPlugins();
150161

151162
// Trigger the onFinderCategoryChangeState event.
152163
$this->getDispatcher()->dispatch('onFinderCategoryChangeState', new FinderEvent\AfterCategoryChangeStateEvent('onFinderCategoryChangeState', [
153-
'context' => $extension,
154-
'subject' => $pks,
155-
'value' => $value,
164+
'context' => $event->getExtension(),
165+
'subject' => $event->getPks(),
166+
'value' => $event->getValue(),
156167
]));
157168
}
158169

0 commit comments

Comments
 (0)