Skip to content

Commit

Permalink
Fixed form on a shadow page (#175)
Browse files Browse the repository at this point in the history
* load form from shadow when not exist

* use mail configuration locale to send emails

* move to own function
  • Loading branch information
alexander-schranz authored and wachterjohannes committed Sep 20, 2018
1 parent 7f812b6 commit 39bc07f
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## unreleased

- BUGFIX #175 Fixed form on a shadow pages
- BUGFIX #173 Fixed missing `sulu_form.navigation_provider.template` service
- FEATURE #172 Added formdata parameter to getNotifySubject function in FormConfigurationFactory
- FEATURE #167 Added possibility to change mailchimp subscribe status
Expand Down
36 changes: 33 additions & 3 deletions Content/Types/FormSelect.php
Expand Up @@ -14,6 +14,8 @@
use Sulu\Bundle\FormBundle\Form\BuilderInterface;
use Sulu\Bundle\FormBundle\Repository\FormRepository;
use Sulu\Component\Content\Compat\PropertyInterface;
use Sulu\Component\Content\Compat\Structure\PageBridge;
use Sulu\Component\Content\Compat\Structure\StructureBridge;
use Sulu\Component\Content\SimpleContentType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Exception\MissingOptionsException;
Expand Down Expand Up @@ -76,22 +78,50 @@ public function getContentData(PropertyInterface $property)

$type = $property->getParams()['type']->getValue();

/** @var PageBridge $structure */
$structure = $property->getStructure();

/** @var FormInterface $form */
$form = $this->formBuilder->build(
$id,
$type,
$property->getStructure()->getUuid(),
$property->getStructure()->getLanguageCode(),
$structure->getUuid(),
$structure->getLanguageCode(),
$property->getName()
);

if (!$form) {
return;
$form = $this->loadShadowForm($property, $id, $type);

if (!$form) {
return;
}
}

return $form->createView();
}

private function loadShadowForm(PropertyInterface $property, $id, $type)
{
$structure = $property->getStructure();

if (!$structure instanceof StructureBridge) {
return;
}

if (!$structure->getIsShadow()) {
return;
}

return $this->formBuilder->build(
$id,
$type,
$structure->getUuid(),
$structure->getShadowBaseLanguage(),
$property->getName()
);
}

/**
* {@inheritdoc}
*/
Expand Down
14 changes: 14 additions & 0 deletions Entity/Dynamic.php
Expand Up @@ -408,6 +408,20 @@ public function getLocale()
return $this->locale;
}

/**
* Set locale.
*
* @param string $locale
*
* @return $this
*/
public function setLocale($locale)
{
$this->locale = $locale;

return $this;
}

/**
* Get type.
*
Expand Down
1 change: 1 addition & 0 deletions Event/RequestListener.php
Expand Up @@ -96,6 +96,7 @@ public function onKernelRequest(GetResponseEvent $event)
/** @var Dynamic $dynamic */
$dynamic = $form->getData();
$configuration = $this->formConfigurationFactory->buildByDynamic($dynamic);
$dynamic->setLocale($request->getLocale()); // Need to be set to request locale for shadow pages, configuraiton will hold the original locale

if ($this->formHandler->handle($form, $configuration)) {
$serializedObject = $dynamic->getForm()->serializeForLocale($dynamic->getLocale(), $dynamic);
Expand Down
7 changes: 5 additions & 2 deletions Form/Builder.php
Expand Up @@ -114,8 +114,6 @@ public function buildByRequest(Request $request)
throw new HttpException(400, 'SuluFormBundle: Checksum not valid!');
}

$locale = $request->getLocale();

if (!isset($parameters['type'])
|| !isset($parameters['formId'])
|| !isset($parameters['formName'])
Expand All @@ -124,6 +122,11 @@ public function buildByRequest(Request $request)
continue;
}

$locale = $request->getLocale();
if (isset($parameters['locale'])) {
$locale = $parameters['locale'];
}

return $this->build(
$parameters['formId'],
$parameters['type'],
Expand Down
2 changes: 1 addition & 1 deletion Form/Handler.php
Expand Up @@ -163,7 +163,7 @@ private function sendMail(FormInterface $form, MailConfigurationInterface $confi
$formData = $form->getData();
if ($formData instanceof Dynamic) {
$additionalData = [
'formEntity' => $formData->getForm()->serializeForLocale($formData->getLocale(), $formData),
'formEntity' => $formData->getForm()->serializeForLocale($configuration->getLocale(), $formData),
];
}

Expand Down
8 changes: 7 additions & 1 deletion Form/Type/DynamicFormType.php
Expand Up @@ -119,13 +119,19 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$this->typePool->get($field->getType())->build($builder, $field, $locale, $options);
}

// Add hidden locale. (de, en, ...)
$builder->add('locale', HiddenType::class, [
'data' => $locale,
'mapped' => false,
]);

// Add hidden type field. (page, article, event, blog, ...)
$builder->add('type', HiddenType::class, [
'data' => $type,
'mapped' => false,
]);

// Add hidden typeId field. (UUID, Database id,)
// Add hidden typeId field. (UUID, Database id, ...)
$builder->add('typeId', HiddenType::class, [
'data' => $typeId,
'mapped' => false,
Expand Down

0 comments on commit 39bc07f

Please sign in to comment.