TYPO3 CMS  TYPO3_6-2
FluidTemplateContentObject.php
Go to the documentation of this file.
1 <?php
3 
18 
29 
33  protected $view = NULL;
34 
61  public function render($conf = array()) {
62  $parentView = $this->view;
64 
65  if (!is_array($conf)) {
66  $conf = array();
67  }
68 
69  $this->setTemplate($conf);
70  $this->setLayoutRootPath($conf);
71  $this->setPartialRootPath($conf);
72  $this->setFormat($conf);
73  $this->setExtbaseVariables($conf);
74  $this->assignSettings($conf);
75  $this->assignContentObjectVariables($conf);
77 
78  $content = $this->renderFluidView();
79  $content = $this->applyStandardWrapToRenderedContent($content, $conf);
80 
81  $this->view = $parentView;
82  return $content;
83  }
84 
94  protected function initializeStandaloneViewInstance() {
95  $this->view = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Fluid\\View\\StandaloneView');
96  }
97 
105  protected function setTemplate(array $conf) {
106  // Fetch the Fluid template
107  if (!empty($conf['template']) && !empty($conf['template.'])) {
108  $templateSource = $this->cObj->cObjGetSingle($conf['template'], $conf['template.']);
109  $this->view->setTemplateSource($templateSource);
110  } else {
111  $file = isset($conf['file.']) ? $this->cObj->stdWrap($conf['file'], $conf['file.']) : $conf['file'];
113  $templateService = $GLOBALS['TSFE']->tmpl;
114  $templatePathAndFilename = $templateService->getFileName($file);
115  $this->view->setTemplatePathAndFilename(PATH_site . $templatePathAndFilename);
116  }
117  }
118 
125  protected function setLayoutRootPath(array $conf) {
126  // Override the default layout path via typoscript
127  $layoutPaths = array();
128  if (isset($conf['layoutRootPath']) || isset($conf['layoutRootPath.'])) {
129  $layoutRootPath = isset($conf['layoutRootPath.'])
130  ? $this->cObj->stdWrap($conf['layoutRootPath'], $conf['layoutRootPath.'])
131  : $conf['layoutRootPath'];
132  $layoutPaths[] = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($layoutRootPath);
133  }
134  if (isset($conf['layoutRootPaths.'])) {
135  foreach ($conf['layoutRootPaths.'] as $key => $path) {
136  $layoutPaths = array_replace($layoutPaths, $this->applyStandardWrapToFluidPaths($conf['layoutRootPaths.']));
137  }
138  }
139  if (!empty($layoutPaths)) {
140  $this->view->setLayoutRootPaths($layoutPaths);
141  }
142  }
143 
150  protected function setPartialRootPath(array $conf) {
151  $partialPaths = array();
152  if (isset($conf['partialRootPath']) || isset($conf['partialRootPath.'])) {
153  $partialRootPath = isset($conf['partialRootPath.'])
154  ? $this->cObj->stdWrap($conf['partialRootPath'], $conf['partialRootPath.'])
155  : $conf['partialRootPath'];
156  $partialPaths[] = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($partialRootPath);
157  }
158  if (isset($conf['partialRootPaths.'])) {
159  foreach ($conf['partialRootPaths.'] as $key => $path) {
160  $partialPaths = array_replace($partialPaths, $this->applyStandardWrapToFluidPaths($conf['partialRootPaths.']));
161  }
162  }
163  if (!empty($partialPaths)) {
164  $this->view->setPartialRootPaths($partialPaths);
165  }
166  }
167 
174  protected function setFormat(array $conf) {
175  $format = isset($conf['format.']) ? $this->cObj->stdWrap($conf['format'], $conf['format.']) : $conf['format'];
176  if ($format) {
177  $this->view->setFormat($format);
178  }
179  }
180 
187  protected function setExtbaseVariables(array $conf) {
189  $requestPluginName = isset($conf['extbase.']['pluginName.']) ? $this->cObj->stdWrap($conf['extbase.']['pluginName'], $conf['extbase.']['pluginName.']) : $conf['extbase.']['pluginName'];
190  if ($requestPluginName) {
191  $this->view->getRequest()->setPluginName($requestPluginName);
192  }
193  $requestControllerExtensionName = isset($conf['extbase.']['controllerExtensionName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerExtensionName'], $conf['extbase.']['controllerExtensionName.']) : $conf['extbase.']['controllerExtensionName'];
194  if ($requestControllerExtensionName) {
195  $this->view->getRequest()->setControllerExtensionName($requestControllerExtensionName);
196  }
197  $requestControllerName = isset($conf['extbase.']['controllerName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerName'], $conf['extbase.']['controllerName.']) : $conf['extbase.']['controllerName'];
198  if ($requestControllerName) {
199  $this->view->getRequest()->setControllerName($requestControllerName);
200  }
201  $requestControllerActionName = isset($conf['extbase.']['controllerActionName.']) ? $this->cObj->stdWrap($conf['extbase.']['controllerActionName'], $conf['extbase.']['controllerActionName.']) : $conf['extbase.']['controllerActionName'];
202  if ($requestControllerActionName) {
203  $this->view->getRequest()->setControllerActionName($requestControllerActionName);
204  }
205  }
206 
214  protected function assignContentObjectVariables(array $conf) {
215  $reservedVariables = array('data', 'current');
216  // Accumulate the variables to be replaced and loop them through cObjGetSingle
217  $variables = (array)$conf['variables.'];
218  foreach ($variables as $variableName => $cObjType) {
219  if (is_array($cObjType)) {
220  continue;
221  }
222  if (!in_array($variableName, $reservedVariables)) {
223  $this->view->assign(
224  $variableName,
225  $this->cObj->cObjGetSingle($cObjType, $variables[$variableName . '.'])
226  );
227  } else {
228  throw new \InvalidArgumentException(
229  'Cannot use reserved name "' . $variableName . '" as variable name in FLUIDTEMPLATE.',
230  1288095720
231  );
232  }
233  }
234  }
235 
243  protected function assignSettings(array $conf) {
244  if (isset($conf['settings.'])) {
246  $typoScriptService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Service\\TypoScriptService');
247  $settings = $typoScriptService->convertTypoScriptArrayToPlainArray($conf['settings.']);
248  $this->view->assign('settings', $settings);
249  }
250  }
251 
258  protected function assignContentObjectDataAndCurrent(array $conf) {
259  $this->view->assign('data', $this->cObj->data);
260  $this->view->assign('current', $this->cObj->data[$this->cObj->currentValKey]);
261  }
262 
268  protected function renderFluidView() {
269  return $this->view->render();
270  }
271 
279  protected function applyStandardWrapToRenderedContent($content, array $conf) {
280  if (isset($conf['stdWrap.'])) {
281  $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
282  }
283  return $content;
284  }
285 
293  protected function applyStandardWrapToFluidPaths(array $paths) {
294  $finalPaths = array();
295  foreach ($paths as $key => $path) {
296  if (StringUtility::isLastPartOfString($key, '.')) {
297  if (isset($paths[substr($key, 0, -1)])) {
298  continue;
299  }
300  $path = $this->cObj->stdWrap('', $path);
301  } elseif (isset($paths[$key . '.'])) {
302  $path = $this->cObj->stdWrap($path, $paths[$key . '.']);
303  }
304  $finalPaths[$key] = GeneralUtility::getFileAbsFileName($path);
305  }
306  return $finalPaths;
307  }
308 
309 }
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static isLastPartOfString($haystack, $needle)
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)