Skip to content

Commit

Permalink
Fix root option on TaxonChoiceType
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Ducoudray committed Jan 30, 2017
1 parent f563c74 commit 8e9a2bb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
13 changes: 11 additions & 2 deletions src/Sylius/Bundle/TaxonomyBundle/Doctrine/ORM/TaxonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Sylius\Bundle\TaxonomyBundle\Doctrine\ORM;

use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Component\Taxonomy\Model\TaxonInterface;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;

/**
Expand Down Expand Up @@ -87,7 +86,7 @@ public function findRootNodes()
/**
* {@inheritdoc}
*/
public function findNodesTreeSorted()
public function findNodesTreeSorted($rootCode = null)
{
return $this->createQueryBuilder('o')
->addOrderBy('o.root')
Expand All @@ -96,6 +95,16 @@ public function findNodesTreeSorted()
->getQuery()
->getResult()
;

if (null !== $rootCode) {
$queryBuilder
->join('o.root', 'root')
->andWhere('root.code = :rootCode')
->setParameter('rootCode', $rootCode)
;
}

return $queryBuilder->getQuery()->getResult();
}

/**
Expand Down
30 changes: 20 additions & 10 deletions src/Sylius/Bundle/TaxonomyBundle/Form/Type/TaxonChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
Expand Down Expand Up @@ -81,21 +80,15 @@ public function configureOptions(OptionsResolver $resolver)
$resolver
->setDefaults([
'choices' => function (Options $options) {
$taxons = $this->taxonRepository->findNodesTreeSorted();

if (null !== $options['filter']) {
$taxons = array_filter($taxons, $options['filter']);
}

return $taxons;
return $this->getTaxons($options['root_code'], $options['filter']);
},
'choice_value' => 'id',
'choice_label' => 'name',
'choice_translation_domain' => false,
'root' => null,
'root_code' => null,
'filter' => null,
])
->setAllowedTypes('root', [TaxonInterface::class, 'string', 'null'])
->setAllowedTypes('root_code', ['string', 'null'])
->setAllowedTypes('filter', ['callable', 'null'])
;
}
Expand All @@ -107,4 +100,21 @@ public function getBlockPrefix()
{
return 'sylius_taxon_choice';
}

/**
* @param string|null $rootCode
* @param callable|null $filter
*
* @return TaxonInterface[]
*/
private function getTaxons($rootCode = null, $filter = null)
{
$taxons = $this->taxonRepository->findNodesTreeSorted($rootCode);

if (null !== $filter) {
$taxons = array_filter($taxons, $filter);
}

return $taxons;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public function findChildren($parentCode);
public function findRootNodes();

/**
* @param string|null $rootCode
*
* @return TaxonInterface[]
*/
public function findNodesTreeSorted();
public function findNodesTreeSorted($rootCode = null);

/**
* @param string $slug
*
Expand Down

0 comments on commit 8e9a2bb

Please sign in to comment.