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

[BUG] $GLOBALS['TYPO3_REQUEST'] applicationType is set to FE in backend if page of site can not be found #4023

Open
vertexvaar opened this issue May 6, 2024 · 2 comments

Comments

@vertexvaar
Copy link

This one's a bit complicated but stay with me. I came across this problem for more than 10 times in different projects, and debugging is not easy, but i will it explain as good as possible.
The cases, where i stumbled over this problem are diverse, and the easiest way to reproduce it may not make sense in terms of the use case. The bug can be triggered by other hooks/events. This is not a problem with the yoast_seo extension, i am only using it to show, that the problem exists.

To Reproduce
Steps to reproduce the behavior:

  1. Install yoast-seo-for-typo3/yoast_seo
  2. Create s site configuration with a root page, that does not (yet) exist
  3. Open the solr info backend module
  4. You will see the exception:
    YoastSeoForTypo3\YoastSeo\StructuredData\StructuredDataProviderManager::getTypoScriptFrontendController(): Return value must be of type TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController, null returned

Expected behavior
Invalid site configuration must not lead to an exception, which translates technically to:

The solr extension must not trigger code in the backend that modifies the global state to contain a request with applicationType FE

Root cause analysis

\ApacheSolrForTypo3\Solr\Controller\Backend\Search\AbstractModuleController::initializeAction calls \ApacheSolrForTypo3\Solr\Controller\Backend\Search\AbstractModuleController::autoSelectFirstSiteAndRootPageWhenOnlyOneSiteIsAvailable
That method searches for all sites and "initializes" the site with \ApacheSolrForTypo3\Solr\FrontendEnvironment\Tsfe::initializeTsfe.
If the page does not exist, the page not found handler will be called, which subsequently results in a the call \TYPO3\CMS\Frontend\Middleware\TypoScriptFrontendInitialization::process, which sets $GLOBALS['TYPO3_REQUEST'] = $request;, but the request is a manually created request with applicationType FE. Here, the global state of the application is changed.

Later on, when the ModuleTemplate is initialized and PageRenderer hooks are called, the variable $GLOBALS['TYPO3_REQUEST'] is still set to a request with applicationType FE, which creates these problems.

I have some thoughts on this, but no clear solution.

  1. Never initialize all Sites. TYPO3 instances can have an arbitrary number of sites (~40) in my case, which makes the initialization very slow.
  2. In previous EXT:solr versions, if no page was selected or the selected page did not have a site root in its root line, a message "please select a page" was displayed, which was absolutely fine.
  3. Initializing TSFE in the backend has always been a bad idea and source of errors and other problems. There should be another way to achieve what \ApacheSolrForTypo3\Solr\FrontendEnvironment\Tsfe::initializeTsfe is trying to do.

Used versions (please complete the following information):

  • TYPO3 Version: 12.4
  • Browser: any
  • EXT:solr Version: 12.0.2
  • Used Apache Solr Version: 11.0
  • PHP Version: 8.1-8.3
@vertexvaar
Copy link
Author

Disabling the method fixes all the problems and reduces the module TTFB from > 10 seconds to < 1 sec.

Index: Classes/Controller/Backend/Search/AbstractModuleController.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/Classes/Controller/Backend/Search/AbstractModuleController.php b/Classes/Controller/Backend/Search/AbstractModuleController.php
--- a/Classes/Controller/Backend/Search/AbstractModuleController.php	
+++ b/Classes/Controller/Backend/Search/AbstractModuleController.php	(date 1714990316205)
@@ -102,9 +102,9 @@
 
         $this->requestedPageUID = $this->selectedPageUID;
 
-        if ($this->autoSelectFirstSiteAndRootPageWhenOnlyOneSiteIsAvailable()) {
-            return;
-        }
+//        if ($this->autoSelectFirstSiteAndRootPageWhenOnlyOneSiteIsAvailable()) {
+//            return;
+//        }
 
         if ($this->selectedPageUID < 1) {
             return;
@@ -124,6 +124,7 @@
      */
     protected function autoSelectFirstSiteAndRootPageWhenOnlyOneSiteIsAvailable(): bool
     {
+        return true;
         $solrConfiguredSites = $this->siteRepository->getAvailableSites();
         $availableSites = $this->siteFinder->getAllSites();
         if (count($solrConfiguredSites) === 1 && count($availableSites) === 1) {
@@ -146,10 +147,10 @@
      */
     protected function initializeView(ViewInterface $view): void
     {
-        $sites = $this->siteRepository->getAvailableSites();
-
-        $selectOtherPage = count($sites) > 0 || $this->selectedPageUID < 1;
-        $this->view->assign('showSelectOtherPage', $selectOtherPage);
+//        $sites = $this->siteRepository->getAvailableSites();
+//
+//        $selectOtherPage = count($sites) > 0 || $this->selectedPageUID < 1;
+//        $this->view->assign('showSelectOtherPage', $selectOtherPage);
         $this->view->assign('pageUID', $this->selectedPageUID);
         if ($this->selectedPageUID < 1) {
             return;

@dkd-kaehm
Copy link
Collaborator

That trouble with TSFE is known. We'll try to remove TSFE init stuff from BE on TYPO3 13.

About auto-select Stuff: Please create a pull request against main branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants