Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to define landing page when form is submitted successfully #287

Open
wants to merge 1 commit into
base: 2.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Configuration/FormConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class FormConfiguration implements FormConfigurationInterface
*/
private $fileSave = true;

/**
* @var string|null
*/
private ?string $successRedirect = null;

/**
* @var MailConfigurationInterface
*/
Expand Down Expand Up @@ -148,4 +153,16 @@ public function setSave(bool $save): self

return $this;
}

public function getSuccessRedirect(): ?string
{
return $this->successRedirect;
}

public function setSuccessRedirect(?string $successRedirect): self
{
$this->successRedirect = $successRedirect;

return $this;
}
}
1 change: 1 addition & 0 deletions Configuration/FormConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function buildByDynamic(Dynamic $dynamic): FormConfigurationInterface
$config = $this->create($locale);
$config->setFileFields($this->getFileFieldsByDynamic($dynamic));
$config->setFileSave(!$translation->getDeactivateAttachmentSave());
$config->setSuccessRedirect($translation->getTargetSuccess());

$adminMailConfiguration = $this->buildAdminMailConfigurationByDynamic($dynamic);
$websiteMailConfiguration = $this->buildWebsiteMailConfigurationByDynamic($dynamic);
Expand Down
1 change: 1 addition & 0 deletions Controller/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ private function getApiEntity(Form $entity, string $locale): array
'toName' => $translation->getToName(),
'subject' => $translation->getSubject(),
'mailText' => $translation->getMailText(),
'targetSuccess' => $translation->getTargetSuccess(),
'submitLabel' => $translation->getSubmitLabel(),
'successText' => $translation->getSuccessText(),
'sendAttachments' => $translation->getSendAttachments(),
Expand Down
1 change: 1 addition & 0 deletions Entity/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public function serializeForLocale(string $locale, ?Dynamic $dynamic = null): ar
'title' => $translation->getTitle(),
'subject' => $translation->getSubject(),
'mailText' => $translation->getMailText(),
'targetSuccess' => $translation->getTargetSuccess(),
'submitLabel' => $translation->getSubmitLabel(),
'successText' => $translation->getSuccessText(),
'fromEmail' => $translation->getFromEmail(),
Expand Down
17 changes: 17 additions & 0 deletions Entity/FormTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class FormTranslation implements AuditableInterface
*/
private $mailText;

/**
* @var null|string
*/
private $targetSuccess;

/**
* @var null|string
*/
Expand Down Expand Up @@ -202,6 +207,18 @@ public function getMailText(): ?string
return $this->mailText;
}

public function getTargetSuccess(): ?string
{
return $this->targetSuccess;
}

public function setTargetSuccess(?string $targetSuccess): self
{
$this->targetSuccess = $targetSuccess;

return $this;
}

public function setSubmitLabel(?string $submitLabel): self
{
$this->submitLabel = $submitLabel;
Expand Down
7 changes: 6 additions & 1 deletion Event/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ public function onKernelRequest(RequestEvent $event): void
$dynFormSavedEvent = new DynFormSavedEvent($serializedObject, $dynamic);
$this->eventDispatcher->dispatch($dynFormSavedEvent, DynFormSavedEvent::NAME);

$response = new RedirectResponse('?send=true');
if ($form->get('targetSuccess') && !empty($form->get('targetSuccess')->getData())) {
$response = new RedirectResponse($form->get('targetSuccess')->getData());
} else {
$response = new RedirectResponse('?send=true');
}

$event->setResponse($response);
}
}
Expand Down
22 changes: 22 additions & 0 deletions Form/Type/DynamicFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sulu\Bundle\FormBundle\Entity\Dynamic;
use Sulu\Bundle\FormBundle\Entity\Form;
use Sulu\Bundle\FormBundle\Exception\FormNotFoundException;
use Sulu\Component\Content\Mapper\ContentMapperInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
Expand All @@ -41,17 +42,24 @@ class DynamicFormType extends AbstractType
*/
private $honeyPotField;

/**
* @var ContentMapperInterface
*/
private $contentMapper;

/**
* DynamicFormType constructor.
*/
public function __construct(
FormFieldTypePool $typePool,
Checksum $checksum,
ContentMapperInterface $contentMapper,
?string $honeyPotField = null
) {
$this->typePool = $typePool;
$this->checksum = $checksum;
$this->honeyPotField = $honeyPotField;
$this->contentMapper = $contentMapper;
}

/**
Expand Down Expand Up @@ -167,6 +175,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
);
}

// Redirect URL after success
if ($translation->getTargetSuccess()) {
$contentStructure = $this->contentMapper->load(
$translation->getTargetSuccess(),
$builder->getData()->getWebspaceKey(),
$locale
);

$builder->add('targetSuccess', HiddenType::class, [
'data' => $contentStructure->getPath(),
'mapped' => false,
]);
}

// Add submit button.
$builder->add(
'submit',
Expand Down
1 change: 1 addition & 0 deletions Manager/FormManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public function save(array $data, ?string $locale = null, ?int $id = null): ?For
$translation->setToEmail(self::getValue($data, 'toEmail'));
$translation->setToName(self::getValue($data, 'toName'));
$translation->setMailText(self::getValue($data, 'mailText'));
$translation->setTargetSuccess(self::getValue($data, 'targetSuccess'));
$translation->setSubmitLabel(self::getValue($data, 'submitLabel'));
$translation->setSuccessText(self::getValue($data, 'successText'));
$translation->setSendAttachments(self::getValue($data, 'sendAttachments', false));
Expand Down
1 change: 1 addition & 0 deletions Resources/config/doctrine/FormTranslation.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<field name="toEmail" column="toEmail" type="string" length="255" nullable="true"/>
<field name="toName" column="toName" type="string" length="255" nullable="true"/>
<field name="mailText" column="mailText" type="text" nullable="true"/>
<field name="targetSuccess" column="targetSuccess" type="string" nullable="true"/>
<field name="submitLabel" column="submitLabel" type="string" nullable="true"/>
<field name="successText" column="successText" type="text" nullable="true"/>
<field name="sendAttachments" column="sendAttachments" type="boolean" nullable="false">
Expand Down
6 changes: 6 additions & 0 deletions Resources/config/forms/form_details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
</meta>

<properties>
<property name="targetSuccess" type="single_page_selection">
<meta>
<title lang="en">sulu_form.success_landing_page</title>
</meta>
</property>

<property name="submitLabel" type="text_line">
<meta>
<title>sulu_form.submit_label</title>
Expand Down
9 changes: 5 additions & 4 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<service id="sulu_form.form_type" class="Sulu\Bundle\FormBundle\Form\Type\DynamicFormType">
<argument type="service" id="sulu_form.dynamic.form_field_type_pool" />
<argument type="service" id="sulu_form.checksum" />
<argument type="service" id="sulu.content.mapper"/>
<argument>%sulu_form.honeypot_field%</argument>

<tag name="form.type"/>
Expand Down Expand Up @@ -175,8 +176,8 @@

<!-- Properties XML Loader -->
<service
id="sulu_form.metadata.properties_xml_loader"
class="Sulu\Bundle\FormBundle\Metadata\PropertiesXmlLoader"
id="sulu_form.metadata.properties_xml_loader"
class="Sulu\Bundle\FormBundle\Metadata\PropertiesXmlLoader"
>
<argument type="service" id="sulu_page.structure.properties_xml_parser"/>
</service>
Expand Down Expand Up @@ -231,8 +232,8 @@

<!-- Form Website Controller -->
<service id="sulu_form.form_website_controller"
class="Sulu\Bundle\FormBundle\Controller\FormWebsiteController"
public="true">
class="Sulu\Bundle\FormBundle\Controller\FormWebsiteController"
public="true">
<tag name="container.service_subscriber" />
<tag name="controller.service_arguments" />
<tag name="sulu.context" context="website"/>
Expand Down
3 changes: 2 additions & 1 deletion Resources/translations/admin.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@
"sulu_form.salutation_mr": "Mr.",
"sulu_form.salutation_ms": "Ms.",
"sulu_form.single_form_selection.no_form_selected": "No form selected",
"sulu_form.single_form_selection.overlay_title": "Select a form"
"sulu_form.single_form_selection.overlay_title": "Select a form",
"sulu_form.success_landing_page": "Success landing page"
}