ew_base/Classes/ViewHelpers/FlexFormViewHelper.php
2024-12-14 14:03:20 +01:00

81 lines
2.4 KiB
PHP

<?php
namespace Evoweb\EwBase\ViewHelpers;
/*
* This file is developed by evoWeb.
*
* 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.
*/
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* = Examples =
*
* <code title="Simple Loop">
* <ewb:flexForm data="{data}">....</ewb:flexForm>
* </code>
* <output>
* fixed flexform data for extbase controller
* </output>
*
* @api
*/
class FlexFormViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var boolean
*/
protected $escapeOutput = false;
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('data', 'array', 'Array to get flex form data from', true);
$this->registerArgument('fieldName', 'string', 'Field name', false, 'pi_flexform');
$this->registerArgument('as', 'string', 'Name of the variable to assign', false, 'flexFormData');
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
*
* @return array
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
$data = [];
if (is_array($arguments['data'])) {
if (isset($arguments['data'][$arguments['fieldName']])) {
$data = is_array($arguments['data'][$arguments['fieldName']]) ?
$arguments['data'][$arguments['fieldName']] :
GeneralUtility::xml2array($arguments['data'][$arguments['fieldName']]);
$data = $data['data'] ?? $data;
}
}
$templateVariableContainer = $renderingContext->getVariableProvider();
$templateVariableContainer->add($arguments['as'], $data);
$content = $renderChildrenClosure();
$templateVariableContainer->remove($arguments['as']);
return $content;
}
}