Skip to content

Instantly share code, notes, and snippets.

@denyskoch
Created February 28, 2014 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denyskoch/9268181 to your computer and use it in GitHub Desktop.
Save denyskoch/9268181 to your computer and use it in GitHub Desktop.
ViewHelper for merging only if one path is not set ...
<?php
/**
* Converts raw flexform xml into an associative array
*
*/
class Tx_SomeExt_ViewHelpers_DataViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
/**
* @var array
*/
private static $dataCache = array();
/**
* @var Tx_Flux_Service_FluxService
*/
protected $configurationService;
/**
* Inject Flux service
* @param Tx_Flux_Service_FluxService $configurationService
* @return void
*/
public function injectConfigurationService(Tx_Flux_Service_FluxService $configurationService) {
$this->configurationService = $configurationService;
}
/**
* Render method
* @param integer $uid
* @param string $path
* @throws Tx_Fluid_Core_ViewHelper_Exception
* @return array
*/
public function render($uid, $path) {
$table = $this->templateVariableContainer->get('fluxTableName');
$field = $this->templateVariableContainer->get('fluxRecordField');
if (TRUE === isset(self::$dataCache[$uid.$table.$field])) {
return self::$dataCache[$uid.$table.$field];
}
$row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('uid, pid, is_siteroot,' . $field, $table, sprintf('uid=%d', $uid));
if (FALSE === $row) {
throw new Tx_Fluid_Core_ViewHelper_Exception(sprintf('Either table "%s", field "%s" or record with uid %d do not exist.', $table, $field, $uid), 1358679983);
}
$dataArray = $this->configurationService->convertFlexFormContentToArray($row[$field]);
$value = $this->getValue($dataArray, $path);
if(NULL !== $value) {
$dataArray = $value;
}
else {
if($row['pid'] > 0 && $row['is_siteroot'] == 0) {
$dataArray = $this->render($row['pid'], $path);
}
}
self::$dataCache[$uid.$table.$field] = $dataArray;
return $dataArray;
}
protected function getValue($dataArray, $path) {
if (strpos($path, '.') === FALSE) {
$value = isset($dataArray[$path]) ? $dataArray[$path] : NULL;
} else {
$parts = explode('.', $path);
$first = array_shift($parts);
if(isset($dataArray[$first])) {
$value = Tx_Extbase_Reflection_ObjectAccess::getPropertyPath($dataArray[$first], implode('.', $parts));
}
}
return $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment