Skip to content

Commit

Permalink
[TASK] Update flux:form.render to work with new FormEngine
Browse files Browse the repository at this point in the history
This allows a Flux form to be output as HTML with dependencies included - can be used anywhere in the TYPO3 backend, including in Preview section. Frontend usage of FormEngine is still not possible.
  • Loading branch information
NamelessCoder committed Dec 13, 2015
1 parent 0aaa843 commit 3a58293
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
47 changes: 26 additions & 21 deletions Classes/ViewHelpers/Form/RenderViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use FluidTYPO3\Flux\Form;
use FluidTYPO3\Flux\Service\FluxService;
use TYPO3\CMS\Backend\Form\FormEngine;
use TYPO3\CMS\Backend\Form\NodeFactory;
use TYPO3\CMS\Backend\Template\DocumentTemplate;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper as FluidFormViewHelper;

/**
Expand Down Expand Up @@ -42,31 +44,34 @@ public function render(Form $form) {
$record = $form->getOption(Form::OPTION_RECORD);
$table = $form->getOption(Form::OPTION_RECORD_TABLE);
$field = $form->getOption(Form::OPTION_RECORD_FIELD);
$this->ensureBackendDocumentExists();
$formHandler = $this->getFormEngine();
return $formHandler->printNeededJSFunctions_top() .
$formHandler->getSoloField($table, $record, $field) .
$formHandler->printNeededJSFunctions();
$node = $this->getNodeFactory()->create(array(
'type' => 'flex',
'renderType' => 'flex',
'flexFormDataStructureArray' => $form->build(),
'tableName' => $table,
'fieldName' => $field,
'databaseRow' => $record,
'inlineStructure' => array(),
'parameterArray' => array(
'itemFormElName' => sprintf('data[%s][%d][%s]', $table, (integer) $record['uid'], $field),
'itemFormElValue' => GeneralUtility::xml2array($record[$field]),
'fieldChangeFunc' => array(),
'fieldConf' => array(
'config' => array(
'ds' => $form->build()
)
)
)
));
$output = $node->render();
return $output['html'];
}

/**
* @codeCoverageIgnore
* @return void
*/
protected function ensureBackendDocumentExists() {
if (FALSE === isset($GLOBALS['SOBE'])) {
$GLOBALS['SOBE'] = (object) array('doc' => new DocumentTemplate());
}
}

/**
* @codeCoverageIgnore
* @return FormEngine
* @return NodeFactory
*/
protected function getFormEngine() {
$formHandler = new FormEngine();
$formHandler->prependFormFieldNames = $this->getFieldNamePrefix() . '[settings]';
return $formHandler;
protected function getNodeFactory() {
return new NodeFactory();
}

}
21 changes: 9 additions & 12 deletions Tests/Unit/ViewHelpers/Form/RenderViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use FluidTYPO3\Flux\Form;
use FluidTYPO3\Flux\Tests\Unit\ViewHelpers\AbstractViewHelperTestCase;
use TYPO3\CMS\Backend\Form\NodeInterface;
use TYPO3\CMS\Backend\Form\NodeFactory;

/**
* RenderViewHelperTest
Expand All @@ -20,19 +22,14 @@ class RenderViewHelperTest extends AbstractViewHelperTestCase {
* @test
*/
public function testRender() {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'] = array();
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeResolver'] = array();
$form = Form::create();
$engine = $this->getMock(
'TYPO3\\CMS\\Backend\\Form\\FormEngine',
array('printNeededJSFunctions_top', 'getSoloField', 'printNeededJSFunctions'),
array(), '', FALSE
);
$engine->expects($this->once())->method('printNeededJSFunctions_top')->willReturn('1');
$engine->expects($this->once())->method('getSoloField')->willReturn('2');
$engine->expects($this->once())->method('printNeededJSFunctions')->willReturn('3');
$instance = $this->getMock($this->createInstanceClassName(), array('getFormEngine'));
$instance->expects($this->once())->method('getFormEngine')->willReturn($engine);
$result = $instance->render($form);
$this->assertEquals('123', $result);
$nodeFactory = $this->getMock(NodeFactory::class, array('create'));
$nodeFactory->expects($this->once())->method('create')->willReturn($this->getMock(NodeInterface::class));
$instance = $this->getMock($this->createInstanceClassName(), array('getNodeFactory'));
$instance->expects($this->once())->method('getNodeFactory')->willReturn($nodeFactory);
$instance->render($form);
}

}

0 comments on commit 3a58293

Please sign in to comment.