From b3ed72624d6efbd06517daa899ed91d4fc31d73d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Chris=20M=C3=BCller?=
<2566282+brotkrueml@users.noreply.github.com>
Date: Sun, 8 Sep 2024 14:33:19 +0200
Subject: [PATCH 1/2] [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
- Add request where missing as argument and improve docblock
- Streamline namespace to be in line with other examples
Releases: main
---
.../UserAndUserInt/_ExampleListRecords.php | 25 ++++++++++++++-----
.../UserAndUserInt/_ExampleTime.php | 2 +-
.../UserAndUserInt/_Hostname.php | 7 ++++--
3 files changed, 25 insertions(+), 9 deletions(-)
diff --git a/Documentation/ContentObjects/UserAndUserInt/_ExampleListRecords.php b/Documentation/ContentObjects/UserAndUserInt/_ExampleListRecords.php
index 165faec9f..4f7e60398 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
*/
@@ -27,17 +36,21 @@ public function setContentObjectRenderer(ContentObjectRenderer $cObj): void
/**
* List the headers of the content elements on the page
*
- * @param string Empty string (no content to process)
- * @param array TypoScript configuration
+ * @param string Empty string (no content to process)
+ * @param array TypoScript configuration
+ * @param ServerRequestInterface The current PSR-7 request object
* @return string HTML output, showing content elements (in reverse order, if configured)
*/
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],
+ // The request attribute 'frontend.page.information' has been introduced
+ // with TYPO3 v13. For compatibility with older TYPO3 versions use the
+ // version switch on the top of the page.
+ ['pid' => $request->getAttribute('frontend.page.information')->getId()],
[],
['sorting' => $conf['reverseOrder'] ? 'DESC' : 'ASC'],
);
@@ -45,6 +58,6 @@ public function listContentRecordsOnPage(string $content, array $conf, ServerReq
foreach ($result as $row) {
$output[] = $row['header'];
}
- return implode('
', $output);
+ return implode('
', $output);
}
}
diff --git a/Documentation/ContentObjects/UserAndUserInt/_ExampleTime.php b/Documentation/ContentObjects/UserAndUserInt/_ExampleTime.php
index 33e33506c..5fa7d3351 100644
--- a/Documentation/ContentObjects/UserAndUserInt/_ExampleTime.php
+++ b/Documentation/ContentObjects/UserAndUserInt/_ExampleTime.php
@@ -13,7 +13,7 @@ final class ExampleTime
*
* @param string Empty string (no content to process)
* @param array TypoScript configuration
- * @param ServerRequestInterface $request
+ * @param ServerRequestInterface $request The current PSR-7 request object
* @return string HTML output, showing the current server time.
*/
public function printTime(string $content, array $conf, ServerRequestInterface $request): string
diff --git a/Documentation/ContentObjects/UserAndUserInt/_Hostname.php b/Documentation/ContentObjects/UserAndUserInt/_Hostname.php
index e20bece27..b34f05506 100644
--- a/Documentation/ContentObjects/UserAndUserInt/_Hostname.php
+++ b/Documentation/ContentObjects/UserAndUserInt/_Hostname.php
@@ -2,7 +2,9 @@
declare(strict_types=1);
-namespace Vendor\SitePackage\UserFunctions;
+namespace MyVendor\SitePackage\UserFunctions;
+
+use Psr\Http\Message\ServerRequestInterface;
final class Hostname
{
@@ -11,9 +13,10 @@ final class Hostname
*
* @param string Empty string (no content to process)
* @param array TypoScript configuration
+ * @param ServerRequestInterface $request The current PSR-7 request object
* @return string HTML result
*/
- public function getHostname(string $content, array $conf): string
+ public function getHostname(string $content, array $conf, ServerRequestInterface $request): string
{
return gethostname() ?: '';
}
From 84af05bdfc38c92bd077bf986df1181f2d3149ad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Chris=20M=C3=BCller?=
<2566282+brotkrueml@users.noreply.github.com>
Date: Sun, 8 Sep 2024 14:41:57 +0200
Subject: [PATCH 2/2] Adjust spaces in docblock
---
Documentation/ContentObjects/UserAndUserInt/_Hostname.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/Documentation/ContentObjects/UserAndUserInt/_Hostname.php b/Documentation/ContentObjects/UserAndUserInt/_Hostname.php
index b34f05506..0566b3088 100644
--- a/Documentation/ContentObjects/UserAndUserInt/_Hostname.php
+++ b/Documentation/ContentObjects/UserAndUserInt/_Hostname.php
@@ -11,8 +11,8 @@ final class Hostname
/**
* Return standard host name for the local machine
*
- * @param string Empty string (no content to process)
- * @param array TypoScript configuration
+ * @param string Empty string (no content to process)
+ * @param array TypoScript configuration
* @param ServerRequestInterface $request The current PSR-7 request object
* @return string HTML result
*/