diff --git a/typo3/sysext/core/Documentation/Changelog/13.2/Feature-104020-ViewHelperToCheckFeatureFlags.rst b/typo3/sysext/core/Documentation/Changelog/13.2/Feature-104020-ViewHelperToCheckFeatureFlags.rst new file mode 100644 index 0000000000000000000000000000000000000000..e1ebe7a8e208d1079176e9a34354be8a4c1c4188 --- /dev/null +++ b/typo3/sysext/core/Documentation/Changelog/13.2/Feature-104020-ViewHelperToCheckFeatureFlags.rst @@ -0,0 +1,50 @@ +.. include:: /Includes.rst.txt + +.. _feature-104020-1718381897: + +==================================================== +Feature: #104020 - ViewHelper to check feature flags +==================================================== + +See :issue:`104020` + +Description +=========== + +The `<f:feature>` ViewHelper allows integrators to check for feature flags from within Fluid +templates. The ViewHelper follows the same rules as the underlying TYPO3 api, which means +that undefined flags will be treated as `false`. + +Examples +======== + +Basic usage +----------- + +:: + + <f:feature name="myFeatureFlag"> + This is being shown if the flag is enabled + </f:feature> + +feature / then / else +--------------------- + +:: + + <f:feature name="myFeatureFlag"> + <f:then> + Flag is enabled + </f:then> + <f:else> + Flag is undefined or not enabled + </f:else> + </f:feature> + + +Impact +====== + +Feature flags can now be checked from within Fluid templates. + +.. index:: Fluid, ext:fluid diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/FeatureViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/FeatureViewHelper.php new file mode 100644 index 0000000000000000000000000000000000000000..2f9a24e20334dc7c9681b5260ae995cede726364 --- /dev/null +++ b/typo3/sysext/fluid/Classes/ViewHelpers/FeatureViewHelper.php @@ -0,0 +1,66 @@ +<?php + +declare(strict_types=1); + +/* + * This file is part of the TYPO3 CMS project. + * + * It is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, either version 2 + * of the License, or any later version. + * + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + * + * The TYPO3 project - inspiring people to share! + */ + +namespace TYPO3\CMS\Fluid\ViewHelpers; + +use TYPO3\CMS\Core\Configuration\Features; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; +use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper; + +/** + * This ViewHelper checks if a feature flag is enabled + * + * Examples + * ======== + * + * Basic usage + * ----------- + * + * :: + * + * <f:feature name="myFeatureFlag"> + * This is being shown if the flag is enabled + * </f:feature> + * + * feature / then / else + * --------------------- + * + * :: + * + * <f:feature name="myFeatureFlag"> + * <f:then> + * Flag is enabled + * </f:then> + * <f:else> + * Flag is undefined or not enabled + * </f:else> + * </f:feature> + */ +final class FeatureViewHelper extends AbstractConditionViewHelper +{ + public function initializeArguments(): void + { + parent::initializeArguments(); + $this->registerArgument('name', 'string', 'name of the feature flag that should be checked', true); + } + + public static function verdict(array $arguments, RenderingContextInterface $renderingContext): bool + { + return GeneralUtility::makeInstance(Features::class)->isFeatureEnabled($arguments['name']); + } +} diff --git a/typo3/sysext/fluid/Tests/Functional/ViewHelpers/FeatureViewHelperTest.php b/typo3/sysext/fluid/Tests/Functional/ViewHelpers/FeatureViewHelperTest.php new file mode 100644 index 0000000000000000000000000000000000000000..3c31d292fea5a56b96bc7e09330123c8c466a382 --- /dev/null +++ b/typo3/sysext/fluid/Tests/Functional/ViewHelpers/FeatureViewHelperTest.php @@ -0,0 +1,49 @@ +<?php + +declare(strict_types=1); + +/* + * This file is part of the TYPO3 CMS project. + * + * It is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, either version 2 + * of the License, or any later version. + * + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + * + * The TYPO3 project - inspiring people to share! + */ + +namespace TYPO3\CMS\Fluid\Tests\Functional\ViewHelpers; + +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; +use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory; +use TYPO3\CMS\Fluid\View\TemplateView; +use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; + +final class FeatureViewHelperTest extends FunctionalTestCase +{ + public static function renderDataProvider(): array + { + return [ + 'featureEnabled' => [true, 'enabled'], + 'featureDisabled' => [false, 'disabled'], + 'featureUndefined' => [null, 'disabled'], + ]; + } + + #[DataProvider('renderDataProvider')] + #[Test] + public function render(?bool $featureStatus, string $expected): void + { + $GLOBALS['TYPO3_CONF_VARS']['SYS']['features']['viewHelperTestFeature'] = $featureStatus; + + $context = $this->get(RenderingContextFactory::class)->create(); + $context->getTemplatePaths()->setTemplateSource( + '<f:feature name="viewHelperTestFeature"><f:then>enabled</f:then><f:else>disabled</f:else></f:feature>' + ); + self::assertSame($expected, (new TemplateView($context))->render()); + } +}