Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TYPO3 13 compatibility #58

Merged
merged 4 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions Classes/Middleware/JumpUrlHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Http\RedirectResponse;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Resource\Security\FileNameValidator;
use TYPO3\CMS\Core\Site\Entity\NullSite;
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
use TYPO3\CMS\Core\TypoScript\PageTsConfig;
use TYPO3\CMS\Core\TypoScript\PageTsConfigFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

Expand Down Expand Up @@ -69,7 +73,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}
// Regular jump URL
$this->validateIfJumpUrlRedirectIsAllowed($jumpUrl, $juHash);
return $this->redirectToJumpUrl($jumpUrl);
return $this->redirectToJumpUrl($jumpUrl, $request);
}
return $handler->handle($request);
}
Expand All @@ -81,9 +85,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
* @throws \Exception
* @return ResponseInterface
*/
protected function redirectToJumpUrl(string $jumpUrl): ResponseInterface
protected function redirectToJumpUrl(string $jumpUrl, ServerRequestInterface $request): ResponseInterface
{
$pageTSconfig = $this->getTypoScriptFrontendController()->getPagesTSconfig();
$pageTSconfig = $this->getPageTsConfig($request);
$pageTSconfig = array_key_exists('TSFE.', $pageTSconfig) && is_array($pageTSconfig['TSFE.'] ?? false) ? $pageTSconfig['TSFE.'] : [];

// Allow sections in links
Expand All @@ -94,6 +98,30 @@ protected function redirectToJumpUrl(string $jumpUrl): ResponseInterface
return new RedirectResponse($jumpUrl, $statusCode);
}

/**
* @param ServerRequestInterface $request
* @return array
* @throws \JsonException
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
*/
protected function getPageTsConfig(ServerRequestInterface $request): array
{
$pageInformation = $request->getAttribute('frontend.page.information');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change only works in v13, but the changes you make in composer.json says that the code should also stay compatible with older versions.

How about we make it work for v12 and v13 at the same time and drop the older versions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you go ...

$id = $pageInformation->getId();
$runtimeCache = GeneralUtility::makeInstance(CacheManager::class)->getCache('runtime');
$pageTsConfig = $runtimeCache->get('pageTsConfig-' . $id);
if ($pageTsConfig instanceof PageTsConfig) {
return $pageTsConfig->getPageTsConfigArray();
}
$fullRootLine = $pageInformation->getRootLine();
ksort($fullRootLine);
$site = $request->getAttribute('site') ?? new NullSite();
$pageTsConfigFactory = GeneralUtility::makeInstance(PageTsConfigFactory::class);
$pageTsConfig = $pageTsConfigFactory->create($fullRootLine, $site);
$runtimeCache->set('pageTsConfig-' . $id, $pageTsConfig);
return $pageTsConfig->getPageTsConfigArray();
}

/**
* If the submitted hash is correct and the user has access to the
* related content element the contents of the submitted file will
Expand Down Expand Up @@ -157,7 +185,7 @@ protected function forwardJumpUrlSecureFileData(string $jumpUrl, string $locatio
protected function isLocationDataValid(string $locationData): bool
{
$isValidLocationData = false;
list($pageUid, $table, $recordUid) = explode(':', $locationData);
[$pageUid, $table, $recordUid] = explode(':', $locationData);
$pageRepository = $this->getTypoScriptFrontendController()->sys_page;
if (empty($table) || $pageRepository->checkRecord($table, $recordUid, true)) {
// This check means that a record is checked only if the locationData has a value for a
Expand Down
42 changes: 34 additions & 8 deletions Classes/TypoLink/LinkModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

use FoT3\Jumpurl\JumpUrlUtility;
use TYPO3\CMS\Core\LinkHandling\LinkService;
use TYPO3\CMS\Core\Site\Entity\NullSite;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
Expand All @@ -36,12 +40,12 @@ class LinkModifier
/**
* @var TypoScriptFrontendController
*/
protected $frontendController;
protected $typoScriptFrontendController;

public function __invoke(AfterLinkIsGeneratedEvent $event): void
{
$this->contentObjectRenderer = $event->getContentObjectRenderer();
$this->frontendController = $this->contentObjectRenderer->getTypoScriptFrontendController();
$this->typoScriptFrontendController = $this->contentObjectRenderer->getTypoScriptFrontendController();

if ($this->isEnabled($event)) {
$url = $event->getLinkResult()->getUrl();
Expand Down Expand Up @@ -192,13 +196,35 @@ protected function getTypoLinkParameter(array $configuration)

protected function getTypoScriptFrontendController(): TypoScriptFrontendController
{
$tsfe = $this->frontendController ?? $GLOBALS['TSFE'] ?? null;
if ($tsfe instanceof TypoScriptFrontendController) {
return $tsfe;
if ($this->typoScriptFrontendController instanceof TypoScriptFrontendController) {
return $this->typoScriptFrontendController;
}
// workaround for getting a TSFE object in Backend
$linkBuilder = GeneralUtility::makeInstance(PageLinkBuilder::class, new ContentObjectRenderer());
return $linkBuilder->getTypoScriptFrontendController();

// This usually happens when typolink is created by the TYPO3 Backend, where no TSFE object
// is there. This functionality is currently completely internal, as these links cannot be
// created properly from the Backend.
// However, this is added to avoid any exceptions when trying to create a link.
// Detecting the "first" site usually comes from the fact that TSFE needs to be instantiated
// during tests
$request = $this->contentObjectRenderer->getRequest();
$site = $request->getAttribute('site');
if (!$site instanceof Site) {
$sites = GeneralUtility::makeInstance(SiteFinder::class)->getAllSites();
$site = reset($sites);
if (!$site instanceof Site) {
$site = new NullSite();
}
}
$language = $request->getAttribute('language');
if (!$language instanceof SiteLanguage) {
$language = $site->getDefaultLanguage();
}
$request = $request->withAttribute('language', $language);

$this->typoScriptFrontendController = GeneralUtility::makeInstance(TypoScriptFrontendController::class);
$this->typoScriptFrontendController->initializePageRenderer($request);
$this->typoScriptFrontendController->initializeLanguageService($request);
return $this->typoScriptFrontendController;
}

protected function getContentObjectRenderer(): ContentObjectRenderer
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
},
"license": "GPL-2.0-or-later",
"require": {
"typo3/cms-core": "^9.0 || ^10.0 || ^11.0 || ^12.0",
"typo3/cms-frontend": "^9.0 || ^10.0 || ^11.0 || ^12.0"
"typo3/cms-core": "^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0",
"typo3/cms-frontend": "^9.0 || ^10.0 || ^11.0 || ^12.0 || ^13.0"
},
"require-dev": {
"nimut/testing-framework": "^5.0",
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'version' => '8.2.2',
'constraints' => [
'depends' => [
'typo3' => '9.5.0-12.4.99',
'typo3' => '9.5.0-13.4.99',
],
'conflicts' => [],
'suggests' => [],
Expand Down