Skip to content

Commit

Permalink
[BUGFIX] Handle possibly-null view instance
Browse files Browse the repository at this point in the history
This change corrects the signature of
ViewHelperVariableContainer->getView to reflect
that the return is possibly-null, and throws a
very specific exception in f:render if encountering
that case (indicating that a RenderingContext
override was incomplete or incorrect).
  • Loading branch information
NamelessCoder authored and mbrodala committed May 31, 2021
1 parent 6400bbe commit 3420f8c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Core/ViewHelper/ViewHelperVariableContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function setView(ViewInterface $view)
*
* !!! This is NOT a public API and might still change!!!
*
* @return ViewInterface The View
* @return ViewInterface|null The View, or null if view was not set
*/
public function getView()
{
Expand Down
10 changes: 10 additions & 0 deletions src/ViewHelpers/RenderViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use TYPO3Fluid\Fluid\Core\Rendering\RenderableInterface;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

/**
Expand Down Expand Up @@ -147,6 +148,15 @@ public static function renderStatic(array $arguments, \Closure $renderChildrenCl
}

$view = $renderingContext->getViewHelperVariableContainer()->getView();
if (!$view) {
throw new Exception(
'The f:render ViewHelper was used in a context where the ViewHelperVariableContainer does not contain ' .
'a reference to the View. Normally this is taken care of by the TemplateView, so most likely this ' .
'error is because you overrode AbstractTemplateView->initializeRenderingContext() and did not call ' .
'$renderingContext->getViewHelperVariableContainer()->setView($this) or parent::initializeRenderingContext. ' .
'This is an issue you must fix in your code as f:render is fully unable to render anything without a View.'
);
}
$content = '';
if ($renderable) {
$content = $renderable->render($renderingContext);
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/ViewHelpers/RenderViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

use TYPO3Fluid\Fluid\Core\Rendering\RenderableInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperVariableContainer;
use TYPO3Fluid\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture;
use TYPO3Fluid\Fluid\Tests\Unit\ViewHelpers\Fixtures\ParsedTemplateImplementationFixture;
Expand Down Expand Up @@ -61,6 +62,23 @@ public function testInitializeArgumentsRegistersExpectedArguments()
$instance->initializeArguments();
}

public function testThrowsExceptionIfExecutedWithoutViewSetOnViewHelperVariableContainerRegardlessOfInvalidArguments()
{
$renderingContext = new RenderingContextFixture();
$this->setExpectedException(Exception::class);
$arguments = [
'partial' => null,
'section' => null,
'delegate' => null,
'renderable' => null,
'arguments' => [],
'optional' => true,
'default' => null,
'contentAs' => null
];
RenderViewHelper::renderStatic($arguments, function() {}, $renderingContext);
}

/**
* @test
*/
Expand Down

0 comments on commit 3420f8c

Please sign in to comment.