[filterOrder] fix error in method getActiveFilters when dealing with entityChoice with incorrect number of translation

This commit is contained in:
Julien Fastré 2023-07-10 15:39:00 +02:00
parent 39896ea6e2
commit 43419f9f15
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 17 additions and 5 deletions

View File

@ -84,9 +84,11 @@ final class FilterOrderHelper
public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = [], array $options = []): self public function addCheckbox(string $name, array $choices, ?array $default = [], ?array $trans = [], array $options = []): self
{ {
$missing = count($choices) - count($trans) - 1; $missing = count($choices) - count($trans);
$this->checkboxes[$name] = [ $this->checkboxes[$name] = [
'choices' => $choices, 'default' => $default, 'choices' => $choices,
'default' => $default,
'trans' => array_merge( 'trans' => array_merge(
$trans, $trans,
0 < $missing ? 0 < $missing ?
@ -223,9 +225,10 @@ final class FilterOrderHelper
} }
} }
foreach ($this->checkboxes as $name => ['choices' => $choices, 'trans' => $trans, 'options' => $options]) { foreach ($this->checkboxes as $name => ['choices' => $choices, 'trans' => $trans]) {
$translatedChoice = array_combine($choices, [...$trans]);
foreach ($this->getCheckboxData($name) as $keyChoice) { foreach ($this->getCheckboxData($name) as $keyChoice) {
$result[] = ['value' => $choices['keyChoice'], 'label' => $options['label'], 'position' => FilterOrderPositionEnum::Checkboxes->value, 'name' => $name]; $result[] = ['value' => $translatedChoice[$keyChoice], 'label' => '', 'position' => FilterOrderPositionEnum::Checkboxes->value, 'name' => $name];
} }
} }
@ -237,7 +240,7 @@ final class FilterOrderHelper
$value = $this->propertyAccessor->getValue($selected, $options['choice_label']); $value = $this->propertyAccessor->getValue($selected, $options['choice_label']);
} else { } else {
if (!$selected instanceof \Stringable) { if (!$selected instanceof \Stringable) {
throw new \UnexpectedValueException(sprintf("we are not able to transform the value of %s to a string. Implements \Stringable or add a 'choice_label' option to the filterFormBuilder", get_class($selected))); throw new \UnexpectedValueException(sprintf("we are not able to transform the value of %s to a string. Implements \\Stringable or add a 'choice_label' option to the filterFormBuilder", get_class($selected)));
} }
$value = (string) $selected; $value = (string) $selected;

View File

@ -1,5 +1,14 @@
<?php <?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Templating\Listing; namespace Chill\MainBundle\Templating\Listing;
enum FilterOrderPositionEnum: string enum FilterOrderPositionEnum: string