Skip to content

Instantly share code, notes, and snippets.

@axelvnk
Last active September 11, 2020 11:04
Show Gist options
  • Save axelvnk/de60856e1739a3c95bc9d304866d4496 to your computer and use it in GitHub Desktop.
Save axelvnk/de60856e1739a3c95bc9d304866d4496 to your computer and use it in GitHub Desktop.
Sulu form fixture and use in document fixture
<?php
namespace App\DataFixtures\Document;
use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\FormBundle\Entity\Form;
use Sulu\Component\DocumentManager\DocumentManager;
use Sulu\Bundle\DocumentManagerBundle\DataFixtures\DocumentFixtureInterface;
class HomepageFixture implements DocumentFixtureInterface
{
const LOCALE = 'en';
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function getOrder(): int
{
return 10;
}
public function load(DocumentManager $documentManager)
{
$document = $documentManager->find('/cmf/your_webspace_key/contents', 'en');
$document->setLocale(static::LOCALE);
$document->setTitle('My Awesome Homepage');
$document->setStructureType('homepage');
$document->getStructure()->bind(
[
'section_title' => 'Oh my god look at this awesome form!',
'my_awesome_form' => $this->getFormWithTitle('Contact form')->getId(),
]
);
$document->setExtension('seo', [
'title' => 'MyAwesomeWebsite | We do the awesomes',
]);
$documentManager->persist($document, static::LOCALE, [
'parent_path' => '/cmf/your_webspace_key/contents',
]);
$documentManager->publish($document, static::LOCALE);
$documentManager->flush();
}
private function getFormWithTitle(?string $title): Form
{
$qb = $this->entityManager->createQueryBuilder();
$qb
->from(Form::class, 'form')
->innerJoin('form.translations', 'translation')
->select('form')
->where($qb->expr()->eq('translation.title', ':title'))
->setParameter('title', $title)
;
return $qb->getQuery()->getSingleResult();
}
}
<?php
namespace App\DataFixtures;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Sulu\Bundle\FormBundle\Entity\Form;
use Sulu\Bundle\FormBundle\Entity\FormField;
use Sulu\Bundle\FormBundle\Entity\FormFieldTranslation;
use Sulu\Bundle\FormBundle\Entity\FormTranslation;
class AppointmentFormFixture extends Fixture
{
public function load(ObjectManager $manager)
{
$form = new Form();
$form->setDefaultLocale('en');
$formTranslation = new FormTranslation();
$formTranslation->setForm($form);
$formTranslation->setLocale('en');
$formTranslation->setTitle('Contact form');
$formTranslation->setSubject('New form submission on the website you manage');
$formTranslation->setFromEmail('appointments@yourdomain.be');
$formTranslation->setFromEmail('info@yourdomain.be');
$formTranslation->setSubmitLabel('Send');
$formTranslation->setSuccessText('Awesome! Form has been submitted!');
$form->addTranslation($formTranslation);
$formFields = [
[
'key' => 'full_name',
'type' => 'text',
'width' => 'full',
'required' => true,
'default_locale' => 'en',
'title' => 'Full name',
],
[
'key' => 'email',
'type' => 'email',
'width' => 'full',
'required' => true,
'default_locale' => 'en',
'title' => 'E-mail address',
],
[
'key' => 'phone',
'type' => 'text',
'width' => 'full',
'required' => false,
'default_locale' => 'en',
'title' => 'Phone number',
],
];
$i = 0;
foreach ($formFields as $formField) {
$fieldObject = $this->createFormField($formField, $i);
$fieldObject->setForm($form);
$form->addField($fieldObject);
$i++;
}
$manager->persist($form);
$manager->flush();
}
private function createFormField(array $fieldData, int $order): FormField
{
$formField = new FormField();
$formFieldTranslation = new FormFieldTranslation();
$formFieldTranslation->setField($formField);
$formField->addTranslation($formFieldTranslation);
$formField->setKey($fieldData['key']);
$formField->setType($fieldData['type']);
$formField->setWidth($fieldData['width']);
$formField->setRequired($fieldData['required']);
$formField->setDefaultLocale($fieldData['default_locale']);
$formField->setOrder($order);
$formFieldTranslation->setLocale('en');
$formFieldTranslation->setTitle($fieldData['title']);
$formFieldTranslation->setPlaceholder($fieldData['placeholder'] ?? null);
return $formField;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment