FriendsOfTYPO3 / jumpurl

Extension to enable redirects based on short links ("jump urls") to specific pages while keeping click statistics
GNU General Public License v2.0
6 stars 18 forks source link

PSR-7 request is missing in ContentObjectRenderer #54

Open RZivaralam opened 6 months ago

RZivaralam commented 6 months ago

hey, I'm using the Solr and Jumpurl plugins. for indexing queues, solr uses a CLI command and I use this CLI command too. but unfortunately, I received this error "PSR-7 request is missing in ContentObjectRenderer. Inject with start(), setRequest() or provide via $GLOBALS['TYPO3_REQUEST']." this error is a core error in typo3\sysext\frontend\Classes\ContentObject\ContentObjectRenderer.php file.it is beacuse that $GLOBALS['TYPO3_REQUEST'] and ServerRequestInterface are not set. actually in CLI command both of $GLOBALS['TYPO3_REQUEST'] and request will not set . I thought it was a bug from Solr but I saw this discussion at https://github.com/TYPO3-Solr/ext-solr/pull/2973 and they have the following claim: "When initializing the TSFE a TYPO3_REQUEST is also initialized. A request must always have an applicationtype attribute. If no applicationType is given ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST']) can't be used and throws an exception."

and they ask us If we have troubles with third-party code, we should give the hint to the maintainer of those extensions:

I added a little code in ext/jumpurl/Classes/TypoLink/LinkModifier.php(the error was here created) and checked if $GLOBALS['TYPO3_REQUEST'] set or not and after that, I could run the CLI command in solr without any errors. My solution was just a quick fix and surely you will have a more complete solution. I would be very grateful if you write a solution for this issue in new updates, I need your plugin :)

the following codes are : use TYPO3\CMS\Core\Http\ApplicationType; use TYPO3\CMS\Core\Http\ServerRequest;

public function __invoke(AfterLinkIsGeneratedEvent $event): void {

if (($GLOBALS['TYPO3_REQUEST'] ?? null)instanceof ServerRequest && ApplicationType::fromRequest($GLOBALS['TYPO3_REQUEST'])->isFrontend()) { (this if I used before I used your code) if ($this->isEnabled($event)) { $url = $event->getLinkResult()->getUrl(); $context = $event->getLinkResult()->getType(); $configuration = $event->getLinkResult()->getLinkConfiguration(); $this->contentObjectRenderer = $event->getContentObjectRenderer(); $this->frontendController = $this->contentObjectRenderer->getTypoScriptFrontendController();

// Strip the absRefPrefix from the URLs.
$urlPrefix = (string)$this->getTypoScriptFrontendController()->absRefPrefix;
if ($urlPrefix !== '' && str_starts_with($url, $urlPrefix)) {
    $url = substr($url, strlen($urlPrefix));
}

// Make sure the slashes in the file URL are not encoded.
if ($context === LinkService::TYPE_FILE) {
    $url = str_replace('%2F', '/', rawurlencode(rawurldecode($url)));
}

if ($context === LinkService::TYPE_PAGE && $url === '') {
    $url = '/';
}

$urlParameters = ['jumpurl' => $url];

$jumpUrlConfig = $configuration['jumpurl.'] ?? [];

// see if a secure File URL should be built
if (!empty($jumpUrlConfig['secure'])) {
    $secureParameters = $this->getParametersForSecureFile(
        $url,
        $jumpUrlConfig['secure.'] ?? []
    );
    $urlParameters = array_merge($urlParameters, $secureParameters);
} else {
    $urlParameters['juHash'] = JumpUrlUtility::calculateHash($url);
}

$typoLinkConfiguration = [
    'parameter' => $this->getTypoLinkParameter($jumpUrlConfig),
    'additionalParams' => GeneralUtility::implodeArrayForUrl('', $urlParameters),
];

$jumpurl = $this->getContentObjectRenderer()->typoLink_URL($typoLinkConfiguration);

// Now add the prefix again if it was not added by a typolink call already.
if ($urlPrefix !== '') {
    if (!str_starts_with($jumpurl, $urlPrefix)) {
        $jumpurl = $urlPrefix . $jumpurl;
    }
    if (!str_starts_with($url, $urlPrefix)) {
        $url = $urlPrefix . $url;
    }
}
$event->setLinkResult($event->getLinkResult()->withAttributes(['href' => $jumpurl, 'jumpurl' => $url]));

} }

klodeckl commented 3 days ago

Same here. The fix works for me. Thank you!