From 0f745b3c45b4c62b8bafe1970be81c1fc4e54739 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Chris=20M=C3=BCller?=
<2566282+brotkrueml@users.noreply.github.com>
Date: Mon, 9 Sep 2024 09:41:51 +0200
Subject: [PATCH] [TASK] Modernize cObject "USER" examples
- Avoid GeneralUtility::makeInstance(), use DI instead (has to be set public therefore)
- Use newly introduced request attribute to retrieve current page ID as best practise
Releases: main
---
.../UserAndUserInt/_ExampleListRecords.php | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)
diff --git a/Documentation/ContentObjects/UserAndUserInt/_ExampleListRecords.php b/Documentation/ContentObjects/UserAndUserInt/_ExampleListRecords.php
index 165faec9f..56a27f9ef 100644
--- a/Documentation/ContentObjects/UserAndUserInt/_ExampleListRecords.php
+++ b/Documentation/ContentObjects/UserAndUserInt/_ExampleListRecords.php
@@ -5,15 +5,24 @@
namespace MyVendor\SitePackage\UserFunctions;
use Psr\Http\Message\ServerRequestInterface;
+use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use TYPO3\CMS\Core\Database\ConnectionPool;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
/**
* Example of a method in a PHP class to be called from TypoScript
+ *
+ * The class is defined as public as we use dependency injection in
+ * this example. If you do not need dependency injection, the
+ * "Autoconfigure" attribute should be omitted!
*/
+#[Autoconfigure(public: true)]
final class ExampleListRecords
{
+ public function __construct(
+ private readonly ConnectionPool $connectionPool,
+ ) {}
+
/**
* Reference to the parent (calling) cObject set from TypoScript
*/
@@ -33,11 +42,11 @@ public function setContentObjectRenderer(ContentObjectRenderer $cObj): void
*/
public function listContentRecordsOnPage(string $content, array $conf, ServerRequestInterface $request): string
{
- $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tt_content');
+ $connection = $this->connectionPool->getConnectionForTable('tt_content');
$result = $connection->select(
['header'],
'tt_content',
- ['pid' => (int)$GLOBALS['TSFE']->id],
+ ['pid' => $request->getAttribute('frontend.page.information')->getId()],
[],
['sorting' => $conf['reverseOrder'] ? 'DESC' : 'ASC'],
);
@@ -45,6 +54,6 @@ public function listContentRecordsOnPage(string $content, array $conf, ServerReq
foreach ($result as $row) {
$output[] = $row['header'];
}
- return implode('
', $output);
+ return implode('
', $output);
}
}