Skip to content

Commit

Permalink
[Fixtures] added possibility to create non-translatable attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
SirDomin committed Jan 15, 2021
1 parent e5c2011 commit adfda63
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function create(array $options = []): ProductAttributeInterface
/** @var ProductAttributeInterface $productAttribute */
$productAttribute = $this->productAttributeFactory->createTyped($options['type']);
$productAttribute->setCode($options['code']);
$productAttribute->setTranslatable($options['translatable']);

foreach ($this->getLocales() as $localeCode) {
$productAttribute->setCurrentLocale($localeCode);
Expand All @@ -82,6 +83,9 @@ protected function configureOptions(OptionsResolver $resolver): void

return $words;
})
->setDefault('translatable', function(Options $options): bool {
return true;
})
->setDefault('code', function (Options $options): string {
return StringInflector::nameToCode($options['name']);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,21 +360,26 @@ private function setAttributeValues(array $productAttributes): array
{
$productAttributesValues = [];
foreach ($productAttributes as $code => $value) {
foreach ($this->getLocales() as $localeCode) {
$productAttributesValues[] = $this->configureProductAttributeValue($code, $localeCode, $value);
/** @var ProductAttributeInterface|null $productAttribute */
$productAttribute = $this->productAttributeRepository->findOneBy(['code' => $code]);

Assert::notNull($productAttribute, sprintf('Can not find product attribute with code: "%s"', $code));

if ($productAttribute->isTranslatable()) {
foreach ($this->getLocales() as $localeCode) {
$productAttributesValues[] = $this->configureProductAttributeValue($productAttribute, $localeCode, $value);
}
} else {
$productAttributesValues[] = $this->configureProductAttributeValue($productAttribute, null, $value);
}

}

return $productAttributesValues;
}

private function configureProductAttributeValue(string $code, string $localeCode, $value): ProductAttributeValueInterface
private function configureProductAttributeValue(ProductAttributeInterface $productAttribute, ?string $localeCode, $value): ProductAttributeValueInterface
{
/** @var ProductAttributeInterface|null $productAttribute */
$productAttribute = $this->productAttributeRepository->findOneBy(['code' => $code]);

Assert::notNull($productAttribute, sprintf('Can not find product attribute with code: "%s"', $code));

/** @var ProductAttributeValueInterface $productAttributeValue */
$productAttributeValue = $this->productAttributeValueFactory->createNew();
$productAttributeValue->setAttribute($productAttribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected function configureResourceNode(ArrayNodeDefinition $resourceNode): voi
->children()
->scalarNode('name')->cannotBeEmpty()->end()
->scalarNode('code')->cannotBeEmpty()->end()
->booleanNode('translatable')->defaultTrue()->end()
->enumNode('type')->values($this->attributeTypes)->cannotBeEmpty()->end()
->variableNode('configuration')->end()
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ sylius_fixtures:
- name: 'Dress material'
code: 'dress_material'
type: 'text'

- name: 'Length'
code: 'length'
type: 'integer'
translatable: false

dress_option:
name: product_option
Expand Down Expand Up @@ -110,6 +115,7 @@ sylius_fixtures:
dress_brand: 'You are breathtaking'
dress_collection: 'Sylius Summer 2019'
dress_material: '100% polyester'
length: 100
product_options:
- 'dress_size'
- 'dress_height'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ sylius_fixtures:
code: 't_shirt_material'
type: 'text'

- name: 'Damage reduction'
code: 'damage_reduction'
type: 'percent'
translatable: false

tshirt_option:
name: product_option
options:
Expand Down Expand Up @@ -90,6 +95,7 @@ sylius_fixtures:
t_shirt_brand: 'You are breathtaking'
t_shirt_collection: 'Sylius Summer 2019'
t_shirt_material: '100% cotton'
damage_reduction: 0.1
product_options:
- 't_shirt_size'
images:
Expand Down

0 comments on commit adfda63

Please sign in to comment.