diff --git a/composer.json b/composer.json index b5fab941..0be9331b 100644 --- a/composer.json +++ b/composer.json @@ -70,6 +70,10 @@ }, "autoload-dev": { "psr-4": { + "Tests\\BitBag\\SyliusElasticsearchPlugin\\": [ + "tests/", + "tests/Application/src" + ], "Tests\\BitBag\\SyliusElasticsearchPlugin\\Behat\\": "tests/Behat", "spec\\BitBag\\SyliusElasticsearchPlugin\\": "spec/" }, diff --git a/config/services/form.xml b/config/services/form.xml index 145438b4..5fa82d2c 100644 --- a/config/services/form.xml +++ b/config/services/form.xml @@ -80,5 +80,13 @@ + + + + + + + + diff --git a/config/twig/bitbag_twig_hooks.yml b/config/twig/bitbag_twig_hooks.yml index 364337e7..347be2a2 100644 --- a/config/twig/bitbag_twig_hooks.yml +++ b/config/twig/bitbag_twig_hooks.yml @@ -202,3 +202,19 @@ sylius_twig_hooks: order_by: 'price' sort: 'asc' priority: 200 + 'sylius_admin.product_attribute.create.content.form.general': + facet_disabled: + template: '@BitBagSyliusElasticsearchPlugin/admin/product_attribute/form/general/facet_disabled.html.twig' + priority: -100 + 'sylius_admin.product_attribute.update.content.form.general': + facet_disabled: + template: '@BitBagSyliusElasticsearchPlugin/admin/product_attribute/form/general/facet_disabled.html.twig' + priority: -100 + 'sylius_admin.product_option.create.content.form.sections.general': + facet_disabled: + template: '@BitBagSyliusElasticsearchPlugin/admin/product_attribute/form/general/facet_disabled.html.twig' + priority: -100 + 'sylius_admin.product_option.update.content.form.sections.general': + facet_disabled: + template: '@BitBagSyliusElasticsearchPlugin/admin/product_attribute/form/general/facet_disabled.html.twig' + priority: -100 diff --git a/doc/installation.md b/doc/installation.md index a94a9ad6..33ae766f 100644 --- a/doc/installation.md +++ b/doc/installation.md @@ -89,14 +89,37 @@ fos_elastica: ``` ## Entities -Use `BitBag\SyliusElasticsearchPlugin\Model\ProductVariantTrait` and `BitBag\SyliusElasticsearchPlugin\Model\ProductVariantInterface` in an overridden ProductVariant entity class. -The configuration, depending on the mapping used, may vary. +Use + +`BitBag\SyliusElasticsearchPlugin\Model\ProductVariantTrait` +`BitBag\SyliusElasticsearchPlugin\Model\ProductVariantInterface` +`BitBag\SyliusElasticsearchPlugin\Entity\DisableFacetAwareTrait` +`BitBag\SyliusElasticsearchPlugin\Entity\ProductAttributeInterface` +`BitBag\SyliusElasticsearchPlugin\Entity\ProductOptionInterface` + +in an overridden ProductVariant,ProductOption and ProductAttribute entity classes. The configuration, depending on the mapping used, may vary. ### Attribute mapping - [Attribute mapping configuration](installation/attribute-mapping.md) ### XML mapping - [XML mapping configuration](installation/xml-mapping.md) +### Add configuration for extended product option and product attribute: +```yaml +sylius_product: + resources: + product_option: + classes: + model: App\Entity\Product\ProductOption + +sylius_attribute: + resources: + product: + attribute: + classes: + model: App\Entity\Product\ProductAttribute +``` + ### Update installed assets with the following command: ```bash bin/console assets:install @@ -107,6 +130,12 @@ bin/console assets:install bin/console cache:clear ``` +### Finish the installation by updating the database schema and installing assets +```bash +bin/console doctrine:migrations:diff +bin/console doctrine:migrations:migrate +``` + ### Finally, with an elasticsearch server running, execute following command: ```bash bin/console fos:elastica:populate diff --git a/src/Entity/DisableFacetAwareInterface.php b/src/Entity/DisableFacetAwareInterface.php new file mode 100644 index 00000000..a881f4b8 --- /dev/null +++ b/src/Entity/DisableFacetAwareInterface.php @@ -0,0 +1,19 @@ + false])] + protected bool $facetDisabled = false; + + public function isFacetDisabled(): bool + { + return $this->facetDisabled; + } + + public function setFacetDisabled(bool $facetDisabled): void + { + $this->facetDisabled = $facetDisabled; + } +} diff --git a/src/Entity/ProductAttributeInterface.php b/src/Entity/ProductAttributeInterface.php new file mode 100644 index 00000000..8de4e65a --- /dev/null +++ b/src/Entity/ProductAttributeInterface.php @@ -0,0 +1,18 @@ +productAttributeRepository->findAllWithTranslations($this->localeContext->getLocaleCode()); + $attributes = $this->productAttributeRepository->findEnabledWithTranslations($this->localeContext->getLocaleCode()); /** @var AttributeInterface $attribute */ foreach ($attributes as $attribute) { @@ -73,7 +73,7 @@ private function discoverAttributes(): void private function discoverOptions(): void { - $options = $this->productOptionRepository->findAllWithTranslations($this->localeContext->getLocaleCode()); + $options = $this->productOptionRepository->findEnabledWithTranslations($this->localeContext->getLocaleCode()); /** @var ProductOptionInterface $option */ foreach ($options as $option) { diff --git a/src/Form/Extension/ProductAttributeTypeExtension.php b/src/Form/Extension/ProductAttributeTypeExtension.php new file mode 100644 index 00000000..e5bf2fb7 --- /dev/null +++ b/src/Form/Extension/ProductAttributeTypeExtension.php @@ -0,0 +1,34 @@ +add('facetDisabled', CheckboxType::class, [ + 'label' => 'bitbag_sylius_elasticsearch_plugin.admin.form.facet_disabled', + ]); + } + + public static function getExtendedTypes(): iterable + { + return [ProductAttributeType::class]; + } +} diff --git a/src/Form/Extension/ProductOptionTypeExtension.php b/src/Form/Extension/ProductOptionTypeExtension.php new file mode 100644 index 00000000..daea4835 --- /dev/null +++ b/src/Form/Extension/ProductOptionTypeExtension.php @@ -0,0 +1,34 @@ +add('facetDisabled', CheckboxType::class, [ + 'label' => 'bitbag_sylius_elasticsearch_plugin.admin.form.facet_disabled', + ]); + } + + public static function getExtendedTypes(): iterable + { + return [ProductOptionType::class]; + } +} diff --git a/src/Form/Type/SearchFacetsType.php b/src/Form/Type/SearchFacetsType.php index 69837f1d..fc36090a 100644 --- a/src/Form/Type/SearchFacetsType.php +++ b/src/Form/Type/SearchFacetsType.php @@ -30,7 +30,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void foreach ($options['facets'] as $facetId => $facetData) { $facet = $this->facetRegistry->getFacetById((string) $facetId); $choices = []; - foreach ($facetData['buckets'] as $bucket) { + foreach ($facetData['buckets'] ?? [] as $bucket) { $choices[$facet->getBucketLabel($bucket)] = $bucket['key']; } if ([] !== $choices) { diff --git a/src/Repository/ProductAttributeRepository.php b/src/Repository/ProductAttributeRepository.php index 10f4f56c..d3a7823d 100644 --- a/src/Repository/ProductAttributeRepository.php +++ b/src/Repository/ProductAttributeRepository.php @@ -38,12 +38,13 @@ public function getAttributeTypeByName(string $attributeName): string return $result['type']; } - public function findAllWithTranslations(?string $locale): array + public function findEnabledWithTranslations(?string $locale): array { /** @var EntityRepository $productAttributeRepository */ $productAttributeRepository = $this->productAttributeRepository; $queryBuilder = $productAttributeRepository->createQueryBuilder('o'); + $queryBuilder->andWhere('o.facetDisabled = 0'); if (null !== $locale) { $queryBuilder diff --git a/src/Repository/ProductAttributeRepositoryInterface.php b/src/Repository/ProductAttributeRepositoryInterface.php index 46a20578..6400bc47 100644 --- a/src/Repository/ProductAttributeRepositoryInterface.php +++ b/src/Repository/ProductAttributeRepositoryInterface.php @@ -16,5 +16,5 @@ interface ProductAttributeRepositoryInterface { public function getAttributeTypeByName(string $attributeName): string; - public function findAllWithTranslations(?string $locale): array; + public function findEnabledWithTranslations(?string $locale): array; } diff --git a/src/Repository/ProductOptionRepository.php b/src/Repository/ProductOptionRepository.php index 07af6300..7c3bcd05 100644 --- a/src/Repository/ProductOptionRepository.php +++ b/src/Repository/ProductOptionRepository.php @@ -22,14 +22,16 @@ public function __construct( ) { } - public function findAllWithTranslations(?string $locale): array + public function findEnabledWithTranslations(?string $locale): array { - /** @var EntityRepository $queryBuilder */ - $queryBuilder = $this->productOptionRepository; + /** @var EntityRepository $productAttributeRepository */ + $productAttributeRepository = $this->productOptionRepository; + + $queryBuilder = $productAttributeRepository->createQueryBuilder('o'); + $queryBuilder->andWhere('o.facetDisabled = 0'); if (null !== $locale) { $queryBuilder - ->createQueryBuilder('o') ->addSelect('translation') /** @phpstan-ignore-next-line phpstan can't read relationship correctly */ ->leftJoin('o.translations', 'translation', 'ot') @@ -39,7 +41,6 @@ public function findAllWithTranslations(?string $locale): array } return $queryBuilder - ->createQueryBuilder('o') ->getQuery() ->getResult() ; diff --git a/src/Repository/ProductOptionRepositoryInterface.php b/src/Repository/ProductOptionRepositoryInterface.php index e5daf281..3158978e 100644 --- a/src/Repository/ProductOptionRepositoryInterface.php +++ b/src/Repository/ProductOptionRepositoryInterface.php @@ -14,5 +14,5 @@ interface ProductOptionRepositoryInterface { - public function findAllWithTranslations(?string $locale): array; + public function findEnabledWithTranslations(?string $locale): array; } diff --git a/templates/admin/product_attribute/form/general/facet_disabled.html.twig b/templates/admin/product_attribute/form/general/facet_disabled.html.twig new file mode 100644 index 00000000..3fac112a --- /dev/null +++ b/templates/admin/product_attribute/form/general/facet_disabled.html.twig @@ -0,0 +1,3 @@ +
+ {{ form_row(hookable_metadata.context.form.facetDisabled, sylius_test_form_attribute('facetDisabled')) }} +
diff --git a/tests/Application/config/packages/doctrine.yaml b/tests/Application/config/packages/doctrine.yaml index f51ba5a2..65f75574 100755 --- a/tests/Application/config/packages/doctrine.yaml +++ b/tests/Application/config/packages/doctrine.yaml @@ -12,3 +12,14 @@ doctrine: charset: UTF8 url: '%env(resolve:DATABASE_URL)%' + + orm: + entity_managers: + default: + auto_mapping: true + mappings: + TestApp: + is_bundle: false + type: attribute + dir: '%kernel.project_dir%/src/Entity' + prefix: Tests\BitBag\SyliusElasticsearchPlugin diff --git a/tests/Application/config/packages/sylius_product.yaml b/tests/Application/config/packages/sylius_product.yaml new file mode 100644 index 00000000..f521ac94 --- /dev/null +++ b/tests/Application/config/packages/sylius_product.yaml @@ -0,0 +1,12 @@ +sylius_product: + resources: + product_option: + classes: + model: Tests\BitBag\SyliusElasticsearchPlugin\Entity\ProductOption + +sylius_attribute: + resources: + product: + attribute: + classes: + model: Tests\BitBag\SyliusElasticsearchPlugin\Entity\ProductAttribute \ No newline at end of file diff --git a/tests/Application/src/Entity/ProductAttribute.php b/tests/Application/src/Entity/ProductAttribute.php new file mode 100644 index 00000000..8d586f72 --- /dev/null +++ b/tests/Application/src/Entity/ProductAttribute.php @@ -0,0 +1,24 @@ +