Skip to content

Commit

Permalink
[FEATURE] Provide event NewsControllerOverrideSettingsEvent #2571
Browse files Browse the repository at this point in the history
  • Loading branch information
georgringer committed Nov 22, 2024
1 parent c11b015 commit 4457696
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 107 deletions.
5 changes: 5 additions & 0 deletions Classes/Controller/NewsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use GeorgRinger\News\Domain\Repository\TagRepository;
use GeorgRinger\News\Event\CreateDemandObjectFromSettingsEvent;
use GeorgRinger\News\Event\NewsCheckPidOfNewsRecordFailedInDetailActionEvent;
use GeorgRinger\News\Event\NewsControllerOverrideSettingsEvent;
use GeorgRinger\News\Event\NewsDateMenuActionEvent;
use GeorgRinger\News\Event\NewsDetailActionEvent;
use GeorgRinger\News\Event\NewsListActionEvent;
Expand Down Expand Up @@ -650,6 +651,10 @@ public function buildSettings(): void
$originalSettings = GeneralUtility::callUserFunction($_funcRef, $_params, $this);
}

$event = new NewsControllerOverrideSettingsEvent($originalSettings, $tsSettings, $this);
$this->eventDispatcher->dispatch($event);
$originalSettings = $event->getSettings();

$this->settings = $originalSettings;
}

Expand Down
47 changes: 47 additions & 0 deletions Classes/Event/NewsControllerOverrideSettingsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Controller\NewsController;

final class NewsControllerOverrideSettingsEvent
{
protected array $settings;
protected array $tsSettings;
protected NewsController $newsController;

public function __construct(array $settings, array $tsSettings, NewsController $newsController)
{
$this->settings = $settings;
$this->tsSettings = $tsSettings;
$this->newsController = $newsController;
}

public function getSettings(): array
{
return $this->settings;
}

public function getTsSettings(): array
{
return $this->tsSettings;
}

public function getNewsController(): NewsController
{
return $this->newsController;
}

public function setSettings(array $settings): void
{
$this->settings = $settings;
}

}
3 changes: 3 additions & 0 deletions Documentation/Reference/Events/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ fired. For additional items see column "Access to" in the table below.
.. csv-table:: Events
:header: "Event class", "Fired in class", "Access to", "Old Signal"

"ModifyDemandRepositoryEvent", "AbstractDemandedRepository", "", ""
"CreateDemandObjectFromSettingsEvent", "NewsController", "", ""
"NewsControllerOverrideSettingsEvent", "NewsController", "", ""
"PluginPreviewSummaryEvent", "PluginPreviewRenderer", "", ""
"NewsCheckPidOfNewsRecordFailedInDetailActionEvent", "NewsController", "getNews()", "checkPidOfNewsRecordFailedInDetailAction"
"NewsDateMenuActionEvent", "NewsController", "getAssignedValues()", "dateMenuAction (NewsController::SIGNAL_NEWS_DATEMENU_ACTION)"
Expand Down
100 changes: 89 additions & 11 deletions Documentation/Tutorials/ExtendNews/Events/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ Several events can be used to modify the behaviour of EXT:news.

.. contents::
:local:
:depth: 1
:depth: 2

Connect to Event

Available events
----------------

Check out the :ref:`Events reference <referenceEvents>`.



Examples
--------

Provide more variables to the view
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To connect to an event, you need to register an event listener in your custom
extension. All what it needs is an entry in your
:file:`Configuration/Services.yaml` file:
Expand All @@ -27,9 +38,6 @@ extension. All what it needs is an entry in your
method: 'methodToConnectToEvent'
event: GeorgRinger\News\Event\NewsListActionEvent
Write your EventListener
------------------------

An example event listener can look like this:

.. code-block:: php
Expand All @@ -42,9 +50,6 @@ An example event listener can look like this:
use GeorgRinger\News\Event\NewsListActionEvent;
/**
* Use NewsListActionEvent from ext:news
*/
class YourListener
{
/**
Expand All @@ -60,7 +65,80 @@ An example event listener can look like this:
}
}
Available events
----------------
Check out the :ref:`Events reference <referenceEvents>`.
.. event_example_findDemanded:
\GeorgRinger\News\Event\ModifyDemandRepositoryEvent
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

This example 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 `Configuration/Services.yaml`:

.. code-block:: yaml
YourVendor\YourExtkey\EventListener\ModifyDemandRepositoryEventListener:
tags:
- name: event.listener
identifier: 'eventnews-modifydemandrepository'
event: GeorgRinger\News\Event\ModifyDemandRepositoryEvent
Now create the file ``Classes/EventListener/ModifyDemandRepositoryEventListener.php``:

.. code-block:: php
<?php
namespace YourVendor\YourExtkey\EventListener;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use GeorgRinger\News\Event\ModifyDemandRepositoryEvent
class ModifyDemandRepositoryEventListener
{
public function __invoke(ModifyDemandRepositoryEvent $event) {
$constraints = $event->getConstraints();
$constraints[] = $query->like('title', '%' . $subject . '%');
$event->setConstraints($constraints);
}
}
Controller/NewsController overrideSettings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Use this event to change the final settings which are for building queries, for the template, ...


This example modifies the settings by changing the category selection.

First, register your implementation in the file `Services/yaml`:

.. code-block:: yaml
YourVendor\YourExtkey\EventListener\NewsControllerOverrideSettingsEventListener:
tags:
- name: event.listener
identifier: 'eventnews-modifysettings'
event: GeorgRinger\News\Event\NewsControllerOverrideSettingsEvent
Now create the file ``Classes/EventListener/NewsControllerOverrideSettingsEvent.php``:

.. code-block:: php
<?php
namespace YourVendor\Extkey\EventListener;
class NewsControllerOverrideSettingsEvent
{
public function __invoke(\GeorgRinger\News\Event\NewsControllerOverrideSettingsEvent $event): array
{
$settings = $event->getSettings();
$settings['categories'] = '2,3';
$event->setSettings($settings);
}
}
102 changes: 6 additions & 96 deletions Documentation/Tutorials/ExtendNews/Hooks/Index.rst
Original file line number Diff line number Diff line change
@@ -1,98 +1,8 @@
.. _events:

======
Events
======

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

.. contents::
:local:
:depth: 1

Events
-----

.. event_example_findDemanded:
\GeorgRinger\News\Event\ModifyDemandRepositoryEvent
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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 `Configuration/Services.yaml`:

.. code-block:: yaml
YourVendor\YourExtkey\EventListener\ModifyDemandRepositoryEventListener:
tags:
- name: event.listener
identifier: 'eventnews-modifydemandrepository'
event: GeorgRinger\News\Event\ModifyDemandRepositoryEvent
Now create the file ``Classes/EventListener/ModifyDemandRepositoryEventListener.php``:

.. code-block:: php
<?php
namespace YourVendor\YourExtkey\EventListener;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use GeorgRinger\News\Event\ModifyDemandRepositoryEvent
class ModifyDemandRepositoryEventListener
{
public function __invoke(ModifyDemandRepositoryEvent $event) {
$constraints = $event->getConstraints();
$constraints[] = $query->like('title', '%' . $subject . '%');
$event->setConstraints($constraints);
}
}
Controller/NewsController overrideSettings
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Use this evebt to change the final settings which are for building queries, for the template, ...

Example
"""""""
This examples modifies the settings by changing the category selection.

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

.. code-block:: php
<?php
defined('TYPO3') or die();
$GLOBALS['TYPO3_CONF_VARS']['EXT']['news']['Controller/NewsController.php']['overrideSettings'][$_EXTKEY]
= \YourVendor\Extkey\Hooks\NewsControllerSettings::class . '->modify';
Now create the file ``Classes/Hooks/NewsControllerSettings.php``:

.. code-block:: php
<?php
namespace YourVendor\Extkey\Hooks;
class NewsControllerSettings
{
public function modify(array $params)
{
$settings = $params['originalSettings'];
$settings['categories'] = '2,3';
return $settings;
}
}
.. hint:: Please change the vendor and extension key to your real life code.
.. _hooks:

=====
Hooks
=====

ALl hooks of EXT:news have been deprecated and will be removed in next major versions.
Please use one of the available events instead.

0 comments on commit 4457696

Please sign in to comment.