diff --git a/src/Sylius/Bundle/TaxonomyBundle/Form/Type/TaxonAutocompleteChoiceType.php b/src/Sylius/Bundle/TaxonomyBundle/Form/Type/TaxonAutocompleteChoiceType.php index 4fa53643394..25c51868408 100644 --- a/src/Sylius/Bundle/TaxonomyBundle/Form/Type/TaxonAutocompleteChoiceType.php +++ b/src/Sylius/Bundle/TaxonomyBundle/Form/Type/TaxonAutocompleteChoiceType.php @@ -28,7 +28,7 @@ public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'resource' => 'sylius.taxon', - 'choice_name' => 'name', + 'choice_name' => 'fullname', 'choice_value' => 'code', ]); } diff --git a/src/Sylius/Bundle/TaxonomyBundle/Resources/config/serializer/Model.Taxon.yml b/src/Sylius/Bundle/TaxonomyBundle/Resources/config/serializer/Model.Taxon.yml index 3c5b801c17b..e876698f48a 100644 --- a/src/Sylius/Bundle/TaxonomyBundle/Resources/config/serializer/Model.Taxon.yml +++ b/src/Sylius/Bundle/TaxonomyBundle/Resources/config/serializer/Model.Taxon.yml @@ -50,3 +50,6 @@ Sylius\Component\Taxonomy\Model\Taxon: hasChildren: serialized_name: hasChildren groups: [Autocomplete] + getFullname: + serialized_name: fullname + groups: [Autocomplete] diff --git a/src/Sylius/Component/Taxonomy/Model/Taxon.php b/src/Sylius/Component/Taxonomy/Model/Taxon.php index fec0f8bcd9a..33bc1fe7165 100644 --- a/src/Sylius/Component/Taxonomy/Model/Taxon.php +++ b/src/Sylius/Component/Taxonomy/Model/Taxon.php @@ -203,6 +203,23 @@ public function setName(?string $name): void $this->getTranslation()->setName($name); } + /** + * {@inheritdoc} + */ + public function getFullname(string $pathDelimiter = ' / '): ?string + { + if ($this->isRoot()) { + return $this->getName(); + } + + return sprintf( + '%s%s%s', + $this->getParent()->getFullname(), + $pathDelimiter, + $this->getName() + ); + } + /** * {@inheritdoc} */ diff --git a/src/Sylius/Component/Taxonomy/Model/TaxonInterface.php b/src/Sylius/Component/Taxonomy/Model/TaxonInterface.php index 68085325ada..6bd84a4a306 100644 --- a/src/Sylius/Component/Taxonomy/Model/TaxonInterface.php +++ b/src/Sylius/Component/Taxonomy/Model/TaxonInterface.php @@ -70,6 +70,8 @@ public function getName(): ?string; public function setName(?string $name): void; + public function getFullname(string $pathDelimiter = ' / '): ?string; + public function getDescription(): ?string; public function setDescription(?string $description): void; diff --git a/src/Sylius/Component/Taxonomy/spec/Model/TaxonSpec.php b/src/Sylius/Component/Taxonomy/spec/Model/TaxonSpec.php index 09bc0904dc1..8f8746b07c9 100644 --- a/src/Sylius/Component/Taxonomy/spec/Model/TaxonSpec.php +++ b/src/Sylius/Component/Taxonomy/spec/Model/TaxonSpec.php @@ -117,6 +117,29 @@ function it_returns_name_when_converted_to_string(): void $this->__toString()->shouldReturn('T-Shirt material'); } + function its_fullname_is_null_if_unnamed(): void + { + $this->getFullname()->shouldReturn(null); + } + + function its_fullname_equal_name_if_no_parent(): void + { + $this->setName('Category'); + $this->getFullname()->shouldReturn('Category'); + } + + function its_fullname_prepends_with_parents_fullname(TaxonInterface $root): void + { + $root->getFullname()->willReturn('Category'); + $this->setName('T-shirts'); + + $root->addChild($this)->shouldBeCalled(); + $this->setParent($root); + + $this->getFullname()->shouldReturn('Category / T-shirts'); + $this->getFullname(' -- ')->shouldReturn('Category -- T-shirts'); + } + function it_has_no_description_by_default(): void { $this->getDescription()->shouldReturn(null);