Skip to content

Instantly share code, notes, and snippets.

@s2b
Last active August 12, 2016 15:50
Show Gist options
  • Save s2b/d8fc140537e56ef7359c724f39bb1705 to your computer and use it in GitHub Desktop.
Save s2b/d8fc140537e56ef7359c724f39bb1705 to your computer and use it in GitHub Desktop.
Class that implements templateRootPaths as well as substitution of “EXT:” in partial/layout rootPaths to Fluid StandaloneView in TYPO3 < 7.3
<?php
namespace VENDOR\MyExtension\Utility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Extbase\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class StandaloneViewUtility
{
/**
* Object Manager
*
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
* @inject
*/
protected $objectManager;
/**
* Creates a standalone view that works across different TYPO3
* versions
*
* @param string $template template name
* @param array|string $templateRootPaths path(s) from which the
* template should be obtained
* @param array|string $partialRootPaths path(s) from which partials
* should be obtained
* @param array|string $layoutRootPaths path(s) from which layouts
* should be obtained
* @param string|null $format overwrites default view format
* (html) if defined
* @param string|null $extensionName sets up translation capability
* for the specified extension
* context if defined
* @return \TYPO3\CMS\Fluid\View\StandaloneView
*/
public function createView(
$template,
$templateRootPaths,
$partialRootPaths = array(),
$layoutRootPaths = array(),
$format = null,
$extensionName = null
) {
$templateRootPaths = (array) $templateRootPaths;
$partialRootPaths = (array) $partialRootPaths;
$layoutRootPaths = (array) $layoutRootPaths;
// Creates the standalone view object
$view = $this->objectManager->get(StandaloneView::class);
// Sets view format
if (isset($format)) {
$view->setFormat($format);
}
//
// Differentiate between TYPO3 versions because older versions don't
// support templateRootPaths
//
if (version_compare(TYPO3_version, '7.3.0', '>=')) {
// Set template
$view->setTemplateRootPaths($templateRootPaths);
$view->setTemplate($template);
// Set layout and partial paths
$view->setLayoutRootPaths($layoutRootPaths);
$view->setPartialRootPaths($partialRootPaths);
} else {
// Emulate template root paths by checking for file existence manually
$templateRootPaths = ArrayUtility::sortArrayWithIntegerKeys($templateRootPaths);
foreach (array_reverse($templateRootPaths) as $templateRootPath) {
// Prepare template path
$templateWithPath = $templateRootPath . '/' . $template;
$templateWithPath = GeneralUtility::fixWindowsFilePath($templateWithPath);
$templateWithPath = GeneralUtility::getFileAbsFileName($templateWithPath, false);
// First check for template with format extension
$templateWithPathAndFormat = $templateWithPath . '.' . $view->getFormat();
if (is_file($templateWithPathAndFormat)) {
$view->setTemplatePathAndFilename($templateWithPathAndFormat);
break;
}
// Then check for plain template
if (is_file($templateWithPath)) {
$view->setTemplatePathAndFilename($templateWithPath);
break;
}
}
// Set layout and partial paths
$partialRootPaths = $this->preparePaths($partialRootPaths);
$view->setPartialRootPaths($partialRootPaths);
$layoutRootPaths = $this->preparePaths($layoutRootPaths);
$view->setLayoutRootPaths($layoutRootPaths);
}
// Enable translation
if (isset($extensionName)) {
$view->getRequest()->setControllerExtensionName($extensionName);
}
return $view;
}
/**
* Prepare array of paths for usage with standalone view
* in older TYPO3 versions
* This makes sure that paths are within the TYPO3 installation
* and that the EXT: prefix is replaced
*
* @param array $paths
* @return array
*/
protected function preparePaths(array $paths) {
return array_map(
array(GeneralUtility::class, 'getFileAbsFileName'),
$paths
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment