Search code examples
typo3typoscriptfluidtypo3-9.x

Retrieve content element field from within a plugin template?


I am modifying the template of a plugin, and I want to retrieve a field from the content element.

Using f:debug I see the only data available is from the plugin itself, and none from the content element.

Is there any way I can perhaps insert the field I need in the plugin settings?

eg. something like:

plugin.tx_plugin.settings {
    contentUid = TEXT
    contentUid.field = uid
}

Solution

  • The best way I can think of to do this is with a custom ViewHelper. Something like:

    namespace MyVendor\MyExtension\ViewHelpers;
    
    use TYPO3\CMS\Core\Utility\GeneralUtility;
    use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
    use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
    use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
    
    class ContentUidViewHelper extends AbstractViewHelper
    {
        public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
        {
            $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
            return $configurationManager->getContentObject()->data['uid'];
        }
    }
    

    In your Fluid template:

    <mynamespace:contentUid />
    

    This will get the uid of the content element, but you can get any field this way. Just change the key of the data array to the field you need.