How can I display the title of a tx_news tag in any fluid template if only the id and the pid of the tag is given?
The best option is to use a ViewHelper
<?php
declare(strict_types=1);
namespace Vendor\Package\ViewHelpers;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
class DbViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var bool
*/
protected $escapeOutput = false;
/**
* Initialize arguments
*/
public function initializeArguments(): void
{
$this->registerArgument(
'table',
'string',
'Table (Foreign table)',
true
);
$this->registerArgument(
'id',
'int',
'ID ',
true
);
$this->registerArgument(
'as',
'string',
'This parameter specifies the name of the variable that will be used for the returned ' .
'ViewHelper result.',
true
);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$templateVariableContainer = $renderingContext->getVariableProvider();
$row = BackendUtility::getRecord($arguments['table'], $arguments['id']);
$templateVariableContainer->add($arguments['as'], $row);
$content = $renderChildrenClosure();
$templateVariableContainer->remove($arguments['as']);
return $content;
}
}
And then you can use
<vendor:db table="tx_news_domain_model_tag" id="123" as="tag">
<strong>{tag.title}</strong>
</vendor:db>
Don't forget to register the namespace in the template