Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -27,24 +36,28 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would mention such information in the text not as a coment in the code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was by intention: Then it is to far away from the context. Ppl tend to just copy/paste without reading text above or below the code snippet. So, they copy the comment with it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they copy this comment they start wondering what a version switch is as they are then not in the context of the docs anymore

// 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'],
);
$output = [];
foreach ($result as $row) {
$output[] = $row['header'];
}
return implode('<br />', $output);
return implode('<br>', $output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions Documentation/ContentObjects/UserAndUserInt/_Hostname.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

declare(strict_types=1);

namespace Vendor\SitePackage\UserFunctions;
namespace MyVendor\SitePackage\UserFunctions;

use Psr\Http\Message\ServerRequestInterface;

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment contains no information beyond what the FQN would give you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also does not harm, Maybe not everyone knows what a ServerRequestInterface is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 2 request objects in TYPO3. The original TYPO3 Request also known als TYPO3_REQUEST and the Extbase Request (which is dying with each further TYPO3 version). What about:

Suggested change
* @param ServerRequestInterface $request The current PSR-7 request object
* @param ServerRequestInterface $request The TYPO3 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() ?: '';
}
Expand Down
Loading