Skip to content

Commit

Permalink
Add counts
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jul 8, 2024
1 parent b8f7739 commit b591aae
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/Form/Builder/CheckboxFacetFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public function build(FormBuilderInterface $builder, string $name, array $values
{
$builder->add($name, CheckboxType::class, [
'label' => sprintf('setono_sylius_meilisearch.form.search.facet.%s', u($name)->snake()),
'label_translation_parameters' => [
'%count%' => $values['true'],
],
'required' => false,
]);
}
Expand All @@ -23,7 +26,7 @@ public function supports(string $name, array $values, array $stats = null): bool
$c = count($values);

return match ($c) {
1 => isset($values['true']) || isset($values['false']),
1 => isset($values['true']),
2 => isset($values['true'], $values['false']),
default => false,
};
Expand Down
12 changes: 8 additions & 4 deletions src/Form/Builder/ChoiceFacetFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Setono\SyliusMeilisearchPlugin\Form\Builder;

use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use function Symfony\Component\String\u;
Expand All @@ -19,6 +18,7 @@ public function build(FormBuilderInterface $builder, string $name, array $values
$builder->add($name, ChoiceType::class, [
'label' => sprintf('setono_sylius_meilisearch.form.search.facet.%s', u($name)->snake()),
'choices' => $choices,
'choice_label' => fn (string $key) => sprintf('%s (%d)', $key, $values[$key]),
'expanded' => true,
'multiple' => true,
'required' => false,
Expand All @@ -27,13 +27,17 @@ public function build(FormBuilderInterface $builder, string $name, array $values

public function supports(string $name, array $values, array $stats = null): bool
{
foreach (array_keys($values) as $value) {
if (is_numeric($value)) {
$keys = array_keys($values);
if (count($keys) < 2) {
return false;
}

foreach ($keys as $key) {
if (is_numeric($key)) {
return false;
}
}

return true;

}
}
4 changes: 3 additions & 1 deletion src/Form/Builder/SearchFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Setono\SyliusMeilisearchPlugin\Form\Builder;

use Meilisearch\Search\SearchResult;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;

Expand All @@ -18,7 +19,8 @@ public function build(SearchResult $searchResult): FormInterface
{
$builder = $this->formFactory->createNamedBuilder('', options: [
'csrf_protection' => false,
]);
'allow_extra_fields' => true,
])->add('q', HiddenType::class);

/**
* Here is an example of the facet stats array
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ setono_sylius_meilisearch:
form:
search:
facet:
on_sale: On sale
on_sale: On sale (%count%)
size: Size
q_placeholder: Search...
update_filter: Update filter
Expand Down

0 comments on commit b591aae

Please sign in to comment.