Project

General

Profile

Bug #57272 » FalTranslationFixViewHelper.php

Kai Tallafus, 2017-04-16 16:34

 
<?php
namespace MyVendor\MyExt\ViewHelpers;

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Frontend\Resource\FileCollector;

class FalTranslationFixViewHelper extends AbstractViewHelper
{

protected $escapeOutput = false;

/**
* @param mixed $record
* @param string $tableName
* @param string $relationFieldName
* @param string $as
*
* @return void
*/
public function render($record, $tableName, $relationFieldName, $as)
{
$fileCollector = GeneralUtility::makeInstance(FileCollector::class);

if ( $record instanceof AbstractDomainObject ) {
$rawRecord = $this->getRawRecord($record, $tableName);
} elseif ( is_array($record) ) {
$rawRecord = $record;
} else {
throw new \UnexpectedValueException('Supplied record must either be an AbstractDomainObject or an array.');
}

$fileCollector->addFilesFromRelation($tableName, $relationFieldName, $rawRecord);

$result = $fileCollector->getFiles();

$this->templateVariableContainer->add($as, $result);
$output = $this->renderChildren();
$this->templateVariableContainer->remove($as);

return $output;
}

protected function getRawRecord($recordModel, $tableName) {

$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($tableName);

$rawRecord = $queryBuilder->select('*')
->from($tableName)
->where(
$queryBuilder->expr()
->eq('uid',
$recordModel->_getProperty('_localizedUid') ? : $recordModel->getUid())
)
->execute()
->fetch();

return $rawRecord;
}
}
(2-2/3)