From 0bdd312b7d635d74726372e9288e443224c2605c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20CROMBEZ?= <777666+jcrombez@users.noreply.github.com> Date: Thu, 23 Nov 2023 16:18:13 +0000 Subject: [PATCH] add "attr" parameter for the actions template --- src/Grid/Grid.php | 30 +++------------------- src/Resources/config/services.yaml | 7 ++++- src/Twig/HtmlExtension.php | 41 ++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 src/Twig/HtmlExtension.php diff --git a/src/Grid/Grid.php b/src/Grid/Grid.php index 1a8bef3..eeee275 100644 --- a/src/Grid/Grid.php +++ b/src/Grid/Grid.php @@ -2,6 +2,7 @@ namespace Kibatic\DatagridBundle\Grid; +use Kibatic\DatagridBundle\Twig\HtmlExtension; use Knp\Component\Pager\Pagination\PaginationInterface; class Grid @@ -21,7 +22,7 @@ public function __construct( PaginationInterface $pagination, string $theme, array $batchActions = [], - ?callable $rowAttributesCallback = null + ?callable $rowAttributesCallback = null, ) { $this->columns = $columns; $this->pagination = $pagination; @@ -72,31 +73,6 @@ public function getRowAttributes($item, bool $keepAsArray = false): null|array|s return $attributes; } - return self::attributesToHtml($attributes); - } - - private static function attributesToHtml(array $attributes): string - { - return array_reduce( - array_keys($attributes), - function (string $carry, string $key) use ($attributes) { - $value = $attributes[$key]; - - if (!\is_scalar($value) && null !== $value) { - throw new \LogicException(sprintf('A "%s" prop was passed when creating the component. No matching "%s" property or mount() argument was found, so we attempted to use this as an HTML attribute. But, the value is not a scalar (it\'s a %s). Did you mean to pass this to your component or is there a typo on its name?', $key, $key, get_debug_type($value))); - } - - if (null === $value) { - throw new \Exception('Passing "null" as an attribute value is forbidden'); - } - - return match ($value) { - true => "{$carry} {$key}", - false => $carry, - default => sprintf('%s %s="%s"', $carry, $key, $value), - }; - }, - '' - ); + return HtmlExtension::attributesToHtml($attributes); } } diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index a295ecd..8b032d9 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -4,4 +4,9 @@ services: autoconfigure: true Kibatic\DatagridBundle\Grid\GridBuilder: - public: true \ No newline at end of file + public: true + + Kibatic\DatagridBundle\Twig\HtmlExtension: + public: true + tags: + - { name: twig.extension } diff --git a/src/Twig/HtmlExtension.php b/src/Twig/HtmlExtension.php new file mode 100644 index 0000000..8f4b78c --- /dev/null +++ b/src/Twig/HtmlExtension.php @@ -0,0 +1,41 @@ +attributesToHtml(...), ['is_safe' => ['html']]), + ]; + } + + public static function attributesToHtml(array $attributes): string + { + return array_reduce( + array_keys($attributes), + function (string $carry, string $key) use ($attributes) { + $value = $attributes[$key]; + + if (!\is_scalar($value) && null !== $value) { + throw new \LogicException(sprintf('A "%s" prop was passed when creating the component. No matching "%s" property or mount() argument was found, so we attempted to use this as an HTML attribute. But, the value is not a scalar (it\'s a %s). Did you mean to pass this to your component or is there a typo on its name?', $key, $key, get_debug_type($value))); + } + + if (null === $value) { + throw new \Exception('Passing "null" as an attribute value is forbidden'); + } + + return match ($value) { + true => "{$carry} {$key}", + false => $carry, + default => sprintf('%s %s="%s"', $carry, $key, $value), + }; + }, + '' + ); + } +}