Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Schweriner/fd3767e9ac88fb59fc60d31b614eba94 to your computer and use it in GitHub Desktop.
Save Schweriner/fd3767e9ac88fb59fc60d31b614eba94 to your computer and use it in GitHub Desktop.
TYPO3 Fluid Link ViewHelpers to force Frontend Links in Backend Context (e.g. in Command Controllers)
<?php
namespace Netzmagnet\SubscribeEverything\ViewHelpers;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Utility\EidUtility;
use \TYPO3\CMS\Backend\Utility\BackendUtility;
abstract class AbstrastFrontendLinkViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
{
/**
*
* @param integer $targetPageUid
* @param string $actionName Name of the action to be called
* @param array $controllerArguments Additional query parameters. Will be "namespaced" and merged with $this->arguments.
* @param string $controllerName Name of the target controller. If not set, current ControllerName is used.
* @param string $extensionName Name of the target extension, without underscores. If not set, current ExtensionName is used.
* @param string $pluginName Name of the target plugin. If not set, current PluginName is used.
* @param string $format The requested format, e.g. ".html
* @param array $additionalParams additional query parameters that won't be prefixed like $arguments (overrule $arguments)
* @param string $argumentPrefix Prefix
*
* @return array
*/
protected function uriFor($targetPageUid = NULL, $actionName = NULL, $controllerArguments = array(), $controllerName = NULL, $extensionName = NULL, $pluginName = NULL, $format = '', array $additionalParams = array(), $argumentPrefix = NULL) {
$environmentService = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Service\EnvironmentService::class);
$extensionService = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Service\ExtensionService::class);
if ($actionName !== NULL) {
$controllerArguments['action'] = $actionName;
}
if ($controllerName !== NULL) {
$controllerArguments['controller'] = $controllerName;
} else {
$controllerArguments['controller'] = $this->request->getControllerName();
}
if ($extensionName === NULL) {
$extensionName = $this->request->getControllerExtensionName();
}
if ($pluginName === NULL && $environmentService->isEnvironmentInFrontendMode()) {
$pluginName = $extensionService->getPluginNameByAction($extensionName, $controllerArguments['controller'], $controllerArguments['action']);
}
if ($pluginName === NULL) {
$pluginName = $this->request->getPluginName();
}
if ($targetPageUid === NULL && $environmentService->isEnvironmentInFrontendMode()) {
$targetPageUid = $extensionService->getTargetPidByPlugin($extensionName, $pluginName);
}
if ($format !== '') {
$controllerArguments['format'] = $format;
}
if ($argumentPrefix !== NULL) {
$prefixedControllerArguments = array($argumentPrefix => $controllerArguments);
} else {
$pluginNamespace = $extensionService->getPluginNamespace($extensionName, $pluginName);
$prefixedControllerArguments = array($pluginNamespace => $controllerArguments);
}
//DebugUtility::debug(array_merge_recursive($additionalParams,$prefixedControllerArguments));
return array_merge_recursive($additionalParams,$prefixedControllerArguments);
}
protected function initTSFE($id = 1, $typeNum = 0) {
EidUtility::initTCA();
if (!is_object($GLOBALS['TT'])) {
$GLOBALS['TT'] = new \TYPO3\CMS\Core\TimeTracker\NullTimeTracker;
$GLOBALS['TT']->start();
}
$GLOBALS['TSFE'] = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class, $GLOBALS['TYPO3_CONF_VARS'], $id, $typeNum);
$GLOBALS['TSFE']->connectToDB();
$GLOBALS['TSFE']->initFEuser();
$GLOBALS['TSFE']->determineId();
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->getConfigArray();
if (ExtensionManagementUtility::isLoaded('realurl')) {
$rootline = BackendUtility::BEgetRootLine($id);
$host = BackendUtility::firstDomainRecord($rootline);
$_SERVER['HTTP_HOST'] = $host;
}
}
}
<?php
namespace Netzmagnet\SubscribeEverything\ViewHelpers;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Utility\EidUtility;
use \TYPO3\CMS\Backend\Utility\BackendUtility;
abstract class AbstrastFrontendUriViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{
/**
*
* @param integer $targetPageUid
* @param string $actionName Name of the action to be called
* @param array $controllerArguments Additional query parameters. Will be "namespaced" and merged with $this->arguments.
* @param string $controllerName Name of the target controller. If not set, current ControllerName is used.
* @param string $extensionName Name of the target extension, without underscores. If not set, current ExtensionName is used.
* @param string $pluginName Name of the target plugin. If not set, current PluginName is used.
* @param string $format The requested format, e.g. ".html
* @param array $additionalParams additional query parameters that won't be prefixed like $arguments (overrule $arguments)
* @param string $argumentPrefix Prefix
*
* @return array
*/
protected function uriFor($targetPageUid = NULL, $actionName = NULL, $controllerArguments = array(), $controllerName = NULL, $extensionName = NULL, $pluginName = NULL, $format = '', array $additionalParams = array(), $argumentPrefix = NULL) {
$environmentService = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Service\EnvironmentService::class);
$extensionService = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Service\ExtensionService::class);
if ($actionName !== NULL) {
$controllerArguments['action'] = $actionName;
}
if ($controllerName !== NULL) {
$controllerArguments['controller'] = $controllerName;
} else {
$controllerArguments['controller'] = $this->request->getControllerName();
}
if ($extensionName === NULL) {
$extensionName = $this->request->getControllerExtensionName();
}
if ($pluginName === NULL && $environmentService->isEnvironmentInFrontendMode()) {
$pluginName = $extensionService->getPluginNameByAction($extensionName, $controllerArguments['controller'], $controllerArguments['action']);
}
if ($pluginName === NULL) {
$pluginName = $this->request->getPluginName();
}
if ($targetPageUid === NULL && $environmentService->isEnvironmentInFrontendMode()) {
$targetPageUid = $extensionService->getTargetPidByPlugin($extensionName, $pluginName);
}
if ($format !== '') {
$controllerArguments['format'] = $format;
}
if ($argumentPrefix !== NULL) {
$prefixedControllerArguments = array($argumentPrefix => $controllerArguments);
} else {
$pluginNamespace = $extensionService->getPluginNamespace($extensionName, $pluginName);
$prefixedControllerArguments = array($pluginNamespace => $controllerArguments);
}
//DebugUtility::debug(array_merge_recursive($additionalParams,$prefixedControllerArguments));
return array_merge_recursive($additionalParams,$prefixedControllerArguments);
}
protected function initTSFE($id = 1, $typeNum = 0) {
EidUtility::initTCA();
if (!is_object($GLOBALS['TT'])) {
$GLOBALS['TT'] = new \TYPO3\CMS\Core\TimeTracker\NullTimeTracker;
$GLOBALS['TT']->start();
}
$GLOBALS['TSFE'] = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class, $GLOBALS['TYPO3_CONF_VARS'], $id, $typeNum);
$GLOBALS['TSFE']->connectToDB();
$GLOBALS['TSFE']->initFEuser();
$GLOBALS['TSFE']->determineId();
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->getConfigArray();
if (ExtensionManagementUtility::isLoaded('realurl')) {
$rootline = BackendUtility::BEgetRootLine($id);
$host = BackendUtility::firstDomainRecord($rootline);
$_SERVER['HTTP_HOST'] = $host;
}
}
}
<?php
namespace Netzmagnet\SubscribeEverything\ViewHelpers\Uri;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* A view helper for creating URIs to extbase actions.
*
* = Examples =
*
* <code title="URI to the show-action of the current controller">
* <f:uri.action action="show" />
* </code>
* <output>
* index.php?id=123&tx_myextension_plugin[action]=show&tx_myextension_plugin[controller]=Standard&cHash=xyz
* (depending on the current page and your TS configuration)
* </output>
*/
class ActionViewHelper extends \Netzmagnet\SubscribeEverything\ViewHelpers\AbstrastFrontendUriViewHelper
{
/**
* @param string $action Target action
* @param array $arguments Arguments
* @param string $controller Target controller. If NULL current controllerName is used
* @param string $extensionName Target Extension Name (without "tx_" prefix and no underscores). If NULL the current extension name is used
* @param string $pluginName Target plugin. If empty, the current plugin name is used
* @param integer $pageUid target page. See TypoLink destination
* @param integer $pageType type of the target page. See typolink.parameter
* @param boolean $noCache set this to disable caching for the target page. You should not need this.
* @param boolean $noCacheHash set this to supress the cHash query parameter created by TypoLink. You should not need this.
* @param string $section the anchor to be added to the URI
* @param string $format The requested format, e.g. ".html
* @param boolean $linkAccessRestrictedPages If set, links pointing to access restricted pages will still link to the page even though the page cannot be accessed.
* @param array $additionalParams additional query parameters that won't be prefixed like $arguments (overrule $arguments)
* @param boolean $absolute If set, the URI of the rendered link is absolute
* @param boolean $addQueryString If set, the current query parameters will be kept in the URI
* @param array $argumentsToBeExcludedFromQueryString arguments to be removed from the URI. Only active if $addQueryString = TRUE
* @param string $addQueryStringMethod Set which parameters will be kept. Only active if $addQueryString = TRUE
* @return string Rendered link
*/
public function render($action = NULL, array $arguments = array(), $controller = NULL, $extensionName = NULL, $pluginName = NULL, $pageUid = NULL, $pageType = 0, $noCache = FALSE, $noCacheHash = FALSE, $section = '', $format = '', $linkAccessRestrictedPages = FALSE, array $additionalParams = array(), $absolute = FALSE, $addQueryString = FALSE, array $argumentsToBeExcludedFromQueryString = array(), $addQueryStringMethod = NULL)
{
$this->initTSFE();
$uriBuilder = $this->controllerContext->getUriBuilder();
$uri = $uriBuilder->reset()
->setTargetPageUid($pageUid)
->setTargetPageType($pageType)
->setNoCache($noCache)
->setUseCacheHash(!$noCacheHash)
->setSection($section)
->setFormat($format)
->setLinkAccessRestrictedPages($linkAccessRestrictedPages)
->setArguments($this->uriFor($pageUid, $action, $arguments, $controller, $extensionName, $pluginName, $format, $additionalParams))
->setCreateAbsoluteUri($absolute)
->setAddQueryString($addQueryString)
->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString)
->setAddQueryStringMethod($addQueryStringMethod)
->buildFrontendUri();
return $uri;
}
}
<?php
namespace Netzmagnet\SubscribeEverything\ViewHelpers\Uri;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* A view helper for creating URIs to TYPO3 pages.
*
* = Examples =
*
* <code title="URI to the current page">
* <f:uri.page>page link</f:uri.page>
* </code>
* <output>
* index.php?id=123
* (depending on the current page and your TS configuration)
* </output>
*
* <code title="query parameters">
* <f:uri.page pageUid="1" additionalParams="{foo: 'bar'}" />
* </code>
* <output>
* index.php?id=1&foo=bar
* (depending on your TS configuration)
* </output>
*
* <code title="query parameters for extensions">
* <f:uri.page pageUid="1" additionalParams="{extension_key: {foo: 'bar'}}" />
* </code>
* <output>
* index.php?id=1&extension_key[foo]=bar
* (depending on your TS configuration)
* </output>
*/
class PageViewHelper extends \Netzmagnet\SubscribeEverything\ViewHelpers\AbstrastFrontendUriViewHelper
{
use CompileWithRenderStatic;
/**
* Initialize arguments
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->initTSFE();
$this->registerArgument('pageUid', 'int', 'target PID');
$this->registerArgument('additionalParams', 'array', 'query parameters to be attached to the resulting URI', false, []);
$this->registerArgument('pageType', 'int', 'type of the target page. See typolink.parameter', false, 0);
$this->registerArgument('noCache', 'bool', 'set this to disable caching for the target page. You should not need this.', false, false);
$this->registerArgument('noCacheHash', 'bool', 'set this to suppress the cHash query parameter created by TypoLink. You should not need this.', false, false);
$this->registerArgument('section', 'string', 'the anchor to be added to the URI', false, '');
$this->registerArgument('linkAccessRestrictedPages', 'bool', 'If set, links pointing to access restricted pages will still link to the page even though the page cannot be accessed.', false, false);
$this->registerArgument('absolute', 'bool', 'If set, the URI of the rendered link is absolute', false, false);
$this->registerArgument('addQueryString', 'bool', 'If set, the current query parameters will be kept in the URI', false, false);
$this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'arguments to be removed from the URI. Only active if $addQueryString = TRUE', false, []);
$this->registerArgument('addQueryStringMethod', 'string', 'Set which parameters will be kept. Only active if $addQueryString = TRUE');
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return string Rendered page URI
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$pageUid = $arguments['pageUid'];
$additionalParams = $arguments['additionalParams'];
$pageType = $arguments['pageType'];
$noCache = $arguments['noCache'];
$noCacheHash = $arguments['noCacheHash'];
$section = $arguments['section'];
$linkAccessRestrictedPages = $arguments['linkAccessRestrictedPages'];
$absolute = $arguments['absolute'];
$addQueryString = $arguments['addQueryString'];
$argumentsToBeExcludedFromQueryString = $arguments['argumentsToBeExcludedFromQueryString'];
$addQueryStringMethod = $arguments['addQueryStringMethod'];
$uriBuilder = $renderingContext->getControllerContext()->getUriBuilder();
$uri = $uriBuilder->setTargetPageUid($pageUid)
->setTargetPageType($pageType)
->setNoCache($noCache)
->setUseCacheHash(!$noCacheHash)
->setSection($section)
->setLinkAccessRestrictedPages($linkAccessRestrictedPages)
->setArguments($additionalParams)
->setCreateAbsoluteUri($absolute)
->setAddQueryString($addQueryString)
->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString)
->setAddQueryStringMethod($addQueryStringMethod)->buildFrontendUri();
return $uri;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment