Skip to content

Instantly share code, notes, and snippets.

@mficzel
Last active July 3, 2020 08:17
Show Gist options
  • Save mficzel/ae75da88cc56f0bb378be113acc557e6 to your computer and use it in GitHub Desktop.
Save mficzel/ae75da88cc56f0bb378be113acc557e6 to your computer and use it in GitHub Desktop.
Contact-form example with Neos.Fusion.Form and a custom controller
<?php
namespace Vendor\Site\Controller;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\SwiftMailer\Message as SwiftMailerMessage;
class ContactController extends ActionController
{
/**
* @var string
* @Flow\InjectConfiguration(path="contact.recipient")
*/
protected $recipient;
/**
* @var string
* @Flow\InjectConfiguration(path="contact.sender")
*/
protected $sender;
/**
* @var string
* @Flow\InjectConfiguration(path="contact.subject")
*/
protected $subject;
/**
* @var string
* @Flow\InjectConfiguration(path="contact.thankYouPath")
*/
protected $thankYouPath;
/**
* @param string $name
* @param string $email
* @param string $phone
* @param string $message
*
* @Flow\Validate(argumentName="name", type="NotEmpty")
* @Flow\Validate(argumentName="email", type="EmailAddress")
* @Flow\Validate(argumentName="email", type="NotEmpty")
* @Flow\Validate(argumentName="phone", type="NotEmpty")
* @Flow\Validate(argumentName="message", type="NotEmpty")
*/
public function contactAction(string $name, string $email, string $phone, string $message)
{
$mail = new SwiftMailerMessage();
$mail
->setFrom($email, $name)
->setSubject($this->subject);
$mail->setTo($this->recipient);
// build body
$content = "";
$content .= "Name: " . strip_tags($name) .chr(10);
$content .= "Email: " . strip_tags($email) .chr(10);
$content .= "Phone: " . strip_tags($phone).chr(10);
$content .= "Message: " .chr(10) . strip_tags($message);
$mail->setBody($content);
// send
$mail->send();
// redirect
$uri = $this->controllerContext->getRequest()->getHttpRequest()->getUri();
$uri = $uri->withPath($this->thankYouPath)->withQuery('');
$this->redirectToUri($uri);
}
}
renderer = afx`
<Neos.Fusion.Form:Form
form.target.package="Vendor.Site"
form.target.controller="Contact"
form.target.action="contact"
>
<Neos.Fusion.Form:Textfield
field.name="name"
attributes.placeholder="Name"
attributes.required="" />
<Neos.Fusion.Form:Input
field.name="email"
attributes.type="email"
attributes.placeholder="Email"
attributes.required="" />
<Neos.Fusion.Form:Textfield
field.name="phone"
attributes.type="text"
attributes.placeholder="Telefon"
attributes.required="" />
<Neos.Fusion.Form:Textarea
field.name="message"
attributes.rows="6"
attributes.placeholder="Nachricht"
attributes.required=""
></Neos.Fusion.Form:Textarea>
<Neos.Fusion.Form:Button attributes.type="submit">Submit</Neos.Fusion.Form:Button>
</Neos.Fusion.Form:Form>
`
privilegeTargets:
'Neos\Flow\Security\Authorization\Privilege\Method\MethodPrivilege':
'Vendor.Site:Contact':
matcher: 'method(Vendor\Site\Controller\ContactController->contactAction())'
roles:
'Neos.Flow:Everybody':
privileges:
-
privilegeTarget: 'Vendor.Site:Contact'
permission: GRANT
-
name: 'Contact'
uriPattern: 'contact/email'
defaults:
'@package': 'Vendor.Site'
'@controller': 'Contact'
'@action': 'contact'
'@format': 'html'
appendExceedingArguments: true
Neos:
Flow:
mvc:
routes:
Vendor.Site:
position: 'before Neos.Neos'
Vendor:
Site:
contact:
recipient: "[email protected]"
subject: "Contact form was sent"
from: "[email protected]"
thankYouPath: "/thankyou.html"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment