Skip to content

Instantly share code, notes, and snippets.

@mneuhaus
Created December 11, 2015 09:21
Show Gist options
  • Select an option

  • Save mneuhaus/7f12dc4cb957446a478d to your computer and use it in GitHub Desktop.

Select an option

Save mneuhaus/7f12dc4cb957446a478d to your computer and use it in GitHub Desktop.
Complete Contact form in Fluidcontent
{namespace v=FluidTYPO3\Vhs\ViewHelpers}
{namespace flux=FluidTYPO3\Flux\ViewHelpers}
{namespace t=Famelo\Template\ViewHelpers}
<f:layout name="{v:extension.loaded(extensionName: 'fluidcontent_core', then: 'ContentCore', else: 'Content')}" />
<f:section name="Configuration">
<flux:form id="contact" options="{group: 'forms'}" label="Contact">
<flux:field.text name="introduction" label="Einleitung" rows="3" />
<flux:field.input name="recipient" label="Empfänger" required="1" />
<flux:field.input name="subject" label="E-Mail Betreff" required="1" />
<flux:field.text name="successMessage" label="Bestätigungstext" rows="2" />
<t:flux.validate name="name" as="NotEmpty" />
<t:flux.validate name="email" as="EmailAddress" />
<t:flux.validate name="email" as="NotEmpty" />
<t:flux.validate name="message" as="NotEmpty" />
</flux:form>
</f:section>
<f:section name="Main">
{introduction -> f:format.nl2br()}
<f:form extensionName="template" pluginName="content" id="form{record.uid}" noCache="1">
<f:form.hidden name="nextSection" value="Success" />
<f:form.textfield name="name" class="form-control" placeholder="Name" value="{name}"/>
<f:if condition="{validationResults.name}">
<f:for each="{validationResults.name}" as="error">
<f:translate key="LLL:EXT:template/Resources/Private/Language/validation.xlf:error.{error.code}" default="{error.code}: {error.message}"/>
</f:for>
</f:if>
<f:form.textfield name="email" class="form-control" placeholder="E-Mail" value="{email}"/>
<f:if condition="{validationResults.email}">
<f:for each="{validationResults.email}" as="error">
<f:translate key="LLL:EXT:template/Resources/Private/Language/validation.xlf:error.{error.code}" default="{error.code}: {error.message}"/>
</f:for>
</f:if>
<f:form.textarea name="message" class="form-control" placeholder="Nachricht" value="{message}"/>
<f:if condition="{validationResults.message}">
<f:for each="{validationResults.message}" as="error">
<f:translate key="LLL:EXT:template/Resources/Private/Language/validation.xlf:error.{error.code}" default="{error.code}: {error.message}"/>
</f:for>
</f:if>
<button type="submit">Absenden</button>
</f:form>
</f:section>
<f:section name="Success">
<div id="form{record.uid}">
{successMessage}
</div>
<t:sendMail subject="{subject}" recipients="{recipient}">
<strong>{name}</strong>
<strong>{E-Mail}</strong>
<p>{message}</p>
</t:sendMail>
</f:section>
<?php
namespace Famelo\Template\Controller;
/*
* This file is part of the FluidTYPO3/Fluidcontent project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Fluidcontent\Controller\AbstractContentController;
use FluidTYPO3\Fluidcontent\Controller\ContentControllerInterface;
use Famelo\Template\ViewHelpers\Flux\ValidateViewHelper;
use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator;
/**
* Flexible Content Element Plugin Rendering Controller
*
* @route off
*/
class ContentController extends AbstractContentController implements ContentControllerInterface {
/**
* Calls the specified action method and passes the arguments.
*
* If the action returns a string, it is appended to the content in the
* response object. If the action doesn't return anything and a valid
* view exists, the view is rendered automatically.
*
* @return void
* @api
*/
protected function callActionMethod()
{
$preparedArguments = array();
/** @var \TYPO3\CMS\Extbase\Mvc\Controller\Argument $argument */
foreach ($this->arguments as $argument) {
$preparedArguments[] = $argument->getValue();
}
$this->emitBeforeCallActionMethodSignal($preparedArguments);
$actionResult = call_user_func_array(array($this, $this->actionMethodName), $preparedArguments);
if ($actionResult === null && $this->view instanceof ViewInterface) {
$this->response->appendContent($this->view->render());
} elseif (is_string($actionResult) && $actionResult !== '') {
$this->response->appendContent($actionResult);
} elseif (is_object($actionResult) && method_exists($actionResult, '__toString')) {
$this->response->appendContent((string)$actionResult);
}
}
/**
* Adds the needed validators to the Arguments:
*
* - Validators checking the data type from the @param annotation
* - Custom validators specified with validate annotations.
* - Model-based validators (validate annotations in the model)
* - Custom model validator classes
*
* @return void
*/
protected function initializeActionMethodValidators()
{
parent::initializeActionMethodValidators();
foreach(ValidateViewHelper::getValidations() as $argument => $validators) {
$argument = $this->arguments->addNewArgument($argument, 'string');
$conjunctionValidator = new ConjunctionValidator();
foreach ($validators as $validatorName) {
$validator = $this->validatorResolver->createValidator($validatorName);
$conjunctionValidator->addValidator($validator);
}
$argument->setValidator($conjunctionValidator);
}
}
public function formAction() {
$validationResult = $this->arguments->getValidationResults();
$arguments = array_merge($this->settings, $this->request->getArguments(), array(
'page' => $GLOBALS['TSFE']->page,
'user' => $GLOBALS['TSFE']->fe_user->user,
'record' => $this->getRecord(),
'contentObject' => $this->configurationManager->getContentObject(),
'cookies' => $_COOKIE,
'session' => $_SESSION
));
if ($validationResult->hasErrors()) {
$arguments['validationResults'] = $validationResult->getFlattenedErrors();
return $this->view->renderStandaloneSection('Main', $arguments);
}
if ($this->request->hasArgument('nextSection')) {
return $this->view->renderStandaloneSection($this->request->getArgument('nextSection'), $arguments);
}
}
public function contactAction() {
return $this->formAction();
}
}
<?php
namespace Famelo\Template\ViewHelpers;
/* *
* This script is part of the TYPO3 project - inspiring people to share! *
* *
* TYPO3 is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License version 2 as published by *
* the Free Software Foundation. *
* *
* This script is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
* TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
* Public License for more details. *
* */
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Frontend\Category\Collection\CategoryCollection;
/**
*/
class SendMailViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
/**
*
* @param string $subject
* @param string $recipients
* @return string Rendered tag
*/
public function render($subject, $recipients) {
$mail = new MailMessage();
$mail->setSubject($subject);
$mail->setTo(explode(',', $recipients));
$mail->setBody($this->renderChildren(), 'text/html');
$mail->setFrom('mneuhaus@famelo.com');
$mail->send();
}
}
<?php
namespace Famelo\Template\ViewHelpers\Flux;
/*
* This file is part of the FluidTYPO3/Flux project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Flux\ViewHelpers\AbstractFormViewHelper;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
/**
*/
class ValidateViewHelper extends AbstractFormViewHelper {
/**
* @var array
*/
protected static $validations = array();
/**
* Initialize
* @return void
*/
public function initializeArguments() {
parent::initializeArguments();
$this->registerArgument('name', 'string', 'Name of the argument to validate', TRUE);
$this->registerArgument('as', 'string', 'validator to apply', TRUE);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return void
*/
static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
if (!isset(static::$validations[$arguments['name']])) {
static::$validations[$arguments['name']] = array();
}
static::$validations[$arguments['name']][$arguments['as']] = $arguments['as'];
}
static public function getValidations() {
return static::$validations;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment