Skip to content

Commit

Permalink
fix: action confirmation callable throws exception #139
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Oct 23, 2024
1 parent 1d9e15e commit 3607227
Showing 1 changed file with 31 additions and 36 deletions.
67 changes: 31 additions & 36 deletions src/Action/Type/ActionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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'];
})
;
}

Expand All @@ -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);
}
}

0 comments on commit 3607227

Please sign in to comment.