0

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?

chicky
  • 103
  • 1
  • 9

2 Answers2

1

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

Georg Ringer
  • 7,779
  • 1
  • 16
  • 34
0

You can do something like,

# Add news title if on single view
lib.articleTitle = RECORDS
lib.articleTitle {
    if.isTrue.data = 251 # News PID. You can use GP:tx_news_pi1|news if you want
    dontCheckPid = 1
    tables = tx_news_domain_model_news
    source.data = 251 # News PID. You can use GP:tx_news_pi1|news if you want
    source.intval = 1
    conf.tx_news_domain_model_news = TEXT
    conf.tx_news_domain_model_news {
        field = title
        htmlSpecialChars = 1
    }
    wrap = |
}

And call lib object from fluid,

<f:cObject typoscriptObjectPath="lib.articleTitle" />

That's it!

Geee
  • 2,217
  • 15
  • 30