Created
March 19, 2013 10:01
-
-
Save bwaidelich/5194916 to your computer and use it in GitHub Desktop.
Custom "Flexible Content Element" renderer based on Fluid, supporting Flexforms etc.
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 | |
| /** | |
| * Renders a Fluidtemplate similar to the FLUIDTEMPLATE content object but with some additional features: | |
| * * $this->cObj->data variables will be available directly in the template (no need to prefix with "data." | |
| * * Flexform fields are parsed and all Flexform variables will be directly available in the template | |
| * | |
| * Usage: | |
| * | |
| * lib.someObject = USER | |
| * lib.someObject { | |
| * userFunc = Tx_YourSiteExtension_Content_FceRenderer->render | |
| * file = path/to/fluid/template | |
| * flexformFields = pi_flexform, some_other_flexformfield | |
| * variables { | |
| * foo = TEXT | |
| * foo.value = This variable overrides cObj->data[foo] and flexform variables named "foo" | |
| * } | |
| * } | |
| */ | |
| class Tx_YourSiteExtension_Content_FceRenderer { | |
| /** | |
| * current cObject | |
| * Note: This must be public cause it is set by tslib_cObj::callUserFunction() | |
| * | |
| * @var tslib_cObj | |
| */ | |
| public $cObj; | |
| /** | |
| * @param string $content | |
| * @param array $configuration | |
| * @return string | |
| */ | |
| public function render($content, array $configuration) { | |
| if (!t3lib_extMgm::isLoaded('fluid')) { | |
| return 'You need to install "Fluid" in order to use the FLUIDTEMPLATE content element'; | |
| } | |
| /** | |
| * 1. initializing Fluid StandaloneView and setting configuration parameters | |
| **/ | |
| /** @var $view Tx_Fluid_View_StandaloneView */ | |
| $view = t3lib_div::makeInstance('Tx_Fluid_View_StandaloneView'); | |
| // fetch the Fluid template | |
| $file = isset($configuration['file.']) | |
| ? $this->cObj->stdWrap($configuration['file'], $configuration['file.']) | |
| : $configuration['file']; | |
| $templatePathAndFilename = $GLOBALS['TSFE']->tmpl->getFileName($file); | |
| $view->setTemplatePathAndFilename($templatePathAndFilename); | |
| // override the default layout path via typoscript | |
| $layoutRootPath = isset($configuration['layoutRootPath.']) | |
| ? $this->cObj->stdWrap($configuration['layoutRootPath'], $configuration['layoutRootPath.']) | |
| : $configuration['layoutRootPath']; | |
| if($layoutRootPath) { | |
| $layoutRootPath = t3lib_div::getFileAbsFileName($layoutRootPath); | |
| $view->setLayoutRootPath($layoutRootPath); | |
| } | |
| // override the default partials path via typoscript | |
| $partialRootPath = isset($configuration['partialRootPath.']) | |
| ? $this->cObj->stdWrap($configuration['partialRootPath'], $configuration['partialRootPath.']) | |
| : $configuration['partialRootPath']; | |
| if($partialRootPath) { | |
| $partialRootPath = t3lib_div::getFileAbsFileName($partialRootPath); | |
| $view->setPartialRootPath($partialRootPath); | |
| } | |
| // override the default format | |
| $format = isset($configuration['format.']) | |
| ? $this->cObj->stdWrap($configuration['format'], $configuration['format.']) | |
| : $configuration['format']; | |
| if ($format) { | |
| $view->setFormat($format); | |
| } | |
| // set some default variables for initializing Extbase | |
| $requestPluginName = isset($configuration['extbase.']['pluginName.']) | |
| ? $this->cObj->stdWrap($configuration['extbase.']['pluginName'], $configuration['extbase.']['pluginName.']) | |
| : $configuration['extbase.']['pluginName']; | |
| if($requestPluginName) { | |
| $view->getRequest()->setPluginName($requestPluginName); | |
| } | |
| $requestControllerExtensionName = isset($configuration['extbase.']['controllerExtensionName.']) | |
| ? $this->cObj->stdWrap($configuration['extbase.']['controllerExtensionName'], $configuration['extbase.']['controllerExtensionName.']) | |
| : $configuration['extbase.']['controllerExtensionName']; | |
| if($requestControllerExtensionName) { | |
| $view->getRequest()->setControllerExtensionName($requestControllerExtensionName); | |
| } | |
| $requestControllerName = isset($configuration['extbase.']['controllerName.']) | |
| ? $this->cObj->stdWrap($configuration['extbase.']['controllerName'], $configuration['extbase.']['controllerName.']) | |
| : $configuration['extbase.']['controllerName']; | |
| if($requestControllerName) { | |
| $view->getRequest()->setControllerName($requestControllerName); | |
| } | |
| $requestControllerActionName = isset($configuration['extbase.']['controllerActionName.']) | |
| ? $this->cObj->stdWrap($configuration['extbase.']['controllerActionName'], $configuration['extbase.']['controllerActionName.']) | |
| : $configuration['extbase.']['controllerActionName']; | |
| if($requestControllerActionName) { | |
| $view->getRequest()->setControllerActionName($requestControllerActionName); | |
| } | |
| /** | |
| * 2. variable assignment | |
| */ | |
| // accumulate the variables to be replaced | |
| // and loop them through cObjGetSingle | |
| $variables = $this->cObj->data; | |
| $flexformFields = isset($configuration['flexformFields.']) | |
| ? $this->cObj->stdWrap($configuration['flexformFields'], $configuration['flexformFields.']) | |
| : $configuration['flexformFields']; | |
| if ($flexformFields) { | |
| foreach (t3lib_div::trimExplode(',', $flexformFields) as $flexformField) { | |
| $flexformVariables = $this->parseFlexformData($variables[$flexformField]); | |
| $variables = array_merge($variables, $flexformVariables); | |
| } | |
| } | |
| $additionalVariables = (array) $configuration['variables.']; | |
| foreach ($additionalVariables as $variableName => $cObjType) { | |
| if (is_array($cObjType)) { | |
| continue; | |
| } | |
| $variables[$variableName] = $this->cObj->cObjGetSingle($cObjType, $variables[$variableName . '.']); | |
| } | |
| $view->assignMultiple($variables); | |
| /** | |
| * 3. render the content | |
| */ | |
| $result = $view->render(); | |
| if(isset($configuration['stdWrap.'])) { | |
| $result = $this->cObj->stdWrap($result, $configuration['stdWrap.']); | |
| } | |
| return $result; | |
| } | |
| /** | |
| * decodes the given flexform string and returns an array with *all* values (vDEF) independent of the flexform sheet | |
| * Note: Duplicate variable names will be overridden! | |
| * | |
| * @param string $flexformData | |
| * @return array | |
| */ | |
| protected function parseFlexformData($flexformData) { | |
| $flexformArray = t3lib_div::xml2array($flexformData); | |
| if (!is_array($flexformArray['data'])) { | |
| return array(); | |
| } | |
| $result = array(); | |
| foreach ($flexformArray['data'] as $sheets) { | |
| foreach ($sheets as $sheetData) { | |
| foreach ($sheetData as $variableName => $valueDefinition) { | |
| $result[$variableName] = $valueDefinition['vDEF']; | |
| } | |
| } | |
| } | |
| return $result; | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment