Last active
September 28, 2017 13:49
-
-
Save rpflamm/c4c6f7fbf348701ffe794d79a0022ef3 to your computer and use it in GitHub Desktop.
Modification of http://blog.teamgeist-medien.de/2017/08/1703.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: