diff --git a/src/Action/Type/ActionType.php b/src/Action/Type/ActionType.php index bba410e9..55f307b5 100755 --- a/src/Action/Type/ActionType.php +++ b/src/Action/Type/ActionType.php @@ -10,7 +10,6 @@ use Kreyu\Bundle\DataTableBundle\Action\ActionView; use Kreyu\Bundle\DataTableBundle\Column\ColumnValueView; use Kreyu\Bundle\DataTableBundle\Util\StringUtil; -use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Contracts\Translation\TranslatableInterface; @@ -46,6 +45,10 @@ public function buildView(ActionView $view, ActionInterface $action, array $opti } } + if (false !== $options['confirmation']) { + $options['confirmation'] = $this->resolveConfirmationOption($options['confirmation']); + } + if ($action->getConfig()->isConfirmable()) { $options['confirmation']['identifier'] = vsprintf( format: '%s-action-confirmation-%s', @@ -103,41 +106,6 @@ public function configureOptions(OptionsResolver $resolver): void ->setAllowedTypes('icon_attr', ['array', 'callable']) ->setAllowedTypes('confirmation', ['bool', 'array', 'callable']) ->setAllowedTypes('visible', ['bool', 'callable']) - ->setNormalizer('confirmation', function (Options $options, mixed $value) { - if (false === $value) { - return false; - } - - if (true === $value) { - $value = []; - } - - ($resolver = new OptionsResolver()) - ->setDefault('confirmation', function (OptionsResolver $resolver) { - $resolver - ->setDefaults([ - 'translation_domain' => 'KreyuDataTable', - 'label_title' => 'Action confirmation', - 'label_description' => 'Are you sure you want to execute this action?', - 'label_confirm' => 'Confirm', - 'label_cancel' => 'Cancel', - 'type' => 'danger', - ]) - ->setAllowedTypes('translation_domain', ['null', 'string']) - ->setAllowedTypes('label_title', ['null', 'string', TranslatableInterface::class]) - ->setAllowedTypes('label_description', ['null', 'string', TranslatableInterface::class]) - ->setAllowedTypes('label_confirm', ['null', 'string', TranslatableInterface::class]) - ->setAllowedTypes('label_cancel', ['null', 'string', TranslatableInterface::class]) - ->setAllowedTypes('type', ['null', 'string']) - ->setAllowedValues('type', ['danger', 'warning', 'info']) - ; - }) - ; - - $value = $resolver->resolve(['confirmation' => $value]); - - return $value['confirmation']; - }) ; } @@ -150,4 +118,31 @@ public function getParent(): ?string { return null; } + + private function resolveConfirmationOption(true|array $confirmation): array + { + if (true === $confirmation) { + $confirmation = []; + } + + $optionsResolver = (new OptionsResolver()) + ->setDefaults([ + 'translation_domain' => 'KreyuDataTable', + 'label_title' => 'Action confirmation', + 'label_description' => 'Are you sure you want to execute this action?', + 'label_confirm' => 'Confirm', + 'label_cancel' => 'Cancel', + 'type' => 'danger', + ]) + ->setAllowedTypes('translation_domain', ['null', 'string']) + ->setAllowedTypes('label_title', ['null', 'string', TranslatableInterface::class]) + ->setAllowedTypes('label_description', ['null', 'string', TranslatableInterface::class]) + ->setAllowedTypes('label_confirm', ['null', 'string', TranslatableInterface::class]) + ->setAllowedTypes('label_cancel', ['null', 'string', TranslatableInterface::class]) + ->setAllowedTypes('type', ['null', 'string']) + ->setAllowedValues('type', ['danger', 'warning', 'info']) + ; + + return $optionsResolver->resolve($confirmation); + } }