ew_base/Classes/ViewHelpers/FlexFormViewHelper.php
2024-12-26 21:44:19 +01:00

69 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Evoweb\EwBase\ViewHelpers;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* = 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
{
/**
* @var boolean
*/
protected $escapeOutput = false;
public function initializeArguments(): void
{
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');
}
public function render(): string
{
$data = [];
if (is_array($this->arguments['data'])) {
if (isset($this->arguments['data'][$this->arguments['fieldName']])) {
$data = is_array($this->arguments['data'][$this->arguments['fieldName']]) ?
$this->arguments['data'][$this->arguments['fieldName']] :
GeneralUtility::xml2array($this->arguments['data'][$this->arguments['fieldName']]);
$data = $data['data'] ?? $data;
}
}
$templateVariableContainer = $this->renderingContext->getVariableProvider();
$templateVariableContainer->add($this->arguments['as'], $data);
$content = $this->renderChildren();
$templateVariableContainer->remove($this->arguments['as']);
return $content;
}
}