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

Add ModifyJsonContentObjectDataEvent #624

Open
alexander-bozung opened this issue Jul 18, 2023 · 1 comment
Open

Add ModifyJsonContentObjectDataEvent #624

alexander-bozung opened this issue Jul 18, 2023 · 1 comment

Comments

@alexander-bozung
Copy link

alexander-bozung commented Jul 18, 2023

It would be great to add a ModifyJsonContentObjectDataEvent in order to modify data in \FriendsOfTYPO3\Headless\ContentObject\JsonContentObject. Here is a patch that would add this Event to the current 4.x dev branch.

Subject: [PATCH] Headless v4 + TYPO3 v12: add ModifyJsonContentObjectDataEvent
---
Index: Classes/ContentObject/JsonContentObject.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Classes/ContentObject/JsonContentObject.php b/Classes/ContentObject/JsonContentObject.php
--- a/Classes/ContentObject/JsonContentObject.php	(revision d5821b400dceaee34264b17c7f3489a5b1841b7d)
+++ b/Classes/ContentObject/JsonContentObject.php	(date 1689667532763)
@@ -25,6 +25,7 @@
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Frontend\ContentObject\AbstractContentObject;
 use TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor;
+use Psr\EventDispatcher\EventDispatcherInterface;

 use function is_array;
 use function strpos;
@@ -47,6 +48,13 @@
         $this->headlessUserInt = GeneralUtility::makeInstance(HeadlessUserInt::class);
     }

+    private EventDispatcherInterface $eventDispatcher;
+
+    public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher): void
+    {
+        $this->eventDispatcher = $eventDispatcher;
+    }
+
     /**
      * Rendering the cObject, JSON
      * @param array $conf Array of TypoScript properties
@@ -69,6 +77,13 @@
             $data = $this->processFieldWithDataProcessing($conf);
         }

+        $event = $this->eventDispatcher->dispatch(
+            new \FriendsOfTYPO3\Headless\Event\ModifyJsonContentObjectDataEvent($conf, $data)
+        );
+        if (!empty($event->getData())) {
+            $data = $event->getData();
+        }
+
         $json = '';

         if (is_array($data)) {
Index: Classes/Event/ModifyJsonContentObjectDataEvent.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Classes/Event/ModifyJsonContentObjectDataEvent.php b/Classes/Event/ModifyJsonContentObjectDataEvent.php
new file mode 100644
--- /dev/null	(date 1689667797403)
+++ b/Classes/Event/ModifyJsonContentObjectDataEvent.php	(date 1689667797403)
@@ -0,0 +1,41 @@
+<?php
+
+declare(strict_types=1);
+
+namespace FriendsOfTYPO3\Headless\Event;
+
+/**
+ * \FriendsOfTYPO3\Headless\Event\ModifyJsonContentObjectDataEvent
+ */
+final class ModifyJsonContentObjectDataEvent
+{
+    public function __construct(
+        private array $conf,
+        private array $data,
+    ) {
+    }
+
+    /**
+     * @return array
+     */
+    public function getConf(): array
+    {
+        return $this->conf;
+    }
+
+    /**
+     * @return array
+     */
+    public function getData(): array
+    {
+        return $this->data;
+    }
+
+    /**
+     * @param array $data
+     */
+    public function setData(array $data): void
+    {
+        $this->data = $data;
+    }
+}
@tmotyl
Copy link
Collaborator

tmotyl commented Jul 18, 2023

@alexander-bozung Can you send it as a PR? It's easier to review there.
Please also elaborate more on the use case you have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants