Skip to content

Instantly share code, notes, and snippets.

@smichaelsen
Created September 16, 2015 13:36
Show Gist options
  • Save smichaelsen/3ff9ad8ddd44de430b7f to your computer and use it in GitHub Desktop.
Save smichaelsen/3ff9ad8ddd44de430b7f to your computer and use it in GitHub Desktop.
ExtbaseViewHelper
<?php
namespace AppZap\Tripshop\ViewHelpers;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Core\Bootstrap;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* Renders an extbase action into your template
*
* <ts:extbase controller="Journey" action="listComingUp"/>
* Renders Journey->listComingUpAction of the current plugin
*
* <ts:extbase extension="News" vendor="GeorgRinger" plugin="Pi1"/>
* Renders the default controller/action of EXT:news
*/
class ExtbaseViewHelper extends AbstractViewHelper {
/**
* @var \TYPO3\CMS\Extbase\Service\ExtensionService
* @inject
*/
protected $extensionService;
/**
* @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
*/
public function initializeArguments() {
$this->registerArgument('extension', 'string', '');
$this->registerArgument('vendor', 'string', '');
$this->registerArgument('plugin', 'string', '');
$this->registerArgument('controller', 'string', '');
$this->registerArgument('action', 'string', '');
}
/**
* @return string
*/
public function render() {
$configuration = [
'extensionName' => $this->arguments['extension'] ?: $this->controllerContext->getRequest()->getControllerExtensionName(),
'vendorName' => $this->arguments['vendor'] ?: $this->controllerContext->getRequest()->getControllerVendorName(),
'pluginName' => $this->arguments['plugin'] ?: $this->controllerContext->getRequest()->getPluginName(),
];
// set controller and action via $_GET
if ($this->arguments['controller'] || $this->arguments['action']) {
$pluginNamespace = $this->extensionService->getPluginNamespace($configuration['extensionName'], $configuration['pluginName']);
$GLOBALS['_GET'][$pluginNamespace] = [];
if ($this->arguments['controller']) {
$GLOBALS['_GET'][$pluginNamespace]['controller'] = $this->arguments['controller'];
}
if ($this->arguments['action']) {
$GLOBALS['_GET'][$pluginNamespace]['action'] = $this->arguments['action'];
}
}
/** @var Bootstrap $bootstrap */
$bootstrap = GeneralUtility::makeInstance(Bootstrap::class);
return $bootstrap->run('', $configuration);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment