Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2][Events] Use event classes for System plugins #43637

Open
wants to merge 19 commits into
base: 5.2-dev
Choose a base branch
from
Prev Previous commit
Next Next commit
system fields
  • Loading branch information
Fedik committed Jun 8, 2024
commit fd330f031ab422e564110ebec41cf2e49e0c980a
78 changes: 58 additions & 20 deletions plugins/system/fields/src/Extension/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@

use Joomla\CMS\Event\Content;
use Joomla\CMS\Event\Model;
use Joomla\CMS\Event\User;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\User\UserFactoryAwareTrait;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
use Joomla\Event\SubscriberInterface;
use Joomla\Registry\Registry;

// phpcs:disable PSR1.Files.SideEffects
Expand All @@ -27,10 +29,34 @@
*
* @since 3.7
*/
final class Fields extends CMSPlugin
final class Fields extends CMSPlugin implements SubscriberInterface
{
use UserFactoryAwareTrait;

/**
* Returns an array of events this subscriber will listen to.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public static function getSubscribedEvents(): array
{
return [
'onContentNormaliseRequestData' => 'onContentNormaliseRequestData',
'onContentPrepare' => 'onContentPrepare',
'onContentPrepareForm' => 'onContentPrepareForm',
'onContentAfterSave' => 'onContentAfterSave',
'onContentAfterDelete' => 'onContentAfterDelete',
'onUserAfterSave' => 'onUserAfterSave',
'onUserAfterDelete' => 'onUserAfterDelete',
'onContentAfterTitle' => 'onContentAfterTitle',
'onContentBeforeDisplay' => 'onContentBeforeDisplay',
'onContentAfterDisplay' => 'onContentAfterDisplay',

];
}

/**
* Normalizes the request data.
*
Expand Down Expand Up @@ -80,17 +106,18 @@ public function onContentNormaliseRequestData(Model\NormaliseRequestDataEvent $e
/**
* The save event.
*
* @param string $context The context
* @param \Joomla\CMS\Table\Table $item The table
* @param boolean $isNew Is new item
* @param array $data The validated data
* @param Model\AfterSaveEvent $event The event object
*
* @return void
*
* @since 3.7.0
*/
public function onContentAfterSave($context, $item, $isNew, $data = []): void
public function onContentAfterSave(Model\AfterSaveEvent $event): void
{
$context = $event->getContext();
$item = $event->getItem();
$data = $event->getData();

// Check if data is an array and the item has an id
if (!\is_array($data) || empty($item->id) || empty($data['com_fields'])) {
return;
Expand Down Expand Up @@ -155,17 +182,17 @@ public function onContentAfterSave($context, $item, $isNew, $data = []): void
/**
* The save event.
*
* @param array $userData The date
* @param boolean $isNew Is new
* @param boolean $success Is success
* @param string $msg The message
* @param User\AfterSaveEvent $event The event object
*
* @return void
*
* @since 3.7.0
*/
public function onUserAfterSave($userData, $isNew, $success, $msg): void
public function onUserAfterSave(User\AfterSaveEvent $event): void
{
$userData = $event->getUser();
$success = $event->getSavingResult();

// It is not possible to manipulate the user during save events
// Check if data is valid or we are in a recursion
if (!$userData['id'] || !$success) {
Expand All @@ -182,21 +209,29 @@ public function onUserAfterSave($userData, $isNew, $success, $msg): void
}

// Trigger the events with a real user
$this->onContentAfterSave('com_users.user', $user, false, $userData);
$contentEvent = new Model\AfterSaveEvent('onContentAfterSave', [
'context' => 'com_users.user',
'subject' => $user,
'isNew' => false,
'data' => $userData,
]);
$this->onContentAfterSave($contentEvent);
}

/**
* The delete event.
*
* @param string $context The context
* @param \stdClass $item The item
* @param Model\AfterDeleteEvent $event The event object
*
* @return void
*
* @since 3.7.0
*/
public function onContentAfterDelete($context, $item): void
public function onContentAfterDelete(Model\AfterDeleteEvent $event): void
{
$context = $event->getContext();
$item = $event->getItem();

// Set correct context for category
if ($context === 'com_categories.category') {
$context = $item->extension . '.categories';
Expand All @@ -219,20 +254,23 @@ public function onContentAfterDelete($context, $item): void
/**
* The user delete event.
*
* @param array $user The context
* @param boolean $success Is success
* @param string $msg The message
* @param User\AfterDeleteEvent $event The event object
*
* @return void
*
* @since 3.7.0
*/
public function onUserAfterDelete($user, $success, $msg): void
public function onUserAfterDelete(User\AfterDeleteEvent $event): void
{
$user = $event->getUser();
$item = new \stdClass();
$item->id = $user['id'];

$this->onContentAfterDelete('com_users.user', $item);
$contentEvent = new Model\AfterDeleteEvent('onContentAfterDelete', [
'context' => 'com_users.user',
'subject' => $user,
]);
$this->onContentAfterDelete($contentEvent);
}

/**
Expand Down