--- /dev/null
+========================================================================
+Feature: #77336 - Allow passing an own unit collection to ByteViewHelper
+========================================================================
+
+Description
+===========
+
+The viewhelper accepts a new parameter named units. It must be a comma separated list of units.
+
+First example: Use the translation VH
+
+.. code-block::
+{fileSize -> f:format.bytes(units: '{f:translate(\'viewhelper.format.bytes.units\', \'fluid\')}'}
+
+Second example: Provide a plain list
+
+.. code-block::
+<f:format.bytes units="byte, kilo, mega, husel, pusel">{size}</f:format.bytes>
+
+results in the currently used collection, provided by the core.
+
+
+Impact
+======
+
+A custom list of units can be passed to the viewHelper and will be used for formatting. Existing behaviour is not changed.
\ No newline at end of file
* // depending on the value of {fileSize}
* </output>
*
+ * You may provide an own set of units, like this: B,KB,MB,GB,TB,PB,EB,ZB,YB
+ * <code title="custom units">
+ * {fileSize -> f:format.bytes(units: '{f:translate(\'viewhelper.format.bytes.units\', \'fluid\')}'
+ * </code>
+ * <output>
+ * 123 KB
+ * // depending on the value of {fileSize}
+ * </output>
+ *
* @api
*/
class BytesViewHelper extends AbstractViewHelper
'.');
$this->registerArgument('thousandsSeparator', 'string',
'The character for grouping the thousand digits', false, ',');
+ $this->registerArgument('units', 'string',
+ 'comma separated list of available units, default is LocalizationUtility::translate(\'viewhelper.format.bytes.units\', \'fluid\')');
}
/**
- * @var array
- */
- protected static $units = array();
-
- /**
* Render the supplied byte count as a human readable string.
*
* @return string Formatted byte count
}
/**
- * set units - temporary method, will vanish with https://review.typo3.org/#/c/49289/
- *
- */
- public static function setUnits($units = [])
- {
- self::$units = $units;
- if (empty($units)) {
- self::$units = GeneralUtility::trimExplode(',',
- LocalizationUtility::translate('viewhelper.format.bytes.units', 'fluid'));
- }
- }
-
- /**
* Applies htmlspecialchars() on the specified value.
*
* @param array $arguments
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
- if (empty(self::$units)) {
- self::setUnits();
+ if ($arguments['units'] !== null) {
+ $units = $arguments['units'];
+ } else {
+ $units = LocalizationUtility::translate('viewhelper.format.bytes.units', 'fluid');
}
+ $units = GeneralUtility::trimExplode(',', $units, true);
$value = $arguments['value'];
if ($value === null) {
}
$bytes = max($value, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
- $pow = min($pow, count(self::$units) - 1);
+ $pow = min($pow, count($units) - 1);
$bytes /= pow(2, 10 * $pow);
return sprintf(
'%s %s',
number_format(round($bytes, 4 * $arguments['decimals']), $arguments['decimals'],
$arguments['decimalSeparator'], $arguments['thousandsSeparator']),
- self::$units[$pow]
+ $units[$pow]
);
}
}
* The TYPO3 project - inspiring people to share!
*/
-use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
use TYPO3\CMS\Fluid\ViewHelpers\Format\BytesViewHelper;
protected function setUp()
{
parent::setUp();
- // XXX: This is bad from a testing POV but the only option right now
- $reflectionClass = new \ReflectionClass(\TYPO3\CMS\Extbase\Utility\LocalizationUtility::class);
- $property = $reflectionClass->getProperty('configurationManager');
- $property->setAccessible(true);
- $property->setValue($this->createMock(ConfigurationManagerInterface::class));
$this->viewHelper = new BytesViewHelper();
$this->injectDependenciesIntoViewHelper($this->viewHelper);
- BytesViewHelper::setUnits(GeneralUtility::trimExplode(',', 'B,KB,MB,GB,TB,PB,EB,ZB,YB'));
}
/**
'decimals' => $decimals,
'decimalSeparator' => $decimalSeparator,
'thousandsSeparator' => $thousandsSeparator,
+ 'units' => 'B,KB,MB,GB,TB,PB,EB,ZB,YB',
]
);
$actualResult = $this->viewHelper->initializeArgumentsAndRender();
return 12345;
}
);
+ $this->setArgumentsUnderTest(
+ $this->viewHelper,
+ [
+ 'units' => 'B,KB,MB,GB,TB,PB,EB,ZB,YB',
+ ]
+ );
$actualResult = $this->viewHelper->initializeArgumentsAndRender();
$this->assertEquals('12 KB', $actualResult);
}