From 44576967e800920d22bc8f8d1fe6ec6a9a1b1372 Mon Sep 17 00:00:00 2001 From: Georg Ringer Date: Fri, 22 Nov 2024 09:42:08 +0100 Subject: [PATCH] [FEATURE] Provide event NewsControllerOverrideSettingsEvent #2571 --- Classes/Controller/NewsController.php | 5 + .../NewsControllerOverrideSettingsEvent.php | 47 ++++++++ Documentation/Reference/Events/Index.rst | 3 + .../Tutorials/ExtendNews/Events/Index.rst | 100 +++++++++++++++-- .../Tutorials/ExtendNews/Hooks/Index.rst | 102 ++---------------- 5 files changed, 150 insertions(+), 107 deletions(-) create mode 100644 Classes/Event/NewsControllerOverrideSettingsEvent.php diff --git a/Classes/Controller/NewsController.php b/Classes/Controller/NewsController.php index 208781a4cb..17ddf9b157 100644 --- a/Classes/Controller/NewsController.php +++ b/Classes/Controller/NewsController.php @@ -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; @@ -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; } diff --git a/Classes/Event/NewsControllerOverrideSettingsEvent.php b/Classes/Event/NewsControllerOverrideSettingsEvent.php new file mode 100644 index 0000000000..e94ecfa47b --- /dev/null +++ b/Classes/Event/NewsControllerOverrideSettingsEvent.php @@ -0,0 +1,47 @@ +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; + } + +} diff --git a/Documentation/Reference/Events/Index.rst b/Documentation/Reference/Events/Index.rst index 348f7603d9..b60fb5be0e 100644 --- a/Documentation/Reference/Events/Index.rst +++ b/Documentation/Reference/Events/Index.rst @@ -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)" diff --git a/Documentation/Tutorials/ExtendNews/Events/Index.rst b/Documentation/Tutorials/ExtendNews/Events/Index.rst index ef8725fa57..ac420051ee 100644 --- a/Documentation/Tutorials/ExtendNews/Events/Index.rst +++ b/Documentation/Tutorials/ExtendNews/Events/Index.rst @@ -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 `. + + + +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: @@ -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 @@ -42,9 +50,6 @@ An example event listener can look like this: use GeorgRinger\News\Event\NewsListActionEvent; - /** - * Use NewsListActionEvent from ext:news - */ class YourListener { /** @@ -60,7 +65,80 @@ An example event listener can look like this: } } -Available events ----------------- -Check out the :ref:`Events reference `. +.. 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 + + 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 + + getSettings(); + $settings['categories'] = '2,3'; + + $event->setSettings($settings); + } + } diff --git a/Documentation/Tutorials/ExtendNews/Hooks/Index.rst b/Documentation/Tutorials/ExtendNews/Hooks/Index.rst index 8fbf84b258..e43d40a09c 100644 --- a/Documentation/Tutorials/ExtendNews/Hooks/Index.rst +++ b/Documentation/Tutorials/ExtendNews/Hooks/Index.rst @@ -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 - - 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 - - modify'; - -Now create the file ``Classes/Hooks/NewsControllerSettings.php``: - -.. code-block:: php - -