Skip to content

Commit

Permalink
[TASK] Make all ViewHelpers compilable
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder authored and Marc Neuhaus committed Oct 8, 2015
1 parent aef1961 commit fdcb40f
Show file tree
Hide file tree
Showing 58 changed files with 939 additions and 535 deletions.
5 changes: 3 additions & 2 deletions Classes/Form/AbstractFormComponent.php
Expand Up @@ -391,10 +391,11 @@ public function getVariable($name) {
* @return ContainerInterface
*/
public function getRoot() {
if (NULL === $this->getParent()) {
$parent = $this->getParent();
if (NULL === $parent || $this === $parent) {
return $this;
}
return $this->getParent()->getRoot();
return $parent->getRoot();
}

/**
Expand Down
35 changes: 6 additions & 29 deletions Classes/Form/AbstractFormContainer.php
Expand Up @@ -42,39 +42,16 @@ public function __construct() {
}

/**
* @param string $namespace
* @param string $type
* @param string $name
* @param null $label
* @return FieldInterface
*/
public function createField($type, $name, $label = NULL) {
$field = parent::createField($type, $name, $label);
$this->add($field);
return $field;
}

/**
* @param string $type
* @param string $name
* @param null $label
* @return ContainerInterface
*/
public function createContainer($type, $name, $label = NULL) {
$container = parent::createContainer($type, $name, $label);
$this->add($container);
return $container;
}

/**
* @param string $type
* @param string $name
* @param null $label
* @return WizardInterface
* @return FormInterface
*/
public function createWizard($type, $name, $label = NULL) {
$wizard = parent::createWizard($type, $name, $label);
$this->add($wizard);
return $wizard;
public function createComponent($namespace, $type, $name, $label = NULL) {
$component = parent::createComponent($namespace, $type, $name, $label);
$this->add($component);
return $component;
}

/**
Expand Down
154 changes: 120 additions & 34 deletions Classes/ViewHelpers/AbstractFormViewHelper.php
Expand Up @@ -11,15 +11,18 @@
use FluidTYPO3\Flux\Form;
use FluidTYPO3\Flux\Form\ContainerInterface;
use FluidTYPO3\Flux\Form\FormInterface;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException;
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;

/**
* Base class for all FlexForm related ViewHelpers
*
* @package Flux
* @subpackage ViewHelpers
*/
abstract class AbstractFormViewHelper extends AbstractViewHelper {
abstract class AbstractFormViewHelper extends AbstractViewHelper implements CompilableInterface {

const SCOPE = 'FluidTYPO3\Flux\ViewHelpers\FormViewHelper';
const SCOPE_VARIABLE_EXTENSIONNAME = 'extensionName';
Expand All @@ -31,21 +34,36 @@ abstract class AbstractFormViewHelper extends AbstractViewHelper {
* @return void
*/
public function render() {
$component = $this->getComponent();
$container = $this->getContainer();
return static::renderStatic($this->arguments, $this->buildRenderChildrenClosure(), $this->renderingContext);
}

/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return void
*/
static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
$component = static::getComponent($renderingContext, $arguments, $renderChildrenClosure);
$container = static::getContainerFromRenderingContext($renderingContext);
$container->add($component);
// rendering child nodes with Form's last sheet as active container
$this->setContainer($component);
$this->renderChildren();
$this->setContainer($container);
static::setContainerInRenderingContext($renderingContext, $component);
$renderChildrenClosure();
static::setContainerInRenderingContext($renderingContext, $container);
static::setExtensionNameInRenderingContext(
$renderingContext,
static::getExtensionNameFromRenderingContextOrArguments($renderingContext, $arguments)
);
parent::renderStatic($arguments, $renderChildrenClosure, $renderingContext);
}

/**
* @return string
*/
public function renderChildren() {
// Make sure the current extension name always propagates to child nodes
$this->viewHelperVariableContainer->addOrUpdate(self::SCOPE, self::SCOPE_VARIABLE_EXTENSIONNAME, $this->getExtensionName());
static::setExtensionNameInRenderingContext($this->renderingContext, $this->getExtensionName());

return parent::renderChildren();
}
Expand All @@ -54,14 +72,38 @@ public function renderChildren() {
* @return string
*/
protected function getExtensionName() {
if (TRUE === $this->hasArgument(self::SCOPE_VARIABLE_EXTENSIONNAME)) {
return $this->arguments[self::SCOPE_VARIABLE_EXTENSIONNAME];
return static::getExtensionNameFromRenderingContextOrArguments($this->renderingContext, $this->arguments);
}

/**
* @param RenderingContextInterface $renderingContext
* @param string $name
* @throws InvalidVariableException
*/
protected static function setExtensionNameInRenderingContext(RenderingContextInterface $renderingContext, $name) {
$renderingContext->getViewHelperVariableContainer()->addOrUpdate(static::SCOPE, static::SCOPE_VARIABLE_EXTENSIONNAME, $name);
}

/**
* @param RenderingContextInterface $renderingContext
* @param array $arguments
* @throws InvalidVariableException
* @return string
*/
protected static function getExtensionNameFromRenderingContextOrArguments(
RenderingContextInterface $renderingContext,
array $arguments
) {
if (TRUE === isset($arguments[static::SCOPE_VARIABLE_EXTENSIONNAME])) {
return $arguments[static::SCOPE_VARIABLE_EXTENSIONNAME];
}
if (TRUE === $this->viewHelperVariableContainer->exists(self::SCOPE, self::SCOPE_VARIABLE_EXTENSIONNAME)) {
return $this->viewHelperVariableContainer->get(self::SCOPE, self::SCOPE_VARIABLE_EXTENSIONNAME);
$viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
if (TRUE === $viewHelperVariableContainer->exists(static::SCOPE, static::SCOPE_VARIABLE_EXTENSIONNAME)) {
return $viewHelperVariableContainer->get(static::SCOPE, static::SCOPE_VARIABLE_EXTENSIONNAME);
}
if (TRUE === isset($this->controllerContext)) {
return $this->controllerContext->getRequest()->getControllerExtensionName();
$controllerContext = $renderingContext->getControllerContext();
if (NULL !== $controllerContext) {
return $controllerContext->getRequest()->getControllerExtensionName();
}
return 'FluidTYPO3.Flux';
}
Expand All @@ -70,35 +112,57 @@ protected function getExtensionName() {
* @return Form
*/
protected function getForm() {
if (TRUE === $this->viewHelperVariableContainer->exists(self::SCOPE, self::SCOPE_VARIABLE_FORM)) {
$form = $this->viewHelperVariableContainer->get(self::SCOPE, self::SCOPE_VARIABLE_FORM);
} elseif (TRUE === $this->templateVariableContainer->exists(self::SCOPE_VARIABLE_FORM)) {
$form = $this->templateVariableContainer->get(self::SCOPE_VARIABLE_FORM);
return static::getFormFromRenderingContext($this->renderingContext);
}

/**
* @param RenderingContextInterface $renderingContext
* @throws InvalidVariableException
* @return FormInterface
*/
protected static function getFormFromRenderingContext(RenderingContextInterface $renderingContext) {
$viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
$templateVariableContainer = $renderingContext->getTemplateVariableContainer();
if (TRUE === $viewHelperVariableContainer->exists(static::SCOPE, static::SCOPE_VARIABLE_FORM)) {
$form = $viewHelperVariableContainer->get(static::SCOPE, static::SCOPE_VARIABLE_FORM);
} elseif (TRUE === $templateVariableContainer->exists(static::SCOPE_VARIABLE_FORM)) {
$form = $templateVariableContainer->get(static::SCOPE_VARIABLE_FORM);
} else {
$form = Form::create();
$this->viewHelperVariableContainer->add(self::SCOPE, self::SCOPE_VARIABLE_FORM, $form);
$viewHelperVariableContainer->add(static::SCOPE, static::SCOPE_VARIABLE_FORM, $form);
}
return $form;
}

/**
* @param string $gridName
* @return Form
* @return Grid
*/
protected function getGrid($gridName = 'grid') {
$form = $this->getForm();
if (FALSE === $this->viewHelperVariableContainer->exists(self::SCOPE, self::SCOPE_VARIABLE_GRIDS)) {
return static::getGridFromRenderingContext($this->renderingContext, $gridName);
}

/**
* @param RenderingContextInterface $renderingContext
* @param string $gridName
* @throws InvalidVariableException
* @return Grid
*/
protected static function getGridFromRenderingContext(RenderingContextInterface $renderingContext, $gridName = 'grid') {
$viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
$form = static::getFormFromRenderingContext($renderingContext);
if (FALSE === $viewHelperVariableContainer->exists(static::SCOPE, static::SCOPE_VARIABLE_GRIDS)) {
$grid = $form->createContainer('Grid', $gridName, 'Grid: ' . $gridName);
$grids = array($gridName => $grid);
$this->viewHelperVariableContainer->add(self::SCOPE, self::SCOPE_VARIABLE_GRIDS, $grids);
$viewHelperVariableContainer->add(static::SCOPE, static::SCOPE_VARIABLE_GRIDS, $grids);
} else {
$grids = $this->viewHelperVariableContainer->get(self::SCOPE, self::SCOPE_VARIABLE_GRIDS);
$grids = $viewHelperVariableContainer->get(static::SCOPE, static::SCOPE_VARIABLE_GRIDS);
if (TRUE === isset($grids[$gridName])) {
$grid = $grids[$gridName];
} else {
$grid = $form->createContainer('Grid', $gridName, 'Grid: ' . $gridName);
$grids[$gridName] = $grid;
$this->viewHelperVariableContainer->addOrUpdate(self::SCOPE, self::SCOPE_VARIABLE_GRIDS, $grids);
$viewHelperVariableContainer->addOrUpdate(static::SCOPE, static::SCOPE_VARIABLE_GRIDS, $grids);
}
}
return $grid;
Expand All @@ -108,28 +172,50 @@ protected function getGrid($gridName = 'grid') {
* @return ContainerInterface
*/
protected function getContainer() {
if (TRUE === $this->viewHelperVariableContainer->exists(self::SCOPE, self::SCOPE_VARIABLE_CONTAINER)) {
$container = $this->viewHelperVariableContainer->get(self::SCOPE, self::SCOPE_VARIABLE_CONTAINER);
} elseif (TRUE === $this->templateVariableContainer->exists(self::SCOPE_VARIABLE_CONTAINER)) {
$container = $this->templateVariableContainer->get(self::SCOPE_VARIABLE_CONTAINER);
return static::getContainerFromRenderingContext($this->renderingContext);
}

/**
* @param RenderingContextInterface $renderingContext
* @throws InvalidVariableException
* @return mixed
*/
protected static function getContainerFromRenderingContext(RenderingContextInterface $renderingContext) {
$viewHelperVariableContainer = $renderingContext->getViewHelperVariableContainer();
$templateVariableContainer = $renderingContext->getTemplateVariableContainer();
if (TRUE === $viewHelperVariableContainer->exists(static::SCOPE, static::SCOPE_VARIABLE_CONTAINER)) {
$container = $viewHelperVariableContainer->get(static::SCOPE, static::SCOPE_VARIABLE_CONTAINER);
} elseif (TRUE === $templateVariableContainer->exists(static::SCOPE_VARIABLE_CONTAINER)) {
$container = $templateVariableContainer->get(static::SCOPE_VARIABLE_CONTAINER);
} else {
$form = $this->getForm();
$form = static::getFormFromRenderingContext($renderingContext);
$container = $form->last();
$this->setContainer($container);
static::setContainerInRenderingContext($renderingContext, $container);
}
return $container;
}

/**
* @param FormInterface $container
* @throws InvalidVariableException
* @return void
*/
protected function setContainer(FormInterface $container) {
$this->viewHelperVariableContainer->addOrUpdate(self::SCOPE, self::SCOPE_VARIABLE_CONTAINER, $container);
if (TRUE === $this->templateVariableContainer->exists(self::SCOPE_VARIABLE_CONTAINER)) {
$this->templateVariableContainer->remove(self::SCOPE_VARIABLE_CONTAINER);
static::setContainerInRenderingContext($this->renderingContext, $container);
}

/**
* @param RenderingContextInterface $renderingContext
* @param FormInterface $container
* @throws InvalidVariableException
* @return void
*/
protected static function setContainerInRenderingContext(RenderingContextInterface $renderingContext, FormInterface $container) {
$renderingContext->getViewHelperVariableContainer()->addOrUpdate(static::SCOPE, static::SCOPE_VARIABLE_CONTAINER, $container);
if (TRUE === $renderingContext->getTemplateVariableContainer()->exists(static::SCOPE_VARIABLE_CONTAINER)) {
$renderingContext->getTemplateVariableContainer()->remove(static::SCOPE_VARIABLE_CONTAINER);
}
$this->templateVariableContainer->add(self::SCOPE_VARIABLE_CONTAINER, $container);
$renderingContext->getTemplateVariableContainer()->add(static::SCOPE_VARIABLE_CONTAINER, $container);
}

}

0 comments on commit fdcb40f

Please sign in to comment.