Skip to content

Instantly share code, notes, and snippets.

@rpflamm
Last active September 28, 2017 13:49
Show Gist options
  • Save rpflamm/c4c6f7fbf348701ffe794d79a0022ef3 to your computer and use it in GitHub Desktop.
Save rpflamm/c4c6f7fbf348701ffe794d79a0022ef3 to your computer and use it in GitHub Desktop.
<?php
namespace TGM\TgmLib\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;
/**
* GetRecordViewHelper
* Fetches a raw record from table=$table and uid=$uid.
* This ViewHelper returns the record if:
* - the table exist,
* - the uid exist and
* - the record was was not deleted (deleted = 0).
* Otherwise it'll return false.
*/
class GetRecordViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var boolean
*/
protected $escapeOutput = false;
/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('table', 'string', 'The table of the record.', false, 'tt_content');
$this->registerArgument('uid', 'int', 'The uid of the record.', true);
$this->registerArgument('as', 'string', 'The name of the variable.', false, 'record');
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
*
* @return mixed
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$record = BackendUtility::getRecord($arguments['table'], $arguments['uid']);
$templateVariableContainer = $renderingContext->getVariableProvider();
$templateVariableContainer->add($arguments['as'], $record);
$output = $renderChildrenClosure();
$templateVariableContainer->remove($arguments['as']);
return $output;
}
}
@rpflamm
Copy link
Author

rpflamm commented Sep 28, 2017

Usage:

<tmgLib:getRecord uid="123" table="tt_content">
Title: {record.title}
</tmbLib>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment