[FEATURE] Add context check for TypoScript 88/57588/3
authorBenni Mack <benni@typo3.org>
Fri, 13 Jul 2018 14:46:09 +0000 (16:46 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Fri, 13 Jul 2018 15:32:24 +0000 (17:32 +0200)
The new Context API is added so TypoScript
can check this in cObject like this:

10 = TEXT
10.data = context:workspace:id

where as the key is context:[aspectName]:[propertyName]

Arrays are automatically converted to comma-separated lists,
which is useful for reading info on user groups.

Resolves: #85550
Releases: master
Change-Id: I86673055e088ec4e65288df6145399f8e45055c8
Reviewed-on: https://review.typo3.org/57588
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Documentation/Changelog/master/Feature-85550-IntroduceContextForTypoScriptDataGetTextProperty.rst [new file with mode: 0644]
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-85550-IntroduceContextForTypoScriptDataGetTextProperty.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-85550-IntroduceContextForTypoScriptDataGetTextProperty.rst
new file mode 100644 (file)
index 0000000..e0748df
--- /dev/null
@@ -0,0 +1,31 @@
+.. include:: ../../Includes.txt
+
+========================================================================
+Feature: #85550 - Introduce context for TypoScript data getText property
+========================================================================
+
+See :issue:`85550`
+
+Description
+===========
+
+The new context API can now be accessed via the :ts:`getText` property in TypoScript.
+
+Example:
+
+.. code-block:: typoscript
+
+       page.10 = TEXT
+       page.10.data = context:workspace:id
+       page.10.wrap = You are in workspace: |
+
+Where as `context` is the keyword for accessing an aspect, the second part is the name of the aspect,
+and the third part is the property of the aspect.
+
+.. code-block:: typoscript
+
+       data = context:[aspectName]:[propertyName]
+
+If a property is an array, it is converted into a comma-separated list.
+
+.. index:: TypoScript, ext:frontend
\ No newline at end of file
index 4920b6e..5af917b 100644 (file)
@@ -4987,6 +4987,16 @@ class ContentObjectRenderer implements LoggerAwareInterface
                             $retVal = '';
                         }
                         break;
+                    case 'context':
+                        $context = GeneralUtility::makeInstance(Context::class);
+                        list($aspectName, $propertyName) = GeneralUtility::trimExplode(':', $key, true, 2);
+                        $retVal = $context->getPropertyFromAspect($aspectName, $propertyName, '');
+                        if (is_array($retVal)) {
+                            $retVal = implode(',', $retVal);
+                        }
+                        if (!is_scalar($retVal)) {
+                            $retVal = '';
+                        }
                 }
             }
 
index f7594ed..ee0e70e 100644 (file)
@@ -21,6 +21,7 @@ use TYPO3\CMS\Core\Cache\CacheManager;
 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface as CacheFrontendInterface;
 use TYPO3\CMS\Core\Context\Context;
 use TYPO3\CMS\Core\Context\UserAspect;
+use TYPO3\CMS\Core\Context\WorkspaceAspect;
 use TYPO3\CMS\Core\Core\ApplicationContext;
 use TYPO3\CMS\Core\Core\Environment;
 use TYPO3\CMS\Core\LinkHandling\LinkService;
@@ -1669,6 +1670,24 @@ class ContentObjectRendererTest extends UnitTestCase
     }
 
     /**
+     * Checks if getData() works with type "context"
+     *
+     * @test
+     */
+    public function getDataWithTypeContext(): void
+    {
+        $context = new Context([
+            'workspace' => new WorkspaceAspect(3),
+            'frontend.user' => new UserAspect(new FrontendUserAuthentication(), [0, -1])
+        ]);
+        GeneralUtility::setSingletonInstance(Context::class, $context);
+        $this->assertEquals(3, $this->subject->getData('context:workspace:id'));
+        $this->assertEquals('0,-1', $this->subject->getData('context:frontend.user:groupIds'));
+        $this->assertEquals(false, $this->subject->getData('context:frontend.user:isLoggedIn'));
+        $this->assertEquals(false, $this->subject->getData('context:frontend.user:foozball'));
+    }
+
+    /**
      * Checks if getData() works with type "parentRecordNumber"
      *
      * @test