54 lines
1.7 KiB
PHP
54 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Form\Extension;
|
|
|
|
use Symfony\Component\Form\AbstractTypeExtension;
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
|
use Symfony\Component\Form\FormInterface;
|
|
use Symfony\Component\Form\FormView;
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
|
|
class ModalSubmitExtension extends AbstractTypeExtension
|
|
{
|
|
public function configureOptions(OptionsResolver $resolver): void
|
|
{
|
|
$resolver->setDefaults([
|
|
'hx_post' => null,
|
|
'hx_get' => null,
|
|
'hx_target' => '#htmx-modal',
|
|
'hx_swap' => 'outerHTML',
|
|
'hx_push_url' => false,
|
|
]);
|
|
}
|
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options): void
|
|
{
|
|
// hx_post keeps the form inside the modal (so validation errors re-render in place);
|
|
// hx_get is for read-only forms such as list filters, which navigate the list instead
|
|
// and therefore usually target the body and push the resulting URL.
|
|
$method = null !== $options['hx_post'] ? 'hx-post' : (null !== $options['hx_get'] ? 'hx-get' : null);
|
|
|
|
if (null === $method) {
|
|
return;
|
|
}
|
|
|
|
$attr = [
|
|
$method => $options['hx-post' === $method ? 'hx_post' : 'hx_get'],
|
|
'hx-target' => $options['hx_target'],
|
|
'hx-swap' => $options['hx_swap'],
|
|
'hx-indicator' => '#htmx-modal-indicator',
|
|
];
|
|
|
|
if (false !== $options['hx_push_url']) {
|
|
$attr['hx-push-url'] = true === $options['hx_push_url'] ? 'true' : $options['hx_push_url'];
|
|
}
|
|
|
|
$view->vars['attr'] = array_merge($view->vars['attr'], $attr);
|
|
}
|
|
|
|
public static function getExtendedTypes(): iterable
|
|
{
|
|
return [FormType::class];
|
|
}
|
|
}
|