Last active
September 11, 2019 09:01
-
-
Save bmoex/bc05aefdbc0bf2132d7b40ed83d356e1 to your computer and use it in GitHub Desktop.
The PageTitle API of TYPO3 CMS: how to use it in a ViewHelper?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.pageTitleProviders { | |
viewhelper { | |
provider = My\Extension\PageTitle\ViewHelperPageTitleProvider | |
before = altPageTitle | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\Extension\ViewHelpers; | |
use Closure; | |
use My\Extension\PageTitle\ViewHelperPageTitleProvider; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; | |
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; | |
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic; | |
/** | |
* ViewHelper for <title> header tag | |
* == Example == | |
* <n:titleTag>{item.title}</n:titleTag> | |
*/ | |
final class TitleTagViewHelper extends AbstractViewHelper | |
{ | |
use CompileWithRenderStatic; | |
/** | |
* Configure title through TitleProvider | |
* | |
* @param array $arguments | |
* @param \Closure $renderChildrenClosure | |
* @param \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext | |
* @return void | |
*/ | |
public static function renderStatic( | |
array $arguments, | |
Closure $renderChildrenClosure, | |
RenderingContextInterface $renderingContext | |
): void { | |
$content = trim($renderChildrenClosure()); | |
if (!empty($content)) { | |
GeneralUtility::makeInstance(ViewHelperPageTitleProvider::class) | |
->setTitle($content); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace My\Extension\PageTitle; | |
use TYPO3\CMS\Core\PageTitle\AbstractPageTitleProvider; | |
/** | |
* Used in config.pageTitleProviders | |
*/ | |
final class ViewHelperPageTitleProvider extends AbstractPageTitleProvider | |
{ | |
/** | |
* @param string $title | |
* @return void | |
*/ | |
public function setTitle(string $title): void | |
{ | |
$this->title = $title; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment