TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Core\Http\ApplicationType;
|
||||
use TYPO3\CMS\Extbase\Mvc\RequestInterface as ExtbaseRequestInterface;
|
||||
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
|
||||
|
||||
/**
|
||||
* ViewHelper for creating links to Extbase actions. Tailored for Extbase
|
||||
* plugins, uses Extbase Request and Extbase UriBuilder.
|
||||
*
|
||||
* ```
|
||||
* <f:link.action action="show" arguments="{blog: blog.uid}">action link</f:link.action>
|
||||
* ```
|
||||
*
|
||||
* @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-link-action
|
||||
*/
|
||||
final class ActionViewHelper extends AbstractTagBasedViewHelper
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $tagName = 'a';
|
||||
|
||||
public function initializeArguments(): void
|
||||
{
|
||||
parent::initializeArguments();
|
||||
$this->registerArgument('action', 'string', 'Target action');
|
||||
$this->registerArgument('arguments', 'array', 'Arguments for the controller action, associative array (do not use reserved keywords "action", "controller" or "format" if not referring to these internal variables specifically)', false, []);
|
||||
$this->registerArgument('controller', 'string', 'Target controller. If NULL current controllerName is used');
|
||||
$this->registerArgument('extensionName', 'string', 'Target Extension Name (without `tx_` prefix and no underscores). If NULL the current extension name is used');
|
||||
$this->registerArgument('pluginName', 'string', 'Target plugin. If empty, the current plugin name is used');
|
||||
$this->registerArgument('pageUid', 'int', 'Target page. See TypoLink destination');
|
||||
$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.');
|
||||
$this->registerArgument('language', 'string', 'link to a specific language - defaults to the current language, use a language ID or "current" to enforce a specific language');
|
||||
$this->registerArgument('section', 'string', 'The anchor to be added to the URI', false, '');
|
||||
$this->registerArgument('format', 'string', 'The requested format, e.g. ".html', 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('additionalParams', 'array', 'Additional query parameters that won\'t be prefixed like $arguments (overrule $arguments)', false, []);
|
||||
$this->registerArgument('absolute', 'bool', 'If set, the URI of the rendered link is absolute', false, false);
|
||||
$this->registerArgument('addQueryString', 'string', 'If set, the current query parameters will be kept in the URL. If set to "untrusted", then ALL query parameters will be added. Be aware, that this might lead to problems when the generated link is cached.', false, false);
|
||||
$this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'Arguments to be removed from the URI. Only active if $addQueryString = true', false, []);
|
||||
}
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
$request = null;
|
||||
if ($this->renderingContext->hasAttribute(ServerRequestInterface::class)) {
|
||||
$request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
|
||||
}
|
||||
// Since f:uri.action and f:link.action use exactly the same ViewHelper arguments,
|
||||
// the glue code between the ViewHelper API and TYPO3's URI generation is shared across both ViewHelpers.
|
||||
$childContent = (string)$this->renderChildren();
|
||||
if ($request instanceof ExtbaseRequestInterface) {
|
||||
$uri = \TYPO3\CMS\Fluid\ViewHelpers\Uri\ActionViewHelper::createUriWithExtbaseContext($request, $this->arguments);
|
||||
if ($uri === '') {
|
||||
return $childContent;
|
||||
}
|
||||
$this->tag->addAttribute('href', $uri);
|
||||
$this->tag->setContent($childContent);
|
||||
$this->tag->forceClosingTag(true);
|
||||
return $this->tag->render();
|
||||
}
|
||||
|
||||
if ($request instanceof ServerRequestInterface && ApplicationType::fromRequest($request)->isFrontend()) {
|
||||
$linkResult = \TYPO3\CMS\Fluid\ViewHelpers\Uri\ActionViewHelper::createFrontendLinkWithCoreContext($request, $this->arguments, $childContent);
|
||||
if ($linkResult === null) {
|
||||
return $childContent;
|
||||
}
|
||||
// Removing TypoLink target here to ensure same behaviour with extbase uri builder in this context.
|
||||
$linkResultAttributes = $linkResult->getAttributes();
|
||||
unset($linkResultAttributes['target']);
|
||||
$this->tag->addAttributes($linkResultAttributes);
|
||||
$this->tag->setContent($childContent);
|
||||
$this->tag->forceClosingTag(true);
|
||||
return $this->tag->render();
|
||||
}
|
||||
throw new \RuntimeException(
|
||||
'The rendering context of ViewHelper f:link.action is missing a valid request object.',
|
||||
1690365240
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Core\Http\ApplicationType;
|
||||
use TYPO3\CMS\Core\LinkHandling\EmailLinkHandler;
|
||||
use TYPO3\CMS\Frontend\Typolink\LinkFactory;
|
||||
use TYPO3\CMS\Frontend\Typolink\UnableToLinkException;
|
||||
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
|
||||
|
||||
/**
|
||||
* ViewHelper to generate an email link (`mailto:`), respecting TYPO3s `spamProtectEmailAddresses` TypoScript setting.
|
||||
*
|
||||
* ```
|
||||
* <f:link.email email="foo@example.com" subject="Website contact" cc="fooSupervisor@example.com" />
|
||||
* ```
|
||||
*
|
||||
* @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-link-email
|
||||
* @see https://docs.typo3.org/permalink/t3tsref:confval-config-spamprotectemailaddresses
|
||||
*/
|
||||
final class EmailViewHelper extends AbstractTagBasedViewHelper
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $tagName = 'a';
|
||||
|
||||
public function __construct(
|
||||
private readonly EmailLinkHandler $emailLinkHandler,
|
||||
private readonly LinkFactory $linkFactory,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function initializeArguments(): void
|
||||
{
|
||||
parent::initializeArguments();
|
||||
$this->registerArgument('email', 'string', 'The email address to be turned into a link', true);
|
||||
$this->registerArgument('cc', 'string', 'The email address(es) for CC of the email link');
|
||||
$this->registerArgument('bcc', 'string', 'The email address(es) for BCC of the email link');
|
||||
$this->registerArgument('subject', 'string', 'A prefilled subject for the email link');
|
||||
$this->registerArgument('body', 'string', 'A prefilled body for the email link');
|
||||
}
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
$email = $this->arguments['email'];
|
||||
$linkHref = $this->emailLinkHandler->asString($this->arguments);
|
||||
$attributes = [];
|
||||
$linkText = htmlspecialchars($email);
|
||||
$request = $this->renderingContext->hasAttribute(ServerRequestInterface::class) ? $this->renderingContext->getAttribute(ServerRequestInterface::class) : null;
|
||||
if ($request !== null && ApplicationType::fromRequest($request)->isFrontend()) {
|
||||
// If there is no request, backend is assumed.
|
||||
try {
|
||||
$linkResult = $this->linkFactory->create($linkText, ['parameter' => $linkHref], $request->getAttribute('currentContentObject'));
|
||||
$linkText = (string)$linkResult->getLinkText();
|
||||
$attributes = $linkResult->getAttributes();
|
||||
} catch (UnableToLinkException) {
|
||||
// Just render the email as is (= Backend Context), if LinkBuilder failed
|
||||
}
|
||||
}
|
||||
$tagContent = $this->renderChildren();
|
||||
if ($tagContent !== null) {
|
||||
$linkText = (string)$tagContent;
|
||||
}
|
||||
$this->tag->setContent($linkText);
|
||||
$this->tag->addAttribute('href', $linkHref);
|
||||
$this->tag->forceClosingTag(true);
|
||||
$this->tag->addAttributes($attributes);
|
||||
return $this->tag->render();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
|
||||
|
||||
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
|
||||
|
||||
/**
|
||||
* ViewHelper for creating links to external targets.
|
||||
*
|
||||
* ```
|
||||
* <f:link.external uri="https://www.typo3.org" target="_blank">external link</f:link.external>
|
||||
* ```
|
||||
*
|
||||
* @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-link-external
|
||||
*/
|
||||
final class ExternalViewHelper extends AbstractTagBasedViewHelper
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $tagName = 'a';
|
||||
|
||||
public function initializeArguments(): void
|
||||
{
|
||||
parent::initializeArguments();
|
||||
$this->registerArgument('uri', 'string', 'The URI that will be put in the href attribute of the rendered link tag', true);
|
||||
$this->registerArgument('defaultScheme', 'string', 'Scheme the href attribute will be prefixed with if specified $uri does not contain a scheme already', false, 'https');
|
||||
}
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
$uri = $this->arguments['uri'];
|
||||
$defaultScheme = $this->arguments['defaultScheme'];
|
||||
|
||||
$scheme = parse_url($uri, PHP_URL_SCHEME);
|
||||
if ($scheme === null && $defaultScheme !== '') {
|
||||
$uri = $defaultScheme . '://' . $uri;
|
||||
}
|
||||
$this->tag->addAttribute('href', $uri);
|
||||
$this->tag->setContent((string)$this->renderChildren());
|
||||
$this->tag->forceClosingTag(true);
|
||||
|
||||
return $this->tag->render();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Core\Core\Environment;
|
||||
use TYPO3\CMS\Core\Crypto\HashService;
|
||||
use TYPO3\CMS\Core\Resource\File;
|
||||
use TYPO3\CMS\Core\Resource\FileInterface;
|
||||
use TYPO3\CMS\Core\Resource\FileReference;
|
||||
use TYPO3\CMS\Core\Resource\ProcessedFile;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\Utility\PathUtility;
|
||||
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
|
||||
use TYPO3Fluid\Fluid\Core\ViewHelper\InvalidArgumentValueException;
|
||||
|
||||
/**
|
||||
* ViewHelper for creating links to a file (FAL).
|
||||
*
|
||||
* ```
|
||||
* <f:link.file file="{file}" target="_blank" download="true" filename="some-file.pdf">See file</f:link.file>
|
||||
* ```
|
||||
*
|
||||
* @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-link-file
|
||||
*/
|
||||
final class FileViewHelper extends AbstractTagBasedViewHelper
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $tagName = 'a';
|
||||
|
||||
public function __construct(
|
||||
private readonly HashService $hashService
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function initializeArguments(): void
|
||||
{
|
||||
parent::initializeArguments();
|
||||
$this->registerArgument('file', FileInterface::class, 'Specifies the file to create a link to', true);
|
||||
$this->registerArgument('download', 'bool', 'Specifies if file should be downloaded instead of displayed');
|
||||
$this->registerArgument('filename', 'string', 'Specifies an alternative filename. If filename contains a file extension, this must be the same as from \'file\'.');
|
||||
}
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
$file = $this->arguments['file'];
|
||||
|
||||
if (!($file instanceof FileInterface)) {
|
||||
throw new InvalidArgumentValueException('Argument \'file\' must be an instance of ' . FileInterface::class, 1621511632);
|
||||
}
|
||||
|
||||
// Get the public URL. This url is either be defined by a GeneratePublicUrlForResourceEvent,
|
||||
// an OnlineMedia helper, the corresponding driver or using the file dump functionality.
|
||||
$publicUrl = $file->getPublicUrl();
|
||||
|
||||
// Early return in case public url is null as this indicates the file is
|
||||
// not accessible, e.g. because the corresponding storage is offline.
|
||||
if ($publicUrl === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (str_contains($publicUrl, 'dumpFile')) {
|
||||
// In case we deal with is a file dump URL, recreate the URL
|
||||
// by taking the defined view helper arguments into account.
|
||||
$publicUrl = $this->createFileDumpUrl($file);
|
||||
} elseif ($this->arguments['download'] ?? false) {
|
||||
// In case the URL directly links to the file (no eID) and
|
||||
// the file should be downloaded instead of displayed, this
|
||||
// must be set by the "download" tag attribute, which may
|
||||
// contain an alternative filename.
|
||||
$this->tag->addAttribute(
|
||||
'download',
|
||||
$this->getAlternativeFilename($file)
|
||||
);
|
||||
}
|
||||
|
||||
$this->tag->addAttribute('href', $publicUrl);
|
||||
$childContent = $this->renderChildren();
|
||||
$this->tag->setContent($childContent ? (string)$childContent : htmlspecialchars($file->getName()));
|
||||
$this->tag->forceClosingTag(true);
|
||||
|
||||
return $this->tag->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a file dump URL, taking the view helper arguments into account
|
||||
*/
|
||||
private function createFileDumpUrl(FileInterface $file): string
|
||||
{
|
||||
$parameters = ['eID' => 'dumpFile'];
|
||||
|
||||
if ($file instanceof File) {
|
||||
$parameters['t'] = 'f';
|
||||
$parameters['f'] = $file->getUid();
|
||||
} elseif ($file instanceof FileReference) {
|
||||
$parameters['t'] = 'r';
|
||||
$parameters['r'] = $file->getUid();
|
||||
} elseif ($file instanceof ProcessedFile) {
|
||||
$parameters['t'] = 'p';
|
||||
$parameters['p'] = $file->getUid();
|
||||
}
|
||||
|
||||
if ($download = $this->arguments['download'] ?? false) {
|
||||
$parameters['dl'] = (int)$download;
|
||||
}
|
||||
|
||||
if (($filename = $this->getAlternativeFilename($file)) !== '') {
|
||||
$parameters['fn'] = $filename;
|
||||
}
|
||||
|
||||
$parameters['token'] = $this->hashService->hmac(implode('|', $parameters), 'resourceStorageDumpFile');
|
||||
|
||||
return GeneralUtility::locationHeaderUrl(
|
||||
PathUtility::getAbsoluteWebPath(Environment::getPublicPath() . '/index.php'),
|
||||
$this->renderingContext->getAttribute(ServerRequestInterface::class)
|
||||
) . '?' . http_build_query($parameters, '', '&', PHP_QUERY_RFC3986);
|
||||
}
|
||||
|
||||
private function getAlternativeFilename(FileInterface $file): string
|
||||
{
|
||||
$alternativeFilename = $this->arguments['filename'] ?? '';
|
||||
|
||||
// Return early if filename is empty or not valid
|
||||
if ($alternativeFilename === '' || !preg_match('/^[0-9a-z._\-]+$/i', $alternativeFilename)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$extension = pathinfo($alternativeFilename, PATHINFO_EXTENSION);
|
||||
if ($extension === '') {
|
||||
// Add original extension in case alternative filename did not contain any
|
||||
$alternativeFilename = rtrim($alternativeFilename, '.') . '.' . $file->getExtension();
|
||||
}
|
||||
|
||||
// Check if given or resolved extension matches the original one
|
||||
return $file->getExtension() === pathinfo($alternativeFilename, PATHINFO_EXTENSION)
|
||||
? $alternativeFilename
|
||||
: '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
|
||||
use TYPO3\CMS\Backend\Routing\Route;
|
||||
use TYPO3\CMS\Backend\Routing\UriBuilder as BackendUriBuilder;
|
||||
use TYPO3\CMS\Core\Http\ApplicationType;
|
||||
use TYPO3\CMS\Core\Utility\ArrayUtility;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Core\Utility\MathUtility;
|
||||
use TYPO3\CMS\Extbase\Mvc\RequestInterface as ExtbaseRequestInterface;
|
||||
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder as ExtbaseUriBuilder;
|
||||
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
|
||||
use TYPO3\CMS\Frontend\Typolink\LinkFactory;
|
||||
use TYPO3\CMS\Frontend\Typolink\UnableToLinkException;
|
||||
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
|
||||
|
||||
/**
|
||||
* ViewHelper for creating links to TYPO3 pages.
|
||||
*
|
||||
* ```
|
||||
* <f:link.page pageUid="42" additionalParams="{foo: 'bar'}">page link</f:link.page>
|
||||
* ```
|
||||
*
|
||||
* @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-link-page
|
||||
*/
|
||||
final class PageViewHelper extends AbstractTagBasedViewHelper
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $tagName = 'a';
|
||||
|
||||
public function __construct(
|
||||
private readonly BackendUriBuilder $uriBuilder,
|
||||
private readonly LinkFactory $linkFactory
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function initializeArguments(): void
|
||||
{
|
||||
parent::initializeArguments();
|
||||
$this->registerArgument('pageUid', 'int', 'Target page. See TypoLink destination');
|
||||
$this->registerArgument('pageType', 'int', 'Type of the target page. See typolink.parameter');
|
||||
$this->registerArgument('noCache', 'bool', 'Set this to disable caching for the target page. You should not need this.');
|
||||
$this->registerArgument('language', 'string', 'link to a specific language - defaults to the current language, use a language ID or "current" to enforce a specific language');
|
||||
$this->registerArgument('section', 'string', 'The anchor to be added to the URI');
|
||||
$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.');
|
||||
$this->registerArgument('additionalParams', 'array', 'Additional query parameters that won\'t be prefixed like $arguments (overrule $arguments)');
|
||||
$this->registerArgument('absolute', 'bool', 'If set, the URI of the rendered link is absolute');
|
||||
$this->registerArgument('addQueryString', 'string', 'If set, the current query parameters will be kept in the URL. If set to "untrusted", then ALL query parameters will be added. Be aware, that this might lead to problems when the generated link is cached.', false, false);
|
||||
$this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'Arguments to be removed from the URI. Only active if $addQueryString = true');
|
||||
}
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
$request = null;
|
||||
if ($this->renderingContext->hasAttribute(ServerRequestInterface::class)) {
|
||||
$request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
|
||||
}
|
||||
if ($request instanceof ExtbaseRequestInterface) {
|
||||
return $this->renderWithExtbaseContext($request);
|
||||
}
|
||||
if ($request instanceof ServerRequestInterface) {
|
||||
if (ApplicationType::fromRequest($request)->isFrontend()) {
|
||||
// Use the regular typolink functionality.
|
||||
return $this->renderFrontendLinkWithCoreContext($request);
|
||||
}
|
||||
$uri = $this->renderBackendLinkWithCoreContext($request);
|
||||
if ($uri !== '') {
|
||||
$this->tag->addAttribute('href', $uri);
|
||||
$this->tag->setContent((string)$this->renderChildren());
|
||||
$this->tag->forceClosingTag(true);
|
||||
$result = $this->tag->render();
|
||||
} else {
|
||||
$result = (string)$this->renderChildren();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
throw new \RuntimeException(
|
||||
'The rendering context of ViewHelper f:link.page is missing a valid request object.',
|
||||
1639819269
|
||||
);
|
||||
}
|
||||
|
||||
private function renderFrontendLinkWithCoreContext(ServerRequestInterface $request): string
|
||||
{
|
||||
$pageUid = isset($this->arguments['pageUid']) ? (int)$this->arguments['pageUid'] : 'current';
|
||||
$pageType = isset($this->arguments['pageType']) ? (int)$this->arguments['pageType'] : 0;
|
||||
$noCache = isset($this->arguments['noCache']) && (bool)$this->arguments['noCache'];
|
||||
$section = isset($this->arguments['section']) ? (string)$this->arguments['section'] : '';
|
||||
$language = isset($this->arguments['language']) ? (string)$this->arguments['language'] : null;
|
||||
$linkAccessRestrictedPages = isset($this->arguments['linkAccessRestrictedPages']) && (bool)$this->arguments['linkAccessRestrictedPages'];
|
||||
$additionalParams = isset($this->arguments['additionalParams']) ? (array)$this->arguments['additionalParams'] : [];
|
||||
$absolute = isset($this->arguments['absolute']) && (bool)$this->arguments['absolute'];
|
||||
$addQueryString = $this->arguments['addQueryString'] ?? false;
|
||||
$argumentsToBeExcludedFromQueryString = isset($this->arguments['argumentsToBeExcludedFromQueryString']) ? (array)$this->arguments['argumentsToBeExcludedFromQueryString'] : [];
|
||||
|
||||
$typolinkConfiguration = [
|
||||
'parameter' => $pageUid,
|
||||
];
|
||||
if ($pageType) {
|
||||
$typolinkConfiguration['parameter'] .= ',' . $pageType;
|
||||
}
|
||||
if ($noCache) {
|
||||
$typolinkConfiguration['no_cache'] = 1;
|
||||
}
|
||||
if ($language !== null) {
|
||||
$typolinkConfiguration['language'] = $language;
|
||||
}
|
||||
if ($section) {
|
||||
$typolinkConfiguration['section'] = $section;
|
||||
}
|
||||
if ($linkAccessRestrictedPages) {
|
||||
$typolinkConfiguration['linkAccessRestrictedPages'] = 1;
|
||||
}
|
||||
if ($additionalParams) {
|
||||
$typolinkConfiguration['queryParameters'] = $additionalParams;
|
||||
}
|
||||
if ($absolute) {
|
||||
$typolinkConfiguration['forceAbsoluteUrl'] = true;
|
||||
}
|
||||
if ($addQueryString && $addQueryString !== 'false') {
|
||||
$typolinkConfiguration['addQueryString'] = $addQueryString;
|
||||
if ($argumentsToBeExcludedFromQueryString !== []) {
|
||||
$typolinkConfiguration['addQueryString.']['exclude'] = implode(',', $argumentsToBeExcludedFromQueryString);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
|
||||
$cObj->setRequest($request);
|
||||
$linkResult = $this->linkFactory->create((string)$this->renderChildren(), $typolinkConfiguration, $cObj);
|
||||
|
||||
// Removing TypoLink target here to ensure same behaviour with extbase uri builder in this context.
|
||||
$linkResultAttributes = $linkResult->getAttributes();
|
||||
unset($linkResultAttributes['target']);
|
||||
|
||||
$this->tag->addAttributes($linkResultAttributes);
|
||||
$this->tag->setContent((string)$this->renderChildren());
|
||||
$this->tag->forceClosingTag(true);
|
||||
$result = $this->tag->render();
|
||||
} catch (UnableToLinkException) {
|
||||
$result = (string)$this->renderChildren();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function renderBackendLinkWithCoreContext(ServerRequestInterface $request): string
|
||||
{
|
||||
$pageUid = isset($this->arguments['pageUid']) ? (int)$this->arguments['pageUid'] : null;
|
||||
$section = isset($this->arguments['section']) ? (string)$this->arguments['section'] : '';
|
||||
$additionalParams = isset($this->arguments['additionalParams']) ? (array)$this->arguments['additionalParams'] : [];
|
||||
$absolute = isset($this->arguments['absolute']) && (bool)$this->arguments['absolute'];
|
||||
$addQueryString = $this->arguments['addQueryString'] ?? false;
|
||||
$argumentsToBeExcludedFromQueryString = isset($this->arguments['argumentsToBeExcludedFromQueryString']) ? (array)$this->arguments['argumentsToBeExcludedFromQueryString'] : [];
|
||||
|
||||
$arguments = [];
|
||||
if ($addQueryString && $addQueryString !== 'false') {
|
||||
$arguments = $request->getQueryParams();
|
||||
foreach ($argumentsToBeExcludedFromQueryString as $argumentToBeExcluded) {
|
||||
$argumentArrayToBeExcluded = [];
|
||||
parse_str($argumentToBeExcluded, $argumentArrayToBeExcluded);
|
||||
$arguments = ArrayUtility::arrayDiffKeyRecursive($arguments, $argumentArrayToBeExcluded);
|
||||
}
|
||||
}
|
||||
|
||||
$id = $pageUid ?? $request->getQueryParams()['id'] ?? null;
|
||||
if ($id !== null) {
|
||||
$arguments['id'] = $id;
|
||||
}
|
||||
if (!isset($arguments['route']) && ($route = $request->getAttribute('route')) instanceof Route) {
|
||||
$arguments['route'] = $route->getOption('_identifier');
|
||||
}
|
||||
$arguments = array_replace_recursive($arguments, $additionalParams);
|
||||
$routeName = $arguments['route'] ?? null;
|
||||
unset($arguments['route'], $arguments['token']);
|
||||
try {
|
||||
if ($absolute) {
|
||||
$uri = (string)$this->uriBuilder->buildUriFromRoute($routeName, $arguments, BackendUriBuilder::ABSOLUTE_URL);
|
||||
} else {
|
||||
$uri = (string)$this->uriBuilder->buildUriFromRoute($routeName, $arguments, BackendUriBuilder::ABSOLUTE_PATH);
|
||||
}
|
||||
} catch (RouteNotFoundException) {
|
||||
$uri = '';
|
||||
}
|
||||
if ($section !== '') {
|
||||
$uri .= '#' . $section;
|
||||
}
|
||||
return $uri;
|
||||
}
|
||||
|
||||
private function renderWithExtbaseContext(ExtbaseRequestInterface $request): string
|
||||
{
|
||||
$pageUid = isset($this->arguments['pageUid']) ? (int)$this->arguments['pageUid'] : null;
|
||||
$pageType = isset($this->arguments['pageType']) ? (int)$this->arguments['pageType'] : 0;
|
||||
$noCache = isset($this->arguments['noCache']) && (bool)$this->arguments['noCache'];
|
||||
$section = isset($this->arguments['section']) ? (string)$this->arguments['section'] : '';
|
||||
$language = isset($this->arguments['language']) ? (string)$this->arguments['language'] : null;
|
||||
$linkAccessRestrictedPages = isset($this->arguments['linkAccessRestrictedPages']) && (bool)$this->arguments['linkAccessRestrictedPages'];
|
||||
$additionalParams = isset($this->arguments['additionalParams']) ? (array)$this->arguments['additionalParams'] : [];
|
||||
$absolute = isset($this->arguments['absolute']) && (bool)$this->arguments['absolute'];
|
||||
$addQueryString = $this->arguments['addQueryString'] ?? false;
|
||||
$argumentsToBeExcludedFromQueryString = isset($this->arguments['argumentsToBeExcludedFromQueryString']) ? (array)$this->arguments['argumentsToBeExcludedFromQueryString'] : [];
|
||||
|
||||
$uriBuilder = GeneralUtility::makeInstance(ExtbaseUriBuilder::class);
|
||||
$uriBuilder->reset()
|
||||
->setRequest($request)
|
||||
->setTargetPageType($pageType)
|
||||
->setNoCache($noCache)
|
||||
->setSection($section)
|
||||
->setLanguage($language)
|
||||
->setLinkAccessRestrictedPages($linkAccessRestrictedPages)
|
||||
->setArguments($additionalParams)
|
||||
->setCreateAbsoluteUri($absolute)
|
||||
->setAddQueryString($addQueryString)
|
||||
->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString);
|
||||
|
||||
if (MathUtility::canBeInterpretedAsInteger($pageUid)) {
|
||||
$uriBuilder->setTargetPageUid((int)$pageUid);
|
||||
}
|
||||
|
||||
$uri = $uriBuilder->build();
|
||||
if ($uri !== '') {
|
||||
$this->tag->addAttribute('href', $uri);
|
||||
$this->tag->setContent((string)$this->renderChildren());
|
||||
$this->tag->forceClosingTag(true);
|
||||
$result = $this->tag->render();
|
||||
} else {
|
||||
$result = (string)$this->renderChildren();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* 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!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Fluid\ViewHelpers\Link;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use TYPO3\CMS\Core\LinkHandling\TypoLinkCodecService;
|
||||
use TYPO3\CMS\Core\LinkHandling\TypolinkParameter;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
|
||||
use TYPO3Fluid\Fluid\Core\Variables\ScopedVariableProvider;
|
||||
use TYPO3Fluid\Fluid\Core\Variables\StandardVariableProvider;
|
||||
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
|
||||
|
||||
/**
|
||||
* ViewHelper to create links from fields supported by the link wizard
|
||||
*
|
||||
* ```
|
||||
* <f:link.typolink parameter="123" additionalParams="&u=b" language="2" />
|
||||
* ```
|
||||
*
|
||||
* @see https://docs.typo3.org/permalink/t3viewhelper:typo3-fluid-link-typolink
|
||||
*/
|
||||
final class TypolinkViewHelper extends AbstractViewHelper
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $escapeOutput = false;
|
||||
|
||||
public function __construct(
|
||||
private readonly TypoLinkCodecService $typoLinkCodecService
|
||||
) {}
|
||||
|
||||
public function initializeArguments(): void
|
||||
{
|
||||
$this->registerArgument('parameter', 'mixed', 'stdWrap.typolink style parameter string', true);
|
||||
$this->registerArgument('target', 'string', 'Define where to display the linked URL', false, '');
|
||||
$this->registerArgument('class', 'string', 'Define classes for the link element', false, '');
|
||||
$this->registerArgument('title', 'string', 'Define the title for the link element', false, '');
|
||||
$this->registerArgument('language', 'string', 'link to a specific language - defaults to the current language, use a language ID or "current" to enforce a specific language');
|
||||
$this->registerArgument('additionalParams', 'string', 'Additional query parameters to be attached to the resulting URL', false, '');
|
||||
$this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes to be added directly to the resulting HTML tag', false, []);
|
||||
$this->registerArgument('addQueryString', 'string', 'If set, the current query parameters will be kept in the URL. If set to "untrusted", then ALL query parameters will be added. Be aware, that this might lead to problems when the generated link is cached.', false, false);
|
||||
$this->registerArgument('addQueryStringExclude', 'string', 'Define parameters to be excluded from the query string (only active if addQueryString is set)', false, '');
|
||||
$this->registerArgument('absolute', 'bool', 'Ensure the resulting URL is an absolute URL', false, false);
|
||||
$this->registerArgument('partsAs', 'string', 'Variable name containing typoLink parts (if any), defaults to "typoLinkParts"');
|
||||
$this->registerArgument('parts-as', 'string', 'Only kept for backwards compatibility, use "partsAs" instead');
|
||||
$this->registerArgument('textWrap', 'string', 'Wrap the link using the typoscript "wrap" data type', false, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws \UnexpectedValueException
|
||||
*/
|
||||
public function render(): string
|
||||
{
|
||||
$parameter = $this->arguments['parameter'] ?? '';
|
||||
$partsAs = $this->arguments['partsAs'] ?? $this->arguments['parts-as'] ?? 'typoLinkParts';
|
||||
if (!$parameter instanceof TypolinkParameter) {
|
||||
$parameter = TypolinkParameter::createFromTypolinkParts(
|
||||
is_scalar($parameter) ? $this->typoLinkCodecService->decode((string)$parameter) : []
|
||||
);
|
||||
}
|
||||
// Merge the $parameter with other arguments
|
||||
$typolinkParameter = TypolinkParameter::createFromTypolinkParts(self::mergeTypoLinkConfiguration($parameter->toArray(), $this->arguments))->toArray();
|
||||
// expose internal typoLink configuration to Fluid child context
|
||||
$variableProvider = new ScopedVariableProvider($this->renderingContext->getVariableProvider(), new StandardVariableProvider([$partsAs => $typolinkParameter]));
|
||||
$this->renderingContext->setVariableProvider($variableProvider);
|
||||
// If no link has to be rendered, the inner content will be returned as such
|
||||
$content = (string)$this->renderChildren();
|
||||
// clean up exposed variables
|
||||
$this->renderingContext->setVariableProvider($variableProvider->getGlobalVariableProvider());
|
||||
$typolink = $this->typoLinkCodecService->encode($typolinkParameter);
|
||||
if ($typolink !== '') {
|
||||
$request = null;
|
||||
if ($this->renderingContext->hasAttribute(ServerRequestInterface::class)) {
|
||||
$request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
|
||||
}
|
||||
$content = self::invokeContentObjectRenderer($this->arguments, $typolink, $content, $request);
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
private static function invokeContentObjectRenderer(array $arguments, string $typoLinkParameter, string $content, ?ServerRequestInterface $request): string
|
||||
{
|
||||
$addQueryString = $arguments['addQueryString'] ?? false;
|
||||
$addQueryStringExclude = $arguments['addQueryStringExclude'] ?? '';
|
||||
$absolute = $arguments['absolute'] ?? false;
|
||||
$aTagParams = self::serializeTagParameters($arguments);
|
||||
|
||||
$instructions = [
|
||||
'parameter' => $typoLinkParameter,
|
||||
'ATagParams' => $aTagParams,
|
||||
'forceAbsoluteUrl' => $absolute,
|
||||
];
|
||||
if (array_key_exists('language', $arguments) && $arguments['language'] !== null) {
|
||||
$instructions['language'] = (string)$arguments['language'];
|
||||
}
|
||||
if ($addQueryString && $addQueryString !== 'false') {
|
||||
$instructions['addQueryString'] = $addQueryString;
|
||||
$instructions['addQueryString.'] = [
|
||||
'exclude' => $addQueryStringExclude,
|
||||
];
|
||||
}
|
||||
if ((string)($arguments['textWrap'] ?? '') !== '') {
|
||||
$instructions['ATagBeforeWrap'] = true;
|
||||
$instructions['wrap'] = $arguments['textWrap'];
|
||||
}
|
||||
|
||||
$contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
|
||||
if ($request) {
|
||||
$contentObject->setRequest($request);
|
||||
}
|
||||
return $contentObject->typoLink($content, $instructions);
|
||||
}
|
||||
|
||||
private static function serializeTagParameters(array $arguments): string
|
||||
{
|
||||
// array(param1 -> value1, param2 -> value2) --> param1="value1" param2="value2" for typolink.ATagParams
|
||||
$extraAttributes = [];
|
||||
$additionalAttributes = $arguments['additionalAttributes'] ?? [];
|
||||
foreach ($additionalAttributes as $attributeName => $attributeValue) {
|
||||
$extraAttributes[] = $attributeName . '="' . htmlspecialchars((string)$attributeValue) . '"';
|
||||
}
|
||||
return implode(' ', $extraAttributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges view helper arguments with typolink parts.
|
||||
*/
|
||||
private static function mergeTypoLinkConfiguration(array $typoLinkConfiguration, array $arguments): array
|
||||
{
|
||||
if ($typoLinkConfiguration === []) {
|
||||
return $typoLinkConfiguration;
|
||||
}
|
||||
|
||||
$target = $arguments['target'] ?? '';
|
||||
$class = $arguments['class'] ?? '';
|
||||
$title = $arguments['title'] ?? '';
|
||||
$additionalParams = $arguments['additionalParams'] ?? '';
|
||||
|
||||
// Override target if given in target argument
|
||||
if ($target) {
|
||||
$typoLinkConfiguration['target'] = $target;
|
||||
}
|
||||
// Combine classes if given in both "parameter" string and "class" argument
|
||||
if ($class) {
|
||||
$classes = explode(' ', trim($typoLinkConfiguration['class']) . ' ' . trim($class));
|
||||
$typoLinkConfiguration['class'] = implode(' ', array_unique(array_filter($classes)));
|
||||
}
|
||||
// Override title if given in title argument
|
||||
if ($title) {
|
||||
$typoLinkConfiguration['title'] = $title;
|
||||
}
|
||||
// Combine additionalParams
|
||||
if ($additionalParams) {
|
||||
$typoLinkConfiguration['additionalParams'] .= $additionalParams;
|
||||
}
|
||||
|
||||
return $typoLinkConfiguration;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user