Retrieve content element field from within a plugin template?

Viewed 189

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
}
3 Answers

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.

In the corresponding method (like the listAction or showAction) of the controller you can get the data of the content element in the following way:

$contentObject = $this->configurationManager->getContentObject();
$this->view->assign('contentObjectData', $contentObject->data);

As far as I know, you can't get to that data using typoscript, but I've never needed it anyway since I've been using the above code in the controller.

settings do not have stdWrap-type per default, but only string. So you can not use cObjects as values.

For one (or a few) settings, you could do the stdWrap-processing in your method/class yourself:

$settingsAsTypoScriptArray = $this->objectManager->get(TypoScriptService::class)->convertPlainArrayToTypoScriptArray($this->settings);
$contentObj = $this->configurationManager->getContentObject();
if ($contentObj === null) {
  $contentObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
}
// now override the values in the settings array with the processed value
$contentUid = (int)$contentObj->stdWrap($settingsAsTypoScriptArray['contentUid'], $settingsAsTypoScriptArray['contentUid.']);

If you wanna have many settings to be stdWraped, have a look into EXT:news. Georg implemented an interesting concept via useStdWrap configuration.

Related