Skip to content

Commit

Permalink
Merge pull request #19 from kitsunet/fluid-standalone
Browse files Browse the repository at this point in the history
TASK: Adapt to Fluid standalone
  • Loading branch information
kitsunet committed Nov 16, 2016
2 parents df1af31 + ea13b28 commit e9c938b
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 62 deletions.
87 changes: 64 additions & 23 deletions Classes/TYPO3/Form/Core/Renderer/FluidFormRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* The TYPO3 project - inspiring people to share! *
* */

use Neos\FluidAdaptor\Core\ViewHelper\TemplateVariableContainer;
use Neos\FluidAdaptor\View\TemplatePaths;
use Neos\FluidAdaptor\View\TemplateView;
use TYPO3\Flow\Annotations as Flow;

/**
Expand Down Expand Up @@ -88,23 +91,22 @@
* returns a non-NULL string), this renderer is automatically instanciated
* and the rendering for this element is delegated to this Renderer.
*/
class FluidFormRenderer extends \TYPO3\Fluid\View\TemplateView implements RendererInterface
class FluidFormRenderer extends TemplateView implements RendererInterface
{
/**
* @var \TYPO3\Form\Core\Runtime\FormRuntime
*/
protected $formRuntime;

/**
* Sets the current controller context
*
* @param \TYPO3\Flow\Mvc\Controller\ControllerContext $controllerContext Controller context which is available inside the view
* @return void
* @api
* @var \TYPO3Fluid\Fluid\Core\Parser\TemplateParser
*/
public function setControllerContext(\TYPO3\Flow\Mvc\Controller\ControllerContext $controllerContext)
protected $templateParser;

public function __construct(array $options = [])
{
$this->controllerContext = $controllerContext;
parent::__construct($options);
$this->templateParser = $this->baseRenderingContext->getTemplateParser();
}

/**
Expand Down Expand Up @@ -138,7 +140,6 @@ public function renderRenderable(\TYPO3\Form\Core\Model\Renderable\RootRenderabl
{
$renderable->beforeRendering($this->formRuntime);

$this->templateParser->setConfiguration($this->buildParserConfiguration());
$renderableType = $renderable->getType();

if ($renderable->getRendererClassName() !== null && $renderable->getRendererClassName() !== get_class($this)) {
Expand All @@ -156,6 +157,11 @@ public function renderRenderable(\TYPO3\Form\Core\Model\Renderable\RootRenderabl

$renderablePathAndFilename = $this->getPathAndFilenameForRenderable($renderableType, $renderingOptions);
$parsedRenderable = $this->getParsedRenderable($renderable->getType(), $renderablePathAndFilename);
$currentTemplatePathsResolver = $this->getTemplatePaths();
$newTemplatePathsResolver = $this->createTemplatePathsResolverWithRenderingOptions($renderingOptions);
$this->baseRenderingContext->setTemplatePaths($newTemplatePathsResolver);

isset($renderingOptions['templatePathPattern']);

if ($this->getCurrentRenderingContext() === null) {
// We do not have a "current" rendering context yet, so we use the base rendering context
Expand All @@ -164,14 +170,14 @@ public function renderRenderable(\TYPO3\Form\Core\Model\Renderable\RootRenderabl
} else {
$renderingContext = clone $this->getCurrentRenderingContext();
}
$renderingContext->getViewHelperVariableContainer()->addOrUpdate('TYPO3\Form\Core\Renderer\FluidFormRenderer', 'currentRenderable', $renderable);
$renderingContext->getViewHelperVariableContainer()->addOrUpdate(FluidFormRenderer::class, 'currentRenderable', $renderable);

if (!isset($renderingOptions['renderableNameInTemplate'])) {
throw new \TYPO3\Form\Exception\RenderingException(sprintf('The Renderable "%s" did not have the rendering option "renderableNameInTemplate" defined.', $renderableType), 1326094948);
}

$templateVariableContainer = new \TYPO3\Fluid\Core\ViewHelper\TemplateVariableContainer(array($renderingOptions['renderableNameInTemplate'] => $renderable));
$renderingContext->injectTemplateVariableContainer($templateVariableContainer);
$templateVariableContainer = new TemplateVariableContainer(array($renderingOptions['renderableNameInTemplate'] => $renderable));
$renderingContext->setVariableProvider($templateVariableContainer);

if ($parsedRenderable->hasLayout()) {
$renderableLayoutName = $parsedRenderable->getLayoutName($renderingContext);
Expand All @@ -187,9 +193,45 @@ public function renderRenderable(\TYPO3\Form\Core\Model\Renderable\RootRenderabl
$this->stopRendering();
}

$this->baseRenderingContext->setTemplatePaths($currentTemplatePathsResolver);
return $output;
}

public function renderPartial($partialName, $sectionName, array $variables, $ignoreUnknown = false)
{
return parent::renderPartial($partialName, $sectionName, $variables, $ignoreUnknown); // TODO: Change the autogenerated stub
}

/**
* @param array $renderingOptions
* @return TemplatePaths
*/
protected function createTemplatePathsResolverWithRenderingOptions($renderingOptions)
{
/** @var TemplatePaths $newTemplatePathsResolver */
$newTemplatePathsResolver = clone $this->getTemplatePaths();

if (isset($renderingOptions['templatePathPattern'])) {
$pathPattern = str_replace('{@type}', '@action', $renderingOptions['templatePathPattern']);
$pathPattern = preg_replace('/{(@[a-zA-z0-9_-]*)}/', '$1', $pathPattern);
$newTemplatePathsResolver->setOption('templatePathAndFilenamePattern', $pathPattern);
}

if (isset($renderingOptions['partialPathPattern'])) {
$pathPattern = str_replace('{@type}', '@partial', $renderingOptions['partialPathPattern']);
$pathPattern = preg_replace('/{(@[a-zA-z0-9_-]*)}/', '$1', $pathPattern);
$newTemplatePathsResolver->setOption('partialPathAndFilenamePattern', $pathPattern);
}

if (isset($renderingOptions['layoutPathPattern'])) {
$pathPattern = str_replace('{@type}', '@action', $renderingOptions['layoutPathPattern']);
$pathPattern = preg_replace('/{(@[a-zA-z0-9_-]*)}/', '$1', $pathPattern);
$newTemplatePathsResolver->setOption('layoutPathAndFilenamePattern', $pathPattern);
}

return $newTemplatePathsResolver;
}

/**
* Get full template path and filename for the given $renderableType.
*
Expand Down Expand Up @@ -245,7 +287,7 @@ protected function getPathAndFilenameForRenderableLayout($renderableType, array
*
* @param string $renderableType The name of the partial
* @return string the full path which should be used. The path definitely exists.
* @throws \TYPO3\Fluid\View\Exception\InvalidTemplateResourceException
* @throws \Neos\FluidAdaptor\View\Exception\InvalidTemplateResourceException
* @throws \TYPO3\Form\Exception\RenderingException
*/
protected function getPartialPathAndFilename($renderableType)
Expand All @@ -265,7 +307,7 @@ protected function getPartialPathAndFilename($renderableType)
if (file_exists($partialPath)) {
return $partialPath;
}
throw new \TYPO3\Fluid\View\Exception\InvalidTemplateResourceException('The template file "' . $partialPath . '" could not be loaded.', 1326713418);
throw new \Neos\FluidAdaptor\View\Exception\InvalidTemplateResourceException('The template file "' . $partialPath . '" could not be loaded.', 1326713418);
}

/**
Expand All @@ -275,8 +317,8 @@ protected function getPartialPathAndFilename($renderableType)
*
* @param string $renderableType
* @param string $renderablePathAndFilename
* @return \TYPO3\Fluid\Core\Parser\ParsedTemplateInterface
* @return \TYPO3\Fluid\Core\Parser\ParsedTemplateInterface
* @return \Neos\FluidAdaptor\Core\Parser\ParsedTemplateInterface
* @return \Neos\FluidAdaptor\Core\Parser\ParsedTemplateInterface
* @throws \TYPO3\Form\Exception
* @internal
*/
Expand All @@ -288,14 +330,13 @@ protected function getParsedRenderable($renderableType, $renderablePathAndFilena
$templateModifiedTimestamp = \filemtime($renderablePathAndFilename);
$renderableIdentifier = sprintf('renderable_%s_%s', str_replace(array('.', ':'), '_', $renderableType), sha1($renderablePathAndFilename . '|' . $templateModifiedTimestamp));

if ($this->templateCompiler->has($renderableIdentifier)) {
$parsedRenderable = $this->templateCompiler->get($renderableIdentifier);
} else {
$parsedRenderable = $this->templateParser->parse(file_get_contents($renderablePathAndFilename));
if ($parsedRenderable->isCompilable()) {
$this->templateCompiler->store($renderableIdentifier, $parsedRenderable);
$parsedRenderable = $this->baseRenderingContext->getTemplateParser()->getOrParseAndStoreTemplate(
$renderableIdentifier,
function () use ($renderablePathAndFilename) {
return file_get_contents($renderablePathAndFilename);
}
}
);

return $parsedRenderable;
}
}
21 changes: 12 additions & 9 deletions Classes/TYPO3/Form/Finishers/EmailFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* *
* The TYPO3 project - inspiring people to share! *
* */
use Neos\FluidAdaptor\View\StandaloneView;
use TYPO3\Form\Core\Model\AbstractFinisher;
use TYPO3\Form\Exception\FinisherException;

/**
* This finisher sends an email to one recipient
Expand All @@ -36,7 +39,7 @@
* - format: format of the email (one of the FORMAT_* constants). By default mails are sent as HTML
* - testMode: if TRUE the email is not actually sent but outputted for debugging purposes. Defaults to FALSE
*/
class EmailFinisher extends \TYPO3\Form\Core\Model\AbstractFinisher
class EmailFinisher extends AbstractFinisher
{
const FORMAT_PLAINTEXT = 'plaintext';
const FORMAT_HTML = 'html';
Expand All @@ -56,7 +59,7 @@ class EmailFinisher extends \TYPO3\Form\Core\Model\AbstractFinisher
* @see AbstractFinisher::execute()
*
* @return void
* @throws \TYPO3\Form\Exception\FinisherException
* @throws FinisherException
*/
protected function executeInternal()
{
Expand All @@ -77,13 +80,13 @@ protected function executeInternal()
$testMode = $this->parseOption('testMode');

if ($subject === null) {
throw new \TYPO3\Form\Exception\FinisherException('The option "subject" must be set for the EmailFinisher.', 1327060320);
throw new FinisherException('The option "subject" must be set for the EmailFinisher.', 1327060320);
}
if ($recipientAddress === null) {
throw new \TYPO3\Form\Exception\FinisherException('The option "recipientAddress" must be set for the EmailFinisher.', 1327060200);
throw new FinisherException('The option "recipientAddress" must be set for the EmailFinisher.', 1327060200);
}
if ($senderAddress === null) {
throw new \TYPO3\Form\Exception\FinisherException('The option "senderAddress" must be set for the EmailFinisher.', 1327060210);
throw new FinisherException('The option "senderAddress" must be set for the EmailFinisher.', 1327060210);
}

$mail = new \TYPO3\SwiftMailer\Message();
Expand Down Expand Up @@ -130,14 +133,14 @@ protected function executeInternal()
}

/**
* @return \TYPO3\Fluid\View\StandaloneView
* @throws \TYPO3\Form\Exception\FinisherException
* @return StandaloneView
* @throws FinisherException
*/
protected function initializeStandaloneView()
{
$standaloneView = new \TYPO3\Fluid\View\StandaloneView();
$standaloneView = new StandaloneView();
if (!isset($this->options['templatePathAndFilename'])) {
throw new \TYPO3\Form\Exception\FinisherException('The option "templatePathAndFilename" must be set for the EmailFinisher.', 1327058829);
throw new FinisherException('The option "templatePathAndFilename" must be set for the EmailFinisher.', 1327058829);
}
$standaloneView->setTemplatePathAndFilename($this->options['templatePathAndFilename']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* */

use TYPO3\Flow\Annotations as Flow;
use TYPO3\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper;
use Neos\FluidAdaptor\ViewHelpers\Form\AbstractFormFieldViewHelper;

/**
* Display a jQuery date picker.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@
* The TYPO3 project - inspiring people to share! *
* */

use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Form\Core\Model\Renderable\RenderableInterface;

/**
* Form Element Rootline Path
*/
class FormElementRootlinePathViewHelper extends \TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper
class FormElementRootlinePathViewHelper extends AbstractViewHelper
{
/**
* @param \TYPO3\Form\Core\Model\Renderable\RenderableInterface $renderable
* @param RenderableInterface $renderable
* @return string
*/
public function render(\TYPO3\Form\Core\Model\Renderable\RenderableInterface $renderable)
public function render(RenderableInterface $renderable)
{
$path = $renderable->getIdentifier();
while ($renderable = $renderable->getParentRenderable()) {
Expand Down
3 changes: 2 additions & 1 deletion Classes/TYPO3/Form/ViewHelpers/Form/TimePickerViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
* The TYPO3 project - inspiring people to share! *
* */

use Neos\FluidAdaptor\ViewHelpers\Form\AbstractFormFieldViewHelper;
use TYPO3\Flow\Annotations as Flow;

/**
* Displays two select-boxes for hour and minute selection.
*/
class TimePickerViewHelper extends \TYPO3\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
class TimePickerViewHelper extends AbstractFormFieldViewHelper
{
/**
* @var string
Expand Down
13 changes: 8 additions & 5 deletions Classes/TYPO3/Form/ViewHelpers/Form/UploadedImageViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
* */

use Doctrine\ORM\Mapping as ORM;
use Neos\FluidAdaptor\ViewHelpers\Form\AbstractFormFieldViewHelper;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Property\PropertyMapper;
use TYPO3\Media\Domain\Model\Image;

/**
* This ViewHelper makes the specified Image object available for its
Expand All @@ -32,10 +35,10 @@
* <a href="...">Link to image resource</a>
* </output>
*/
class UploadedImageViewHelper extends \TYPO3\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
class UploadedImageViewHelper extends AbstractFormFieldViewHelper
{
/**
* @var \TYPO3\Flow\Property\PropertyMapper
* @var PropertyMapper
* @Flow\Inject
*/
protected $propertyMapper;
Expand Down Expand Up @@ -70,17 +73,17 @@ public function render($as = 'image')
* Returns a previously uploaded image.
* If errors occurred during property mapping for this property, NULL is returned
*
* @return \TYPO3\Media\Domain\Model\Image
* @return Image
*/
protected function getUploadedImage()
{
if ($this->getMappingResultsForProperty()->hasErrors()) {
return null;
}
$image = $this->getValue(false);
if ($image instanceof \TYPO3\Media\Domain\Model\Image) {
if ($image instanceof Image) {
return $image;
}
return $this->propertyMapper->convert($image, 'TYPO3\Media\Domain\Model\Image');
return $this->propertyMapper->convert($image, Image::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
* */

use Doctrine\ORM\Mapping as ORM;
use Neos\FluidAdaptor\ViewHelpers\Form\AbstractFormFieldViewHelper;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Property\PropertyMapper;
use TYPO3\Flow\Resource\Resource as PersistentResource;

/**
* This ViewHelper makes the specified Resource object available for its
Expand All @@ -34,10 +37,10 @@
* <a href="...">Link to resource</a>
* </output>
*/
class UploadedResourceViewHelper extends \TYPO3\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
class UploadedResourceViewHelper extends AbstractFormFieldViewHelper
{
/**
* @var \TYPO3\Flow\Property\PropertyMapper
* @var PropertyMapper
* @Flow\Inject
*/
protected $propertyMapper;
Expand Down Expand Up @@ -73,17 +76,17 @@ public function render($as = 'resource')
* Returns a previously uploaded resource.
* If errors occurred during property mapping for this property, NULL is returned
*
* @return \TYPO3\Flow\Resource\Resource
* @return Resource
*/
protected function getUploadedResource()
{
if ($this->getMappingResultsForProperty()->hasErrors()) {
return null;
}
$resourceObject = $this->getValue(false);
if ($resourceObject instanceof \TYPO3\Flow\Resource\Resource) {
if ($resourceObject instanceof PersistentResource) {
return $resourceObject;
}
return $this->propertyMapper->convert($resourceObject, 'TYPO3\Flow\Resource\Resource');
return $this->propertyMapper->convert($resourceObject, PersistentResource::class);
}
}
5 changes: 3 additions & 2 deletions Classes/TYPO3/Form/ViewHelpers/FormViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
* The TYPO3 project - inspiring people to share! *
* */

use Neos\FluidAdaptor\ViewHelpers\FormViewHelper as FluidFormViewHelper;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Mvc\ActionRequest;
use TYPO3\Fluid\Core\ViewHelper\TagBuilder;
use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;

/**
* Custom form ViewHelper that renders the form state instead of referrer fields
*/
class FormViewHelper extends \TYPO3\Fluid\ViewHelpers\FormViewHelper
class FormViewHelper extends FluidFormViewHelper
{
/**
* @Flow\Inject
Expand Down

0 comments on commit e9c938b

Please sign in to comment.