TYPO3 PageTitleProvider for own extensions
Creating custom PageTitleProviders for TYPO3. Use cases are extensions that use a single page for displaying different content like single views of news or blog posts.
Procedure:
Create a class MyPageTitleProvider.php in myextension/Classes/PageTitle:
<?php namespace Rms\Blog; use TYPO3\CMS\Core\PageTitle\AbstractPageTitleProvider; class MyPageTitleProvider extends AbstractPageTitleProvider { /** * @param string $title */ public function setTitle(string $title) { $this->title = $title; } }
After that, the PageTitleProvider and, if there are several providers, the priority are configured in TypoScript
config.pageTitleProviders { custom { provider = Rms\Blog\MyPageTitleProvider before = pages after = altPageTitle } }
Now the new PageTitleProvider can be used e.g. inside a show() function to override the default page title:
<?php class PostController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController { /** * action show * * @param \Rms\Blog\Domain\Model\Post $post * @return \Psr\Http\Message\ResponseInterface */ public function showAction(\Rms\Blog\Domain\Model\Post $post): \Psr\Http\Message\ResponseInterface { $titleProvider = GeneralUtility::makeInstance(\Rms\Blog\MyPageTitleProvider::class); $titleProvider->setTitle($post->getTitle()); $this->view->assign('post', $post); return $this->htmlResponse(); } }