Skip to content

[Backport 13.4] [TASK] Use request attribute instead of TSFE in page title API example #4931

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

Merged
merged 1 commit into from
Oct 28, 2024
Merged
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
4 changes: 4 additions & 0 deletions Documentation/ApiOverview/Seo/PageTitleApi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ example the site title, you can implement a page title provider as follows:
.. literalinclude:: _ExampleWebsiteTitle/_WebsiteTitleProvider.php
:caption: EXT:my_sitepackage/Classes/PageTitle/WebsiteTitleProvider.php

.. versionchanged:: 13.0
The :ref:`frontend.page.information attribute <typo3-request-attribute-frontend-page-information>`
has been introduced.

As we need to :ref:`inject <DependencyInjection>` the class :php:`SiteFinder`
to retrieve the current site configuration, we must make the new page title
provider :ref:`public <knowing-what-to-make-public>`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@

namespace MyVendor\MySitepackage\PageTitle;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\PageTitle\PageTitleProviderInterface;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Page\PageInformation;

final class WebsiteTitleProvider implements PageTitleProviderInterface
final readonly class WebsiteTitleProvider implements PageTitleProviderInterface
{
public function __construct(
private readonly SiteFinder $siteFinder,
private SiteFinder $siteFinder,
) {}

public function getTitle(): string
{
$site = $this->siteFinder->getSiteByPageId($this->getTypoScriptFrontendController()->page['uid']);
/** @var PageInformation $pageInformation */
$pageInformation = $this->getRequest()->getAttribute('frontend.page.information');

$site = $this->siteFinder->getSiteByPageId($pageInformation->getId());
$titles = [
$this->getTypoScriptFrontendController()->page['title'],
$pageInformation->getPageRecord()['title'],
$site->getAttribute('websiteTitle'),
];

// do something
return implode(' - ', $titles);
}

private function getTypoScriptFrontendController(): TypoScriptFrontendController
private function getRequest(): ServerRequestInterface
{
return $GLOBALS['TSFE'];
return $GLOBALS['TYPO3_REQUEST'];
}
}
Loading