[FEATURE] Allow site in TypoScript getText 34/58434/3
authorSusanne Moog <susanne.moog@typo3.org>
Fri, 28 Sep 2018 12:31:34 +0000 (14:31 +0200)
committerFrank Naegler <frank.naegler@typo3.org>
Fri, 28 Sep 2018 15:14:07 +0000 (17:14 +0200)
A new key "site" for TypoScript "getText" has been introduced
which allows accessing site configuration.

Resolves: #86422
Releases: master
Change-Id: Id0efae08445b8890021fb2b68a88c4ac8db5a6fa
Reviewed-on: https://review.typo3.org/58434
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Tested-by: Jörg Bösche <typo3@joergboesche.de>
Reviewed-by: Tobi Kretschmann <tobi@tobishome.de>
Tested-by: Tobi Kretschmann <tobi@tobishome.de>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
typo3/sysext/core/Classes/Site/Entity/Site.php
typo3/sysext/core/Documentation/Changelog/master/Feature-86422-IntroduceSiteForTypoScriptDataGetTextProperty.rst [new file with mode: 0644]
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index 86e58b3..e33aecd 100644 (file)
@@ -82,7 +82,7 @@ class Site implements SiteInterface
         $this->identifier = $identifier;
         $this->rootPageId = $rootPageId;
         $this->configuration = $configuration;
-        $configuration['languages'] = $configuration['languages'] ?: [
+        $configuration['languages'] = !empty($configuration['languages']) ? $configuration['languages'] : [
             0 => [
                 'languageId' => 0,
                 'title' => 'Default',
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-86422-IntroduceSiteForTypoScriptDataGetTextProperty.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-86422-IntroduceSiteForTypoScriptDataGetTextProperty.rst
new file mode 100644 (file)
index 0000000..319edbd
--- /dev/null
@@ -0,0 +1,34 @@
+.. include:: ../../Includes.txt
+
+=====================================================================
+Feature: #86422 - Introduce site for TypoScript data getText property
+=====================================================================
+
+See :issue:`86422`
+
+Description
+===========
+
+Site configuration can now be accessed via the :ts:`getText` property in TypoScript.
+
+Example:
+
+.. code-block:: typoscript
+
+       page.10 = TEXT
+       page.10.data = site:base
+       page.10.wrap = This is your base URL: |
+
+Where `site` is the keyword for accessing an aspect, and the latter parts are the configuration key(s) to access. 
+
+.. code-block:: typoscript
+
+       data = site:customConfigKey.nested.value
+
+
+Impact
+======
+
+Accessing site configuration is now possible in TypoScript, which enables to store site specific configuration options in one central place and allows usage of that configuration from different contexts. While this sounds similar to using TypoScript, with using site configuration this may also be used from backend or CLI context as long as the rootPageId of the site is known. To avoid duplicating configuration options, TypoScript can now access these properties, too.
+
+.. index:: PHP-API, ext:frontend
\ No newline at end of file
index 64ffa5e..5318b93 100644 (file)
@@ -46,6 +46,7 @@ use TYPO3\CMS\Core\Resource\StorageRepository;
 use TYPO3\CMS\Core\Service\DependencyOrderingService;
 use TYPO3\CMS\Core\Service\FlexFormService;
 use TYPO3\CMS\Core\Service\MarkerBasedTemplateService;
+use TYPO3\CMS\Core\Site\Entity\Site;
 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
 use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;
 use TYPO3\CMS\Core\TypoScript\TypoScriptService;
@@ -5067,6 +5068,14 @@ class ContentObjectRenderer implements LoggerAwareInterface
                         if (!is_scalar($retVal)) {
                             $retVal = '';
                         }
+                        break;
+                    case 'site':
+                        $request = $GLOBALS['TYPO3_REQUEST'] ?? null;
+                        $site = $request ? $request->getAttribute('site') : null;
+                        if ($site instanceof Site) {
+                            $retVal = ArrayUtility::getValueByPath($site->getConfiguration(), $key, '.');
+                        }
+                        break;
                 }
             }
 
index 05fc3cd..9313d42 100644 (file)
@@ -17,6 +17,7 @@ namespace TYPO3\CMS\Frontend\Tests\Unit\ContentObject;
 
 use PHPUnit\Framework\Exception;
 use Prophecy\Argument;
+use Psr\Http\Message\ServerRequestInterface;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
 use TYPO3\CMS\Core\Cache\CacheManager;
 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface as CacheFrontendInterface;
@@ -32,6 +33,7 @@ use TYPO3\CMS\Core\Resource\Exception\InvalidPathException;
 use TYPO3\CMS\Core\Resource\File;
 use TYPO3\CMS\Core\Resource\ResourceFactory;
 use TYPO3\CMS\Core\Resource\ResourceStorage;
+use TYPO3\CMS\Core\Site\Entity\Site;
 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
 use TYPO3\CMS\Core\TypoScript\TemplateService;
 use TYPO3\CMS\Core\Utility\DebugUtility;
@@ -1691,6 +1693,28 @@ class ContentObjectRendererTest extends UnitTestCase
     }
 
     /**
+     * Checks if getData() works with type "context"
+     *
+     * @test
+     */
+    public function getDataWithTypeSite(): void
+    {
+        $site = new Site('my-site', 123, [
+           'base' => 'http://example.com',
+           'custom' => [
+               'config' => [
+                   'nested' => 'yeah'
+               ]
+           ]
+        ]);
+        $serverRequest = $this->prophesize(ServerRequestInterface::class);
+        $serverRequest->getAttribute('site')->willReturn($site);
+        $GLOBALS['TYPO3_REQUEST'] = $serverRequest->reveal();
+        $this->assertEquals('http://example.com', $this->subject->getData('site:base'));
+        $this->assertEquals('yeah', $this->subject->getData('site:custom.config.nested'));
+    }
+
+    /**
      * Checks if getData() works with type "parentRecordNumber"
      *
      * @test