Skip to content

Instantly share code, notes, and snippets.

@chriwo
Created November 9, 2016 10:26
Show Gist options
  • Save chriwo/d3bf1d52519b706223a187b2e93420c3 to your computer and use it in GitHub Desktop.
Save chriwo/d3bf1d52519b706223a187b2e93420c3 to your computer and use it in GitHub Desktop.
Uri action frontend viewhelper to generate FE links from BE, e.g. in a scheduler task
<?php
namespace ChriWo\Company\ViewHelpers\Uri;
/***************************************************************
*
* Copyright notice
*
* (c) 2016 Christian Wolfram <[email protected]>
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\TimeTracker\NullTimeTracker;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
/**
* Class ActionFrontendViewHelper
*
* @package TYPO3
* @subpackage company
*/
class ActionFrontendViewHelper extends AbstractViewHelper
{
/**
* @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 int $pageUid target page. See TypoLink destination
* @param int $pageType type of the target page. See typolink.parameter
* @param bool $noCache set this to disable caching for the target page. You should not need this.
* @param bool $noCacheHash set this to suppress 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 bool $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 bool $absolute If set, an absolute URI is rendered
* @param bool $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 = [],
$controller = null,
$extensionName = null,
$pluginName = null,
$pageUid = null,
$pageType = 0,
$noCache = false,
$noCacheHash = false,
$section = '',
$format = '',
$linkAccessRestrictedPages = false,
array $additionalParams = [],
$absolute = false,
$addQueryString = false,
array $argumentsToBeExcludedFromQueryString = [],
$addQueryStringMethod = null
) {
$this->initTsfe($pageUid);
$linkConf = [
'parameter' => $pageUid,
'forceAbsoluteUrl' => $absolute,
'additionalParams' => GeneralUtility::implodeArrayForUrl(
null,
self::buildArguments($action, $arguments, $controller, $extensionName, $pluginName)
),
'no_cache' => $noCache,
'useCacheHash' => !$noCacheHash,
'linkAccessRestrictedPages' => $linkAccessRestrictedPages
];
return GeneralUtility::makeInstance(ContentObjectRenderer::class)->typolink_URL($linkConf);
}
/**
* @param null $actionName
* @param array $controllerArguments
* @param null $controllerName
* @param null $extensionName
* @param null $pluginName
* @return array
*/
private function buildArguments(
$actionName = null,
$controllerArguments = [],
$controllerName = null,
$extensionName = null,
$pluginName = null
) {
if ($actionName !== null) {
$controllerArguments['action'] = $actionName;
}
if ($controllerName !== null) {
$controllerArguments['controller'] = $controllerName;
}
$pluginSignature = strtolower($extensionName . '_' . $pluginName);
$defaultPluginNamespace = 'tx_' . $pluginSignature;
return [$defaultPluginNamespace => $controllerArguments];
}
/**
* @param int $id
* @param int $typeNum
* @return void
*/
private function initTsfe($id = 1, $typeNum = 0)
{
if ($id <= 1) {
$id = 1;
}
if (!is_object($GLOBALS['TT'])) {
$GLOBALS['TT'] = new NullTimeTracker();
$GLOBALS['TT']->start();
}
$GLOBALS['TSFE'] = GeneralUtility::makeInstance(
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') || ExtensionManagementUtility::isLoaded('cooluri')) {
$_SERVER['HTTP_HOST'] = BackendUtility::firstDomainRecord(BackendUtility::BEgetRootLine($id));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment