Commit 86c19567 authored by Daniel Siepmann's avatar Daniel Siepmann Committed by Benni Mack
Browse files

[FEATURE] Support bit-wise and in TypoScript checkIf

Allows direct checks whether an expected bit is part of a bit set, e.g.
in a stored checkbox or radio of a record.

Resolves: #90213
Releases: master
Change-Id: I2ff188c74b207376f5f44307f0a8a2e9514ea6d8
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63046

Tested-by: Georg Ringer's avatarGeorg Ringer <georg.ringer@gmail.com>
Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: Stefan Froemken's avatarStefan Froemken <froemken@gmail.com>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: Georg Ringer's avatarGeorg Ringer <georg.ringer@gmail.com>
Reviewed-by: Stefan Froemken's avatarStefan Froemken <froemken@gmail.com>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent fe67d9a5
.. include:: ../../Includes.txt
==========================================================
Feature: #90213 - Support bit and in TypoScript stdWrap_if
==========================================================
See :issue:`90213`
Description
===========
It is now possible to use :ts:`bitAnd` within TypoScript :ts:`if`.
TYPO3 uses bits to store radio and checkboxes via TCA.
Without this feature one would need to check whether any possible bit value is in a
list. With this feature a simple comparison whether the expected value is part of the
bit set is possible.
Example
=======
An example usage could look like this:
.. code-block:: ts
hideDefaultLanguageOfPage = TEXT
hideDefaultLanguageOfPage {
value = 0
value {
override = 1
override.if {
bitAnd.field = l18n_cfg
value = 1
}
}
}
.. index:: ext:frontend, TypoScript, NotScanned
......@@ -52,6 +52,7 @@ use TYPO3\CMS\Core\Service\FlexFormService;
use TYPO3\CMS\Core\Service\MarkerBasedTemplateService;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
use TYPO3\CMS\Core\Type\BitSet;
use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\ArrayUtility;
......@@ -3076,6 +3077,12 @@ class ContentObjectRenderer implements LoggerAwareInterface
$flag = false;
}
}
if (isset($conf['bitAnd']) || isset($conf['bitAnd.'])) {
$number = isset($conf['bitAnd.']) ? trim($this->stdWrap($conf['bitAnd'], $conf['bitAnd.'])) : trim($conf['bitAnd']);
if ((new BitSet($number))->get($value) === false) {
$flag = false;
}
}
}
if ($conf['negate'] ?? false) {
$flag = !$flag;
......
......@@ -5898,6 +5898,40 @@ class ContentObjectRendererTest extends UnitTestCase
self::assertSame($stop, $subject->_get('stopRendering')[1]);
}
/**
* Data provider for checkIf.
*
* @return array [$expect, $conf]
*/
public function checkIfDataProvider(): array
{
return [
'true bitAnd the same' => [true, ['bitAnd' => '4', 'value' => '4']],
'true bitAnd included' => [true, ['bitAnd' => '6', 'value' => '4']],
'false bitAnd' => [false, ['bitAnd' => '4', 'value' => '3']],
'negate true bitAnd the same' => [false, ['bitAnd' => '4', 'value' => '4', 'negate' => '1']],
'negate true bitAnd included' => [false, ['bitAnd' => '6', 'value' => '4', 'negate' => '1']],
'negate false bitAnd' => [true, ['bitAnd' => '3', 'value' => '4', 'negate' => '1']],
];
}
/**
* Check if checkIf works properly.
*
* @test
* @dataProvider checkIfDataProvider
* @param bool $expect Whether result should be true or false.
* @param array $conf TypoScript configuration to pass into checkIf
*/
public function checkIf(bool $expect, array $conf)
{
$subject = $this->getAccessibleMock(
ContentObjectRenderer::class,
['stdWrap']
);
self::assertSame($expect, $subject->checkIf($conf));
}
/**
* Data provider for stdWrap_ifBlank.
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment