Skip to content

Commit

Permalink
[FEATURE] Provide event ModifyDemandRepositoryEvent #2568
Browse files Browse the repository at this point in the history
  • Loading branch information
georgringer committed Nov 22, 2024
1 parent 01dfbda commit caf0de5
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 41 deletions.
15 changes: 15 additions & 0 deletions Classes/Domain/Repository/AbstractDemandedRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
namespace GeorgRinger\News\Domain\Repository;

use GeorgRinger\News\Domain\Model\DemandInterface;
use GeorgRinger\News\Event\ModifyDemandRepositoryEvent;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\Storage\BackendInterface;
Expand All @@ -25,13 +28,20 @@ abstract class AbstractDemandedRepository extends Repository implements Demanded
{
/** @var BackendInterface */
protected $storageBackend;
protected EventDispatcherInterface $eventDispatcher;

public function injectStorageBackend(
BackendInterface $storageBackend
): void {
$this->storageBackend = $storageBackend;
}

public function __construct()
{
$this->eventDispatcher = GeneralUtility::makeInstance(EventDispatcher::class);
parent::__construct();
}

/**
* Returns an array of constraints created from a given demand object.
*
Expand Down Expand Up @@ -118,6 +128,11 @@ protected function generateQuery(DemandInterface $demand, $respectEnableFields =
}
}

$event = new ModifyDemandRepositoryEvent($demand, $respectEnableFields, $query, $constraints);
$this->eventDispatcher->dispatch($event);
$respectEnableFields = $event->isRespectEnableFields();
$constraints = $event->getConstraints();

if ($respectEnableFields === false) {
$query->getQuerySettings()->setIgnoreEnableFields(true);

Expand Down
60 changes: 60 additions & 0 deletions Classes/Event/ModifyDemandRepositoryEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the "news" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace GeorgRinger\News\Event;

use GeorgRinger\News\Domain\Model\DemandInterface;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;

final class ModifyDemandRepositoryEvent
{

private DemandInterface $demand;
private bool $respectEnableFields;
private QueryInterface $query;
private array $constraints;

public function __construct(DemandInterface $demand, bool $respectEnableFields, QueryInterface $query, array $constraints)
{
$this->demand = $demand;
$this->respectEnableFields = $respectEnableFields;
$this->query = $query;
$this->constraints = $constraints;
}

public function getDemand(): DemandInterface
{
return $this->demand;
}

public function isRespectEnableFields(): bool
{
return $this->respectEnableFields;
}

public function getQuery(): QueryInterface
{
return $this->query;
}

public function getConstraints(): array
{
return $this->constraints;
}

public function setRespectEnableFields(bool $respectEnableFields): void
{
$this->respectEnableFields = $respectEnableFields;
}

public function setConstraints(array $constraints): void
{
$this->constraints = $constraints;
}
}
3 changes: 0 additions & 3 deletions Classes/Event/PluginPreviewSummaryEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@

namespace GeorgRinger\News\Event;

use GeorgRinger\News\Controller\CategoryController;
use GeorgRinger\News\Hooks\PluginPreviewRenderer;
use TYPO3\CMS\Backend\View\BackendLayout\Grid\GridColumnItem;
use TYPO3\CMS\Extbase\Mvc\Request;

final class PluginPreviewSummaryEvent
{

protected GridColumnItem $item;
protected PluginPreviewRenderer $pluginPreviewRenderer;

Expand Down
62 changes: 25 additions & 37 deletions Documentation/Tutorials/ExtendNews/Hooks/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Hooks
=====

Several hooks can be used to modify the behaviour of EXT:news.
Several events can be used to modify the behaviour of EXT:news.

.. contents::
:local:
Expand All @@ -13,60 +13,48 @@ Several hooks can be used to modify the behaviour of EXT:news.
Hooks
-----

.. _hooks_example_findDemanded:
.. event_example_findDemanded:
Domain/Repository/AbstractDemandedRepository.php findDemanded
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
\GeorgRinger\News\Event\ModifyDemandRepositoryEvent
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This hook is very powerful, as it allows to modify the query used to fetch the news records.
This event is very powerful, as it allows to modify the query used to fetch the news records.

Example
"""""""
This examples modifies the query and adds a constraint that only news records are shown which contain the word *yeah*.


First, register your implementation in the file ``ext_localconf.php``:

.. code-block:: php
First, register your implementation in the file `Configuration/Services.yaml`:

<?php
defined('TYPO3') or die();
.. code-block:: yaml
$GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['Domain/Repository/AbstractDemandedRepository.php']['findDemanded'][$_EXTKEY]
= \YourVendor\Extkey\Hooks\Repository::class . '->modify';
YourVendor\YourExtkey\EventListener\ModifyDemandRepositoryEventListener:
tags:
- name: event.listener
identifier: 'eventnews-modifydemandrepository'
event: \GeorgRinger\News\Event\ModifyDemandRepositoryEvent
Now create the file ``Classes/Hooks/Repository.php``:
Now create the file ``Classes/EventListener/ModifyDemandRepositoryEventListener.php``:

.. code-block:: php
<?php
<?php
namespace YourVendor\Extkey\Hooks;
namespace YourVendor\YourExtkey\EventListener;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use \GeorgRinger\News\Domain\Repository\NewsRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use GeorgRinger\News\Event\ModifyDemandRepositoryEvent
class Repository
{
public function modify(array $params, NewsRepository $newsRepository)
{
$this->updateConstraints($params['demand'], $params['respectEnableFields'], $params['query'], $params['constraints']);
}
class ModifyDemandRepositoryEventListener
{
public function __invoke(ModifyDemandRepositoryEvent $event) {
$constraints = $event->getConstraints();
$constraints[] = $query->like('title', '%' . $subject . '%');
$event->setConstraints($constraints);
}
}
/**
* @param \GeorgRinger\News\Domain\Model\Dto\NewsDemand $demand
* @param bool $respectEnableFields
* @param \TYPO3\CMS\Extbase\Persistence\QueryInterface $query
* @param array $constraints
*/
protected function updateConstraints($demand, $respectEnableFields, \TYPO3\CMS\Extbase\Persistence\QueryInterface $query, array &$constraints)
{
$subject = 'yeah';
$constraints[] = $query->like('title', '%' . $subject . '%');
}
}
.. hint:: Please change the vendor and extension key to your real life code.
Controller/NewsController overrideSettings
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'author' => 'Georg Ringer',
'author_email' => 'mail@ringer.it',
'state' => 'stable',
'version' => '12.1.0',
'version' => '12.2.0',
'constraints' => [
'depends' => [
'php' => '8.1.0-8.3.99',
Expand Down

0 comments on commit caf0de5

Please sign in to comment.