Created
July 17, 2012 12:01
-
-
Save PeterTheOne/3129052 to your computer and use it in GitHub Desktop.
Persist a TYPO3.Form to my own domain model by extending RedirectFinisher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Shows a form for creating a new account object | |
* | |
* @return void | |
*/ | |
public function newAction() { | |
} | |
/** | |
* Adds the given new account object to the account repository | |
* | |
* @param string $username | |
* @param string $firstname | |
* @param string $lastname | |
* @param string $password1 | |
* | |
* @return void | |
*/ | |
public function createAction($username, $firstname, $lastname, $password1) { | |
$roles = array('Customer'); | |
// create a account with password an add it to the accountRepository | |
$account = $this->accountFactory->createAccountWithPassword($username, $password1, $roles); | |
$this->accountRepository->add($account); | |
// create person with firstName, lastname and account | |
$personName = new \TYPO3\Party\Domain\Model\PersonName('', $firstname, '', $lastname); | |
$person = new \TYPO3\Party\Domain\Model\Person(); | |
$person->setName($personName); | |
$person->addAccount($account); | |
$this->partyRepository->add($person); | |
// authenticate Account | |
$authenticationTokens = $this->securityContext->getAuthenticationTokensOfType('TYPO3\FLOW3\Security\Authentication\Token\UsernamePassword'); | |
if (count($authenticationTokens) === 1) { | |
$authenticationTokens[0]->setAccount($account); | |
$authenticationTokens[0]->setAuthenticationStatus(\TYPO3\FLOW3\Security\Authentication\TokenInterface::AUTHENTICATION_SUCCESSFUL); | |
} | |
// add a message and redirect to the login form | |
$this->addFlashMessage('Account created.'); | |
$this->redirect('index', 'Login'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param array $factorySpecificConfiguration | |
* @param string $presetName | |
* | |
* @return \TYPO3\Form\Core\Model\FormDefinition | |
*/ | |
public function build(array $factorySpecificConfiguration, $presetName) { | |
$formConfiguration = $this->getPresetConfiguration($presetName); | |
$form = new FormDefinition('contactForm', $formConfiguration); | |
$page1 = $form->createPage('page1'); | |
$userName = $page1->createElement('userName', 'TYPO3.Form:SingleLineText'); | |
$userName->setLabel('Username'); | |
$userName->addValidator(new \TYPO3\FLOW3\Validation\Validator\NotEmptyValidator()); | |
$firstName = $page1->createElement('firstName', 'TYPO3.Form:SingleLineText'); | |
$firstName->setLabel('First Name'); | |
$firstName->addValidator(new \TYPO3\FLOW3\Validation\Validator\NotEmptyValidator()); | |
$lastName = $page1->createElement('lastName', 'TYPO3.Form:SingleLineText'); | |
$lastName->setLabel('Last Name'); | |
$lastName->addValidator(new \TYPO3\FLOW3\Validation\Validator\NotEmptyValidator()); | |
$password = $page1->createElement('password', 'TYPO3.Form:PasswordWithConfirmation'); | |
$password->setLabel('Password'); | |
$password->addValidator(new \TYPO3\FLOW3\Validation\Validator\NotEmptyValidator()); | |
$password->addValidator(new \TYPO3\FLOW3\Validation\Validator\StringLengthValidator(array('minimum' => 8))); | |
$redirectFinisher = new \My\Package\FormFinishers\ExtendedRedirectFinisher(); | |
$redirectFinisher->setOptions( | |
array( | |
'action' => 'create', | |
'arguments' => array( | |
'username' => '{userName}', | |
'firstname' => '{firstName}', | |
'lastname' => '{lastName}', | |
'password1' => '{password}' | |
) | |
) | |
); | |
$form->addFinisher($redirectFinisher); | |
return $form; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Read the option called $optionName from $this->options, and parse {...} | |
* as object accessors. | |
* | |
* if $optionName was not found, the corresponding default option is returned (from $this->defaultOptions) | |
* | |
* @param string $optionName | |
* @return mixed | |
* @api | |
*/ | |
protected function parseOptionArguments($optionName) { | |
$option = $this->options['arguments'][$optionName]; | |
if (!is_string($option)) { | |
return $option; | |
} | |
$formRuntime = $this->finisherContext->getFormRuntime(); | |
return preg_replace_callback('/{([^}]+)}/', function($match) use ($formRuntime) { | |
return \TYPO3\FLOW3\Reflection\ObjectAccess::getPropertyPath($formRuntime, $match[1]); | |
}, $option); | |
} | |
/** | |
* Executes this finisher | |
* @see AbstractFinisher::execute() | |
* | |
* @return void | |
* @throws \TYPO3\Form\Exception\FinisherException | |
*/ | |
public function executeInternal() { | |
$formRuntime = $this->finisherContext->getFormRuntime(); | |
$request = $formRuntime->getRequest()->getMainRequest(); | |
$packageKey = $this->parseOption('package'); | |
$controllerName = $this->parseOption('controller'); | |
$actionName = $this->parseOption('action'); | |
$arguments = $this->options['arguments']; | |
foreach ($arguments as $key => $argument) { | |
$arguments[$key] = $this->parseOptionArguments($key); | |
} | |
$delay = (integer)$this->parseOption('delay'); | |
$statusCode = $this->parseOption('statusCode'); | |
$subpackageKey = NULL; | |
if ($packageKey !== NULL && strpos($packageKey, '\\') !== FALSE) { | |
list($packageKey, $subpackageKey) = explode('\\', $packageKey, 2); | |
} | |
$uriBuilder = new \TYPO3\FLOW3\Mvc\Routing\UriBuilder(); | |
$uriBuilder->setRequest($request); | |
$uriBuilder->reset(); | |
$uri = $uriBuilder->uriFor($actionName, $arguments, $controllerName, $packageKey, $subpackageKey); | |
$uri = $request->getHttpRequest()->getBaseUri() . $uri; | |
$escapedUri = htmlentities($uri, ENT_QUOTES, 'utf-8'); | |
$response = $formRuntime->getResponse(); | |
$response->setContent('<html><head><meta http-equiv="refresh" content="' . $delay . ';url=' . $escapedUri . '"/></head></html>'); | |
$response->setStatus($statusCode); | |
if ($delay === 0) { | |
$response->setHeader('Location', (string)$uri); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{namespace form=TYPO3\Form\ViewHelpers} | |
<f:layout name="Default" /> | |
<f:section name="Title">New account</f:section> | |
<f:section name="Content"> | |
<p>Just fill out the following form to create a new account:</p> | |
<form:render factoryClass="My\Package\FormFactories\AccountFactory" /> | |
</f:section> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment