Skip to content

Commit

Permalink
Fix association types are not visible if no translation is provided b…
Browse files Browse the repository at this point in the history
…y Akeneo
  • Loading branch information
TheGrimmChester committed Oct 20, 2023
1 parent 68acec5 commit 4e85a0e
Showing 1 changed file with 16 additions and 26 deletions.
42 changes: 16 additions & 26 deletions src/Task/AssociationType/BatchAssociationTypesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Sylius\Component\Product\Model\ProductAssociationTypeInterface;
use Sylius\Component\Product\Model\ProductAssociationTypeTranslationInterface;
use Sylius\Component\Product\Repository\ProductAssociationTypeRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Synolia\SyliusAkeneoPlugin\Exceptions\Attribute\ExcludedAttributeException;
use Synolia\SyliusAkeneoPlugin\Exceptions\Attribute\InvalidAttributeException;
use Synolia\SyliusAkeneoPlugin\Exceptions\NoAttributeResourcesException;
use Synolia\SyliusAkeneoPlugin\Exceptions\UnsupportedAttributeTypeException;
use Synolia\SyliusAkeneoPlugin\Logger\Messages;
use Synolia\SyliusAkeneoPlugin\Payload\Association\AssociationTypePayload;
use Synolia\SyliusAkeneoPlugin\Payload\PipelinePayloadInterface;
use Synolia\SyliusAkeneoPlugin\Provider\SyliusAkeneoLocaleCodeProvider;
use Synolia\SyliusAkeneoPlugin\Task\AbstractBatchTask;
use Throwable;

Expand All @@ -31,9 +30,8 @@ public function __construct(
EntityManagerInterface $entityManager,
private LoggerInterface $logger,
private FactoryInterface $productAssociationTypeFactory,
private FactoryInterface $productAssociationTypeTranslationFactory,
private ProductAssociationTypeRepositoryInterface $productAssociationTypeRepository,
private RepositoryInterface $productAssociationTypeTranslationRepository,
private SyliusAkeneoLocaleCodeProvider $syliusAkeneoLocaleCodeProvider,
) {
parent::__construct($entityManager);
}
Expand All @@ -59,6 +57,7 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte

while ($results = $queryResult->fetchAll()) {
foreach ($results as $result) {
/** @var array{code: string, labels: array} $resource */
$resource = json_decode($result['values'], true, 512, \JSON_THROW_ON_ERROR);

try {
Expand All @@ -75,7 +74,7 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte
$productAssociationType->setCode($resource['code']);
}

$this->addTranslations($resource, $productAssociationType);
$this->setTranslations($resource['labels'], $productAssociationType);

$this->entityManager->flush();

Expand Down Expand Up @@ -113,30 +112,21 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte
return $payload;
}

private function addTranslations(array $resource, ProductAssociationTypeInterface $productAssociationType): void
private function setTranslations(array $labels, ProductAssociationTypeInterface $productAssociationType): void
{
foreach ($resource['labels'] as $localeCode => $label) {
$productAssociationTypeTranslation = $this->productAssociationTypeTranslationRepository->findOneBy([
'translatable' => $productAssociationType,
'locale' => $localeCode,
]);

if (!$productAssociationTypeTranslation instanceof ProductAssociationTypeTranslationInterface) {
$productAssociationTypeTranslation = $this->createTranslation($localeCode, $label);
}
foreach ($this->syliusAkeneoLocaleCodeProvider->getUsedLocalesOnBothPlatforms() as $usedLocalesOnBothPlatform) {
$akeneoLocale = $this->syliusAkeneoLocaleCodeProvider->getAkeneoLocale($usedLocalesOnBothPlatform);

$productAssociationType->addTranslation($productAssociationTypeTranslation);
}
}
$productAssociationType->setCurrentLocale($usedLocalesOnBothPlatform);
$productAssociationType->setFallbackLocale($usedLocalesOnBothPlatform);

private function createTranslation(string $localeCode, string $label): ProductAssociationTypeTranslationInterface
{
/** @var ProductAssociationTypeTranslationInterface $productAssociationTypeTranslation */
$productAssociationTypeTranslation = $this->productAssociationTypeTranslationFactory->createNew();
$productAssociationTypeTranslation->setLocale($localeCode);
$productAssociationTypeTranslation->setName($label);
$this->entityManager->persist($productAssociationTypeTranslation);
if (!isset($labels[$akeneoLocale])) {
$productAssociationType->setName(sprintf('[%s]', $productAssociationType->getCode()));

return $productAssociationTypeTranslation;
continue;
}

$productAssociationType->setName($labels[$akeneoLocale]);
}
}
}

0 comments on commit 4e85a0e

Please sign in to comment.