Project

General

Profile

Feature #103526 ยป ConstantViewHelper.php

Dieter Porth, 2024-04-03 10:06

 
<?php

declare(strict_types=1);

namespace Porthd\Newssystem\ViewHelpers;

/***************************************************************
*
* Copyright notice
*
* (c) 2024 Dr. Dieter Porth <info@mobger.de>
*
* All rights reserved
*
* This script is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
* example for Usage of the viewhelper `const`
*
* Requirement:
* You have defined in your code a public PHP-constant. The value 42 is available by the following namespace
* $const = Vendor\MyExt\SelfConstClass::FOO; // = 42
* In the typoScript is defined:
* settings.fooBarBaz = 37
* settings.fooUndef is undefined
*
* Usage in FLuid:
* <div><f:const>Vendor\MyExt\SelfConstClass::FOO</f:const></div>
* Output
* <div>42</div>
*
* Usage in FLuid (unknown const):
* <div><f:const>Vendor\MyExt\SelfConstClass::FOO_BAR</f:const></div>
* Output
* <div></div>
*
* Usage in FLuid (use with override and value):
* <div>1. <f:const value="Vendor\\MyExt\\SelfConstClass::FOO_BAR" override="37"></f:const></div>
* <div>2. <f:const value="Vendor\\MyExt\\SelfConstClass::FOO_BAR" override="{settings.fooBarBaz}"></f:const></div>
* Output
* <div>1. 37</div>
* <div>2. 37</div>
*
* Usage in Inline-Form (unknown const):
* <div data-type="{f:const(value:'Vendor\\MyExt\\SelfConstClass::FOO',override:settings.fooUndef)}"></div>
* Output
* <div data-type="42"></div>
*/
class ConstantViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;


/**
* @var boolean
*/
protected $escapeOutput = false;

/**
* @return void
*/
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerArgument(
'value',
'string',
'full namespace of the php-constant'
);
$this->registerArgument(
'override',
'string',
'The value overrides the constant defined in value or by inclusion',
);
}
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
{

if (isset($arguments['override'])){
return (string)$arguments['override'];
}
$const = ((isset($arguments['value']))?
$arguments['value']:
(string)$renderChildrenClosure()
);

return $const.' '.(defined($const)?(string)constant($const):'');
}
}
    (1-1/1)