From fbc2c2eca81014c117c5b29fe6ebdbe613e16a78 Mon Sep 17 00:00:00 2001 From: jkindly Date: Wed, 18 Sep 2024 08:53:16 +0200 Subject: [PATCH 1/4] OP-525: Content migration script --- doc/block_legacy.csv | 2 + doc/legacy_data_migration.md | 49 +++++++ doc/page_legacy.csv | 2 + src/Entity/Block.php | 3 - src/Entity/BlockInterface.php | 1 - src/Fixture/BlockFixture.php | 1 - src/Fixture/Factory/BlockFixtureFactory.php | 3 - src/Form/Type/BlockType.php | 1 - src/Helper/ContentElementHelper.php | 85 ++++++++++++ src/Importer/AbstractImporter.php | 8 +- src/Importer/BlockImporter.php | 3 - src/Importer/BlockImporterInterface.php | 2 - src/Importer/Legacy/LegacyBlockImporter.php | 109 +++++++++++++++ .../Legacy/LegacyBlockImporterInterface.php | 24 ++++ src/Importer/Legacy/LegacyPageImporter.php | 129 ++++++++++++++++++ .../Legacy/LegacyPageImporterInterface.php | 34 +++++ src/Migrations/Version20240916100051.php | 30 ++++ src/Resolver/BlockResourceResolver.php | 19 --- src/Resources/config/doctrine/Block.orm.xml | 11 -- src/Resources/config/services/fixture.xml | 1 - src/Resources/config/services/importer.xml | 28 +++- src/Resources/config/services/resolver.xml | 2 - .../views/Block/Crud/_form.html.twig | 1 - .../config/packages/sylius_cms_plugin.yaml | 6 - 24 files changed, 493 insertions(+), 61 deletions(-) create mode 100644 doc/block_legacy.csv create mode 100644 doc/legacy_data_migration.md create mode 100644 doc/page_legacy.csv create mode 100644 src/Helper/ContentElementHelper.php create mode 100644 src/Importer/Legacy/LegacyBlockImporter.php create mode 100644 src/Importer/Legacy/LegacyBlockImporterInterface.php create mode 100644 src/Importer/Legacy/LegacyPageImporter.php create mode 100644 src/Importer/Legacy/LegacyPageImporterInterface.php create mode 100644 src/Migrations/Version20240916100051.php diff --git a/doc/block_legacy.csv b/doc/block_legacy.csv new file mode 100644 index 000000000..0e592d202 --- /dev/null +++ b/doc/block_legacy.csv @@ -0,0 +1,2 @@ +code,type,name_en_US,content_en_US,sections,channels,products,image_en_US,slug_en_US +test4,image,Test,test,"blog, general",US_WEB,"010ba66b-adee-3d6e-9d63-67c44d686db1, 01d35db9-247d-3834-b300-20483d5e34e8",https://bitbag.shop/assets/web/images/header-logo.png,https://bitbag.shop/assets/web/images/header-logo.png diff --git a/doc/legacy_data_migration.md b/doc/legacy_data_migration.md new file mode 100644 index 000000000..4ee2ce364 --- /dev/null +++ b/doc/legacy_data_migration.md @@ -0,0 +1,49 @@ +# Legacy data migration + +## Introduction + +You can migrate your blocks & pages from the 4.x version to the 5.x version of the plugin. +To do so, you need to follow the steps below. + +## Steps + +1. Create new CSV files with blocks & pages data in the 4.x format. +See an example in [block_legacy.csv](block_legacy.csv) or [page_legacy.csv](page_legacy.csv). +2. Install the 5.x version of the plugin. +3. Go to the console and run the following command: +```bash +bin/console cms:import:csv page_legacy {file_path}.csv +bin/console cms:import:csv block_legacy {file_path}.csv +``` + +## Info about legacy CSV files columns + +### Blocks + +- **code** - block code. +- **type** - it will be ignored. +- **name_LOCALE** - block name. First occurrence of its column is the default name for the block. +For each locale, there will be created a Heading content element. +- **content_LOCALE** - block content. For each locale, there will be created a Textarea content element. +- **sections** - it will be converted to the block's collections. +- **channels** - block channels. +- **products** - block products. There will be created Products grid content element. +- **image_LOCALE** - block image. For each locale, there will be created a Single media content element. +- **slug_LOCALE** - it will be ignored. + +### Pages + +- **code** - page code. +- **sections** - it will be converted to the page's collections. +- **channels** - page channels. +- **products** - page products. There will be created Products grid content element. +- **slug_LOCALE** - page slug. +- **name_LOCALE** - page name. First occurrence of its column is the default name for the page. +For each locale, there will be created a Heading content element. +- **image_LOCALE** - page image. For each locale, there will be created a Single media content element. +- **meta_keywords_LOCALE** - page meta keywords. +- **meta_description_LOCALE** - page meta description. +- **content_LOCALE** - page content. For each locale, there will be created a Textarea content element. +- **breadcrumb_LOCALE** - it will be ignored. +- **name_when_linked_LOCALE** - for each locale, there will be created a teaser title. +- **description_when_linked_LOCALE** - for each locale, there will be created a teaser content. diff --git a/doc/page_legacy.csv b/doc/page_legacy.csv new file mode 100644 index 000000000..47d208dd7 --- /dev/null +++ b/doc/page_legacy.csv @@ -0,0 +1,2 @@ +code,sections,channels,products,slug_en_US,name_en_US,image_en_US,meta_keywords_en_US,meta_description_en_US,content_en_US,breadcrumb_en_US,name_when_linked_en_US,description_when_linked_en_US +aboutUS,,US_WEB,,about_us,About US,,About US,About US,"",,, diff --git a/src/Entity/Block.php b/src/Entity/Block.php index a84129313..e18a7d821 100755 --- a/src/Entity/Block.php +++ b/src/Entity/Block.php @@ -7,7 +7,6 @@ use Sylius\CmsPlugin\Entity\Trait\ChannelsAwareTrait; use Sylius\CmsPlugin\Entity\Trait\CollectibleTrait; use Sylius\CmsPlugin\Entity\Trait\ContentElementsAwareTrait; -use Sylius\CmsPlugin\Entity\Trait\LocaleAwareTrait; use Sylius\CmsPlugin\Entity\Trait\ProductsAwareTrait; use Sylius\CmsPlugin\Entity\Trait\ProductsInTaxonsAwareTrait; use Sylius\CmsPlugin\Entity\Trait\TaxonAwareTrait; @@ -19,7 +18,6 @@ class Block implements BlockInterface use CollectibleTrait; use ChannelsAwareTrait; use ContentElementsAwareTrait; - use LocaleAwareTrait; use ProductsAwareTrait; use TaxonAwareTrait; use ProductsInTaxonsAwareTrait; @@ -29,7 +27,6 @@ public function __construct() $this->initializeCollectionsCollection(); $this->initializeChannelsCollection(); $this->initializeContentElementsCollection(); - $this->initializeLocalesCollection(); $this->initializeProductsCollection(); $this->initializeTaxonCollection(); $this->initializeProductsInTaxonsCollection(); diff --git a/src/Entity/BlockInterface.php b/src/Entity/BlockInterface.php index 816b0d782..d3deca301 100755 --- a/src/Entity/BlockInterface.php +++ b/src/Entity/BlockInterface.php @@ -14,7 +14,6 @@ interface BlockInterface extends CollectibleInterface, ChannelsAwareInterface, ContentElementsAwareInterface, - LocaleAwareInterface, ProductsAwareInterface, TaxonAwareInterface, ProductsInTaxonsAwareInterface, diff --git a/src/Fixture/BlockFixture.php b/src/Fixture/BlockFixture.php index f20633354..46ebb4bba 100755 --- a/src/Fixture/BlockFixture.php +++ b/src/Fixture/BlockFixture.php @@ -36,7 +36,6 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void ->booleanNode('enabled')->defaultTrue()->end() ->arrayNode('collections')->scalarPrototype()->end()->end() ->arrayNode('channels')->scalarPrototype()->end()->end() - ->arrayNode('locales')->scalarPrototype()->end()->end() ->arrayNode('products')->scalarPrototype()->end()->end() ->arrayNode('taxons')->scalarPrototype()->end()->end() ->arrayNode('products_in_taxons')->scalarPrototype()->end()->end() diff --git a/src/Fixture/Factory/BlockFixtureFactory.php b/src/Fixture/Factory/BlockFixtureFactory.php index 3faee5e62..93f09ed73 100755 --- a/src/Fixture/Factory/BlockFixtureFactory.php +++ b/src/Fixture/Factory/BlockFixtureFactory.php @@ -6,7 +6,6 @@ use Sylius\CmsPlugin\Assigner\ChannelsAssignerInterface; use Sylius\CmsPlugin\Assigner\CollectionsAssignerInterface; -use Sylius\CmsPlugin\Assigner\LocalesAssignerInterface; use Sylius\CmsPlugin\Assigner\ProductsAssignerInterface; use Sylius\CmsPlugin\Assigner\ProductsInTaxonsAssignerInterface; use Sylius\CmsPlugin\Assigner\TaxonsAssignerInterface; @@ -22,7 +21,6 @@ public function __construct( private BlockRepositoryInterface $blockRepository, private CollectionsAssignerInterface $collectionsAssigner, private ChannelsAssignerInterface $channelAssigner, - private LocalesAssignerInterface $localesAssigner, private ProductsAssignerInterface $productsAssigner, private TaxonsAssignerInterface $taxonsAssigner, private ProductsInTaxonsAssignerInterface $productsInTaxonsAssigner, @@ -56,7 +54,6 @@ private function createBlock(string $code, array $blockData): void $this->collectionsAssigner->assign($block, $blockData['collections']); $this->channelAssigner->assign($block, $blockData['channels']); - $this->localesAssigner->assign($block, $blockData['locales']); $this->productsAssigner->assign($block, $blockData['products']); $this->taxonsAssigner->assign($block, $blockData['taxons']); $this->productsInTaxonsAssigner->assign($block, $blockData['products_in_taxons']); diff --git a/src/Form/Type/BlockType.php b/src/Form/Type/BlockType.php index a3a436c52..497d8f21c 100755 --- a/src/Form/Type/BlockType.php +++ b/src/Form/Type/BlockType.php @@ -61,7 +61,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'multiple' => true, 'expanded' => true, ]) - ->add('locales') ->add('contentElements', CollectionType::class, [ 'label' => false, 'entry_type' => ContentConfigurationType::class, diff --git a/src/Helper/ContentElementHelper.php b/src/Helper/ContentElementHelper.php new file mode 100644 index 000000000..32c7acd3e --- /dev/null +++ b/src/Helper/ContentElementHelper.php @@ -0,0 +1,85 @@ +setLocale($locale ?? 'en_US'); + $contentConfiguration->setType('heading'); + $contentConfiguration->setConfiguration([ + 'heading_type' => $headingType ?? 'h1', + 'heading' => $headingContent, + ]); + + return $contentConfiguration; + } + + public static function createTextareaContentElement(?string $locale, ?string $content): ?ContentConfiguration + { + if (null === $content) { + return null; + } + + $contentConfiguration = new ContentConfiguration(); + $contentConfiguration->setLocale($locale ?? 'en_US'); + $contentConfiguration->setType('textarea'); + $contentConfiguration->setConfiguration([ + 'textarea' => $content, + ]); + + return $contentConfiguration; + } + + public static function createProductsGridContentElement(?string $locale, ?string $codes): ?ContentConfiguration + { + if (null === $codes) { + return null; + } + + $productsCodes = explode(',', $codes); + $productsCodes = array_map(static function (string $element): string { + return trim($element); + }, $productsCodes); + + $contentConfiguration = new ContentConfiguration(); + $contentConfiguration->setLocale($locale ?? 'en_US'); + $contentConfiguration->setType('products_grid'); + $contentConfiguration->setConfiguration([ + 'products_grid' => [ + 'products' => $productsCodes, + ], + ]); + + return $contentConfiguration; + } + + public static function createSingleMediaContentElement(?string $locale, ?string $code): ?ContentConfiguration + { + if (null === $code) { + return null; + } + + $contentConfiguration = new ContentConfiguration(); + $contentConfiguration->setLocale($locale ?? 'en_US'); + $contentConfiguration->setType('single_media'); + $contentConfiguration->setConfiguration([ + 'single_media' => $code, + ]); + + return $contentConfiguration; + } +} diff --git a/src/Importer/AbstractImporter.php b/src/Importer/AbstractImporter.php index 656f5964e..7df177e3b 100755 --- a/src/Importer/AbstractImporter.php +++ b/src/Importer/AbstractImporter.php @@ -38,10 +38,12 @@ protected function getAvailableLocales(array $translatableColumns, array $column foreach ($translatableColumns as $translatableColumn) { $translatableColumn = str_replace('__locale__', '_', $translatableColumn); - foreach ($columns as $column) { - if (str_starts_with($column, $translatableColumn)) { - $locales[] = str_replace($translatableColumn, '', $column); + if ( + str_starts_with($column, $translatableColumn) && + preg_match('/^' . preg_quote($translatableColumn, '/') . '([a-z]{2}_[A-Z]{2})$/', $column, $matches) + ) { + $locales[] = $matches[1]; } } } diff --git a/src/Importer/BlockImporter.php b/src/Importer/BlockImporter.php index 7d1e63b5b..4a1f0491a 100644 --- a/src/Importer/BlockImporter.php +++ b/src/Importer/BlockImporter.php @@ -8,7 +8,6 @@ use Sylius\CmsPlugin\Repository\BlockRepositoryInterface; use Sylius\CmsPlugin\Resolver\Importer\ImporterChannelsResolverInterface; use Sylius\CmsPlugin\Resolver\Importer\ImporterCollectionsResolverInterface; -use Sylius\CmsPlugin\Resolver\Importer\ImporterLocalesResolverInterface; use Sylius\CmsPlugin\Resolver\Importer\ImporterProductsInTaxonsResolverInterface; use Sylius\CmsPlugin\Resolver\Importer\ImporterProductsResolverInterface; use Sylius\CmsPlugin\Resolver\Importer\ImporterTaxonsResolverInterface; @@ -22,7 +21,6 @@ public function __construct( private ResourceResolverInterface $blockResourceResolver, private ImporterCollectionsResolverInterface $importerCollectionsResolver, private ImporterChannelsResolverInterface $importerChannelsResolver, - private ImporterLocalesResolverInterface $importerLocalesResolver, private ImporterProductsResolverInterface $importerProductsResolver, private ImporterTaxonsResolverInterface $importerTaxonsResolver, private ImporterProductsInTaxonsResolverInterface $importerProductsInTaxonsResolver, @@ -45,7 +43,6 @@ public function import(array $row): void $this->importerCollectionsResolver->resolve($block, $this->getColumnValue(self::COLLECTIONS_COLUMN, $row)); $this->importerChannelsResolver->resolve($block, $this->getColumnValue(self::CHANNELS_COLUMN, $row)); - $this->importerLocalesResolver->resolve($block, $this->getColumnValue(self::LOCALES_COLUMN, $row)); $this->importerProductsResolver->resolve($block, $this->getColumnValue(self::PRODUCTS_COLUMN, $row)); $this->importerTaxonsResolver->resolve($block, $this->getColumnValue(self::TAXONS_COLUMN, $row)); $this->importerProductsInTaxonsResolver->resolve($block, $this->getColumnValue(self::PRODUCTS_IN_TAXONS_COLUMN, $row)); diff --git a/src/Importer/BlockImporterInterface.php b/src/Importer/BlockImporterInterface.php index 9ea13e71e..a22b25457 100644 --- a/src/Importer/BlockImporterInterface.php +++ b/src/Importer/BlockImporterInterface.php @@ -14,8 +14,6 @@ interface BlockImporterInterface extends ImporterInterface public const COLLECTIONS_COLUMN = 'collections'; - public const LOCALES_COLUMN = 'locales'; - public const CHANNELS_COLUMN = 'channels'; public const PRODUCTS_COLUMN = 'products'; diff --git a/src/Importer/Legacy/LegacyBlockImporter.php b/src/Importer/Legacy/LegacyBlockImporter.php new file mode 100644 index 000000000..00c47eb7e --- /dev/null +++ b/src/Importer/Legacy/LegacyBlockImporter.php @@ -0,0 +1,109 @@ +getColumnValue(self::CODE_COLUMN, $row); + Assert::notNull($code); + /** @var BlockInterface $block */ + $block = $this->blockResourceResolver->getResource($code); + $block->setCode($code); + + $this->importerCollectionsResolver->resolve($block, $this->getColumnValue(self::SECTIONS_COLUMN, $row)); + $this->importerChannelsResolver->resolve($block, $this->getColumnValue(self::CHANNELS_COLUMN, $row)); + + $translationArray = $this->getAvailableLocales($this->getTranslatableColumns(), array_keys($row)); + foreach ($translationArray as $key => $locale) { + if ($key === array_key_first($translationArray)) { + $block->setName($this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row)); + } + + $heading = ContentElementHelper::createHeadingContentElement( + $locale, + 'h2', + $this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row), + ); + if ($heading) { + $heading->setBlock($block); + $block->addContentElement($heading); + } + + $singleMedia = ContentElementHelper::createSingleMediaContentElement( + $locale, + $this->getTranslatableColumnValue(self::IMAGE_COLUMN, $locale, $row), + ); + if ($singleMedia) { + $singleMedia->setBlock($block); + $block->addContentElement($singleMedia); + } + + $content = ContentElementHelper::createTextareaContentElement( + $locale, + $this->getTranslatableColumnValue(self::CONTENT_COLUMN, $locale, $row), + ); + if ($content) { + $content->setBlock($block); + $block->addContentElement($content); + } + } + + $locales = $this->localeRepository->findAll(); + /** @var LocaleInterface $locale */ + foreach ($locales as $locale) { + $productsGrid = ContentElementHelper::createProductsGridContentElement( + $locale->getCode(), + $this->getColumnValue(self::PRODUCTS_COLUMN, $row), + ); + if ($productsGrid) { + $productsGrid->setBlock($block); + $block->addContentElement($productsGrid); + } + } + + $this->validateResource($block, ['cms']); + $this->blockRepository->add($block); + } + + public function getResourceCode(): string + { + return 'block_legacy'; + } + + private function getTranslatableColumns(): array + { + return [ + self::NAME_COLUMN, + self::CONTENT_COLUMN, + self::IMAGE_COLUMN, + ]; + } +} diff --git a/src/Importer/Legacy/LegacyBlockImporterInterface.php b/src/Importer/Legacy/LegacyBlockImporterInterface.php new file mode 100644 index 000000000..182699840 --- /dev/null +++ b/src/Importer/Legacy/LegacyBlockImporterInterface.php @@ -0,0 +1,24 @@ +getColumnValue(self::CODE_COLUMN, $row); + Assert::notNull($code); + + /** @var PageInterface $page */ + $page = $this->pageResourceResolver->getResource($code); + + $page->setCode($code); + $page->setFallbackLocale($this->localeContext->getLocaleCode()); + + $this->importerCollectionsResolver->resolve($page, $this->getColumnValue(self::SECTIONS_COLUMN, $row)); + $this->importerChannelsResolver->resolve($page, $this->getColumnValue(self::CHANNELS_COLUMN, $row)); + + $translationArray = $this->getAvailableLocales($this->getTranslatableColumns(), array_keys($row)); + foreach ($translationArray as $key => $locale) { + $page->setCurrentLocale($locale); + $page->setSlug($this->getTranslatableColumnValue(self::SLUG_COLUMN, $locale, $row)); + $page->setMetaKeywords($this->getTranslatableColumnValue(self::META_KEYWORDS_COLUMN, $locale, $row)); + $page->setMetaDescription($this->getTranslatableColumnValue(self::META_DESCRIPTION_COLUMN, $locale, $row)); + + if ($key === array_key_first($translationArray)) { + $page->setName($this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row)); + } + + $page->setTeaserTitle($this->getTranslatableColumnValue(self::NAME_WHEN_LINKED_COLUMN, $locale, $row)); + $page->setTeaserContent($this->getTranslatableColumnValue(self::DESCRIPTION_WHEN_LINKED_COLUMN, $locale, $row)); + + $heading = ContentElementHelper::createHeadingContentElement( + $locale, + 'h2', + $this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row), + ); + if ($heading) { + $heading->setPage($page); + $page->addContentElement($heading); + } + + $singleMedia = ContentElementHelper::createSingleMediaContentElement( + $locale, + $this->getTranslatableColumnValue(self::IMAGE_COLUMN, $locale, $row), + ); + if ($singleMedia) { + $singleMedia->setPage($page); + $page->addContentElement($singleMedia); + } + + $content = ContentElementHelper::createTextareaContentElement( + $locale, + $this->getTranslatableColumnValue(self::CONTENT_COLUMN, $locale, $row), + ); + if ($content) { + $content->setPage($page); + $page->addContentElement($content); + } + } + + $locales = $this->localeRepository->findAll(); + /** @var LocaleInterface $locale */ + foreach ($locales as $locale) { + $productsGrid = ContentElementHelper::createProductsGridContentElement( + $locale->getCode(), + $this->getColumnValue(self::PRODUCTS_COLUMN, $row), + ); + if ($productsGrid) { + $productsGrid->setPage($page); + $page->addContentElement($productsGrid); + } + } + + $this->validateResource($page, ['cms']); + + $this->entityManager->persist($page); + $this->entityManager->flush(); + } + + public function getResourceCode(): string + { + return 'page_legacy'; + } + + private function getTranslatableColumns(): array + { + return [ + self::SLUG_COLUMN, + self::NAME_COLUMN, + self::IMAGE_COLUMN, + self::CONTENT_COLUMN, + self::META_KEYWORDS_COLUMN, + self::META_DESCRIPTION_COLUMN, + self::NAME_WHEN_LINKED_COLUMN, + self::DESCRIPTION_WHEN_LINKED_COLUMN, + ]; + } +} diff --git a/src/Importer/Legacy/LegacyPageImporterInterface.php b/src/Importer/Legacy/LegacyPageImporterInterface.php new file mode 100644 index 000000000..bc6e980f4 --- /dev/null +++ b/src/Importer/Legacy/LegacyPageImporterInterface.php @@ -0,0 +1,34 @@ +addSql('ALTER TABLE sylius_cms_block_locales DROP FOREIGN KEY FK_49C0AACE559DFD1'); + $this->addSql('ALTER TABLE sylius_cms_block_locales DROP FOREIGN KEY FK_49C0AACE9ED820C'); + $this->addSql('DROP TABLE sylius_cms_block_locales'); + } + + public function down(Schema $schema): void + { + $this->addSql('CREATE TABLE sylius_cms_block_locales (block_id INT NOT NULL, locale_id INT NOT NULL, INDEX IDX_49C0AACE9ED820C (block_id), INDEX IDX_49C0AACE559DFD1 (locale_id), PRIMARY KEY(block_id, locale_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB COMMENT = \'\' '); + $this->addSql('ALTER TABLE sylius_cms_block_locales ADD CONSTRAINT FK_49C0AACE559DFD1 FOREIGN KEY (locale_id) REFERENCES sylius_locale (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE sylius_cms_block_locales ADD CONSTRAINT FK_49C0AACE9ED820C FOREIGN KEY (block_id) REFERENCES sylius_cms_block (id) ON DELETE CASCADE'); + } +} diff --git a/src/Resolver/BlockResourceResolver.php b/src/Resolver/BlockResourceResolver.php index 980c0e0e4..1c42495ef 100755 --- a/src/Resolver/BlockResourceResolver.php +++ b/src/Resolver/BlockResourceResolver.php @@ -8,9 +8,6 @@ use Sylius\CmsPlugin\Entity\BlockInterface; use Sylius\CmsPlugin\Repository\BlockRepositoryInterface; use Sylius\Component\Channel\Context\ChannelContextInterface; -use Sylius\Component\Locale\Context\LocaleContextInterface; -use Sylius\Component\Locale\Model\LocaleInterface; -use Sylius\Component\Resource\Repository\RepositoryInterface; use Webmozart\Assert\Assert; final class BlockResourceResolver implements BlockResourceResolverInterface @@ -19,8 +16,6 @@ public function __construct( private BlockRepositoryInterface $blockRepository, private LoggerInterface $logger, private ChannelContextInterface $channelContext, - private LocaleContextInterface $localeContext, - private RepositoryInterface $localeRepository, ) { } @@ -39,20 +34,6 @@ public function findOrLog(string $code): ?BlockInterface return null; } - /** @var LocaleInterface $locale */ - $locale = $this->localeRepository->findOneBy(['code' => $this->localeContext->getLocaleCode()]); - Assert::notNull($locale); - - if (false === $block->hasLocale($locale) && 0 !== $block->getLocales()->count()) { - $this->logger->warning(sprintf( - 'Block with "%s" code was found in the database, but it does not have "%s" locale.', - $code, - $this->localeContext->getLocaleCode(), - )); - - return null; - } - return $block; } } diff --git a/src/Resources/config/doctrine/Block.orm.xml b/src/Resources/config/doctrine/Block.orm.xml index da1955577..fea56d100 100644 --- a/src/Resources/config/doctrine/Block.orm.xml +++ b/src/Resources/config/doctrine/Block.orm.xml @@ -38,17 +38,6 @@ - - - - - - - - - - - diff --git a/src/Resources/config/services/fixture.xml b/src/Resources/config/services/fixture.xml index 947b58398..6258eceb5 100644 --- a/src/Resources/config/services/fixture.xml +++ b/src/Resources/config/services/fixture.xml @@ -34,7 +34,6 @@ - diff --git a/src/Resources/config/services/importer.xml b/src/Resources/config/services/importer.xml index f5ad25ca3..1ed179826 100644 --- a/src/Resources/config/services/importer.xml +++ b/src/Resources/config/services/importer.xml @@ -13,20 +13,19 @@ - + - - + @@ -35,7 +34,28 @@ - + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Resources/config/services/resolver.xml b/src/Resources/config/services/resolver.xml index 230abbbc5..2aaf7d5b7 100644 --- a/src/Resources/config/services/resolver.xml +++ b/src/Resources/config/services/resolver.xml @@ -32,8 +32,6 @@ - - diff --git a/src/Resources/views/Block/Crud/_form.html.twig b/src/Resources/views/Block/Crud/_form.html.twig index d7fdf6deb..23c580606 100755 --- a/src/Resources/views/Block/Crud/_form.html.twig +++ b/src/Resources/views/Block/Crud/_form.html.twig @@ -12,7 +12,6 @@ {{ form_row(form.code) }} {{ form_row(form.enabled) }} {{ form_row(form.channels) }} - {{ form_row(form.locales) }} {{ form_row(form.collections) }} Date: Wed, 18 Sep 2024 09:57:57 +0200 Subject: [PATCH 2/4] OP-525: PHPSpec fix --- spec/Importer/BlockImporterSpec.php | 4 -- spec/Resolver/BlockResourceResolverSpec.php | 61 --------------------- src/Importer/AbstractImporter.php | 12 ++-- 3 files changed, 6 insertions(+), 71 deletions(-) diff --git a/spec/Importer/BlockImporterSpec.php b/spec/Importer/BlockImporterSpec.php index 22af5226a..9c6566908 100644 --- a/spec/Importer/BlockImporterSpec.php +++ b/spec/Importer/BlockImporterSpec.php @@ -25,7 +25,6 @@ public function let( ResourceResolverInterface $blockResourceResolver, ImporterCollectionsResolverInterface $importerCollectionsResolver, ImporterChannelsResolverInterface $importerChannelsResolver, - ImporterLocalesResolverInterface $importerLocalesResolver, ImporterProductsResolverInterface $importerProductsResolver, ImporterTaxonsResolverInterface $importerTaxonsResolver, ImporterProductsInTaxonsResolverInterface $importerProductsInTaxonsResolver, @@ -36,7 +35,6 @@ public function let( $blockResourceResolver, $importerCollectionsResolver, $importerChannelsResolver, - $importerLocalesResolver, $importerProductsResolver, $importerTaxonsResolver, $importerProductsInTaxonsResolver, @@ -55,7 +53,6 @@ public function it_imports_block( ResourceResolverInterface $blockResourceResolver, ImporterCollectionsResolverInterface $importerCollectionsResolver, ImporterChannelsResolverInterface $importerChannelsResolver, - ImporterLocalesResolverInterface $importerLocalesResolver, ImporterProductsResolverInterface $importerProductsResolver, ImporterTaxonsResolverInterface $importerTaxonsResolver, ImporterProductsInTaxonsResolverInterface $importerProductsInTaxonsResolver, @@ -73,7 +70,6 @@ public function it_imports_block( $importerCollectionsResolver->resolve($block, null)->shouldBeCalled(); $importerChannelsResolver->resolve($block, null)->shouldBeCalled(); - $importerLocalesResolver->resolve($block, null)->shouldBeCalled(); $importerProductsResolver->resolve($block, null)->shouldBeCalled(); $importerTaxonsResolver->resolve($block, null)->shouldBeCalled(); $importerProductsInTaxonsResolver->resolve($block, null)->shouldBeCalled(); diff --git a/spec/Resolver/BlockResourceResolverSpec.php b/spec/Resolver/BlockResourceResolverSpec.php index 45d77c9e7..b87eb2060 100755 --- a/spec/Resolver/BlockResourceResolverSpec.php +++ b/spec/Resolver/BlockResourceResolverSpec.php @@ -4,7 +4,6 @@ namespace spec\Sylius\CmsPlugin\Resolver; -use Doctrine\Common\Collections\ArrayCollection; use PhpSpec\ObjectBehavior; use Psr\Log\LoggerInterface; use Sylius\CmsPlugin\Entity\BlockInterface; @@ -14,7 +13,6 @@ use Sylius\Component\Channel\Context\ChannelContextInterface; use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Locale\Context\LocaleContextInterface; -use Sylius\Component\Locale\Model\LocaleInterface; use Sylius\Component\Resource\Repository\RepositoryInterface; final class BlockResourceResolverSpec extends ObjectBehavior @@ -39,74 +37,15 @@ public function it_implements_block_resource_resolver_interface(): void $this->shouldHaveType(BlockResourceResolverInterface::class); } - public function it_logs_warning_if_block_was_not_found( - BlockRepositoryInterface $blockRepository, - LoggerInterface $logger, - ChannelContextInterface $channelContext, - ChannelInterface $channel, - ) { - $channel->getCode()->willReturn('WEB'); - $channelContext->getChannel()->willReturn($channel); - $blockRepository->findEnabledByCode('homepage_banner', 'WEB')->willReturn(null); - - $logger - ->warning(sprintf( - 'Block with "%s" code was not found in the database.', - 'homepage_banner', - )) - ->shouldBeCalled() - ; - - $this->findOrLog('homepage_banner'); - } - - public function it_logs_warning_if_block_was_found_but_it_does_not_have_locale( - BlockRepositoryInterface $blockRepository, - LoggerInterface $logger, - ChannelContextInterface $channelContext, - ChannelInterface $channel, - LocaleContextInterface $localeContext, - LocaleInterface $locale, - RepositoryInterface $localeRepository, - BlockInterface $block, - ) { - $channel->getCode()->willReturn('WEB'); - $channelContext->getChannel()->willReturn($channel); - $blockRepository->findEnabledByCode('homepage_banner', 'WEB')->willReturn($block); - $localeContext->getLocaleCode()->willReturn('en_US'); - $locale->getCode()->willReturn('en_US'); - $localeRepository->findOneBy(['code' => 'en_US'])->willReturn($locale); - $block->hasLocale($locale)->willReturn(false); - $block->getLocales()->willReturn(new ArrayCollection(['pl_PL'])); - - $logger - ->warning(sprintf( - 'Block with "%s" code was found in the database, but it does not have "%s" locale.', - 'homepage_banner', - 'en_US', - )) - ->shouldBeCalled() - ; - - $this->findOrLog('homepage_banner'); - } - public function it_returns_block_if_found_in_database( BlockRepositoryInterface $blockRepository, BlockInterface $block, ChannelContextInterface $channelContext, ChannelInterface $channel, - LocaleContextInterface $localeContext, - LocaleInterface $locale, - RepositoryInterface $localeRepository, ) { $channel->getCode()->willReturn('WEB'); $channelContext->getChannel()->willReturn($channel); $blockRepository->findEnabledByCode('homepage_banner', 'WEB')->willReturn($block); - $localeContext->getLocaleCode()->willReturn('en_US'); - $locale->getCode()->willReturn('en_US'); - $localeRepository->findOneBy(['code' => 'en_US'])->willReturn($locale); - $block->hasLocale($locale)->willReturn(true); $this->findOrLog('homepage_banner')->shouldReturn($block); } diff --git a/src/Importer/AbstractImporter.php b/src/Importer/AbstractImporter.php index 7df177e3b..e053006b8 100755 --- a/src/Importer/AbstractImporter.php +++ b/src/Importer/AbstractImporter.php @@ -35,15 +35,15 @@ protected function getTranslatableColumnValue( protected function getAvailableLocales(array $translatableColumns, array $columns): array { $locales = []; - foreach ($translatableColumns as $translatableColumn) { $translatableColumn = str_replace('__locale__', '_', $translatableColumn); foreach ($columns as $column) { - if ( - str_starts_with($column, $translatableColumn) && - preg_match('/^' . preg_quote($translatableColumn, '/') . '([a-z]{2}_[A-Z]{2})$/', $column, $matches) - ) { - $locales[] = $matches[1]; + if (str_starts_with($column, $translatableColumn)) { + $localePart = substr($column, strlen($translatableColumn)); + + if (preg_match('/^[a-z]{2}(_[A-Z]{2})?$/', $localePart)) { + $locales[] = $localePart; + } } } } From 4252179b729d3e280c7de83cd024a4cf7b8ca6eb Mon Sep 17 00:00:00 2001 From: jkindly Date: Wed, 18 Sep 2024 10:09:11 +0200 Subject: [PATCH 3/4] OP-525: PHPUnit fix --- src/Resources/config/api_resources/Block.xml | 1 - src/Resources/config/serialization/Block.xml | 3 --- tests/Functional/DataFixtures/ORM/Api/BlockTest/block.yml | 6 ------ .../Expected/Api/BlockTest/test_it_get_block_by_id.json | 3 --- 4 files changed, 13 deletions(-) diff --git a/src/Resources/config/api_resources/Block.xml b/src/Resources/config/api_resources/Block.xml index d1bec15bc..7081e0991 100644 --- a/src/Resources/config/api_resources/Block.xml +++ b/src/Resources/config/api_resources/Block.xml @@ -44,7 +44,6 @@ - diff --git a/src/Resources/config/serialization/Block.xml b/src/Resources/config/serialization/Block.xml index ad39aa163..c0648ecd9 100644 --- a/src/Resources/config/serialization/Block.xml +++ b/src/Resources/config/serialization/Block.xml @@ -23,9 +23,6 @@ sylius_cms:block:read:show - - sylius_cms:block:read:show - sylius_cms:block:read:show diff --git a/tests/Functional/DataFixtures/ORM/Api/BlockTest/block.yml b/tests/Functional/DataFixtures/ORM/Api/BlockTest/block.yml index 822e4510c..95c65dcb3 100644 --- a/tests/Functional/DataFixtures/ORM/Api/BlockTest/block.yml +++ b/tests/Functional/DataFixtures/ORM/Api/BlockTest/block.yml @@ -80,8 +80,6 @@ Sylius\CmsPlugin\Entity\Block: - '@collection1' channels: - '@channel' - locales: - - '@locale' block2: code: 'block2-code' name: 'block2-name' @@ -90,8 +88,6 @@ Sylius\CmsPlugin\Entity\Block: - '@collection2' channels: - '@channel' - locales: - - '@locale' block3: code: 'block3-code' name: 'block3-name' @@ -100,8 +96,6 @@ Sylius\CmsPlugin\Entity\Block: - '@collection3' channels: - '@channel' - locales: - - '@locale' Sylius\CmsPlugin\Entity\Collection: collection1: code: 'collection1-code' diff --git a/tests/Functional/Responses/Expected/Api/BlockTest/test_it_get_block_by_id.json b/tests/Functional/Responses/Expected/Api/BlockTest/test_it_get_block_by_id.json index 33c79870c..565ccbfd1 100644 --- a/tests/Functional/Responses/Expected/Api/BlockTest/test_it_get_block_by_id.json +++ b/tests/Functional/Responses/Expected/Api/BlockTest/test_it_get_block_by_id.json @@ -12,9 +12,6 @@ "channels": [ "/api/v2/shop/channels/code" ], - "locales": [ - "/api/v2/shop/locales/@string@" - ], "contentElements": [], "products": [], "taxons": [], From c8dc43a2541a2c5f69c8ebc7a901a176ef4de814 Mon Sep 17 00:00:00 2001 From: jkindly Date: Tue, 24 Sep 2024 08:53:24 +0200 Subject: [PATCH 4/4] OP-525: Change helper to factory --- .../ContentElementFactory.php} | 4 ++-- src/Importer/Legacy/LegacyBlockImporter.php | 10 +++++----- src/Importer/Legacy/LegacyPageImporter.php | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) rename src/{Helper/ContentElementHelper.php => Factory/ContentElementFactory.php} (97%) diff --git a/src/Helper/ContentElementHelper.php b/src/Factory/ContentElementFactory.php similarity index 97% rename from src/Helper/ContentElementHelper.php rename to src/Factory/ContentElementFactory.php index 32c7acd3e..18c960aee 100644 --- a/src/Helper/ContentElementHelper.php +++ b/src/Factory/ContentElementFactory.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Sylius\CmsPlugin\Helper; +namespace Sylius\CmsPlugin\Factory; use Sylius\CmsPlugin\Entity\ContentConfiguration; -final class ContentElementHelper +final class ContentElementFactory { public static function createHeadingContentElement( ?string $locale, diff --git a/src/Importer/Legacy/LegacyBlockImporter.php b/src/Importer/Legacy/LegacyBlockImporter.php index 00c47eb7e..587f5a8ff 100644 --- a/src/Importer/Legacy/LegacyBlockImporter.php +++ b/src/Importer/Legacy/LegacyBlockImporter.php @@ -5,7 +5,7 @@ namespace Sylius\CmsPlugin\Importer\Legacy; use Sylius\CmsPlugin\Entity\BlockInterface; -use Sylius\CmsPlugin\Helper\ContentElementHelper; +use Sylius\CmsPlugin\Factory\ContentElementFactory; use Sylius\CmsPlugin\Importer\AbstractImporter; use Sylius\CmsPlugin\Repository\BlockRepositoryInterface; use Sylius\CmsPlugin\Resolver\Importer\ImporterChannelsResolverInterface; @@ -47,7 +47,7 @@ public function import(array $row): void $block->setName($this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row)); } - $heading = ContentElementHelper::createHeadingContentElement( + $heading = ContentElementFactory::createHeadingContentElement( $locale, 'h2', $this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row), @@ -57,7 +57,7 @@ public function import(array $row): void $block->addContentElement($heading); } - $singleMedia = ContentElementHelper::createSingleMediaContentElement( + $singleMedia = ContentElementFactory::createSingleMediaContentElement( $locale, $this->getTranslatableColumnValue(self::IMAGE_COLUMN, $locale, $row), ); @@ -66,7 +66,7 @@ public function import(array $row): void $block->addContentElement($singleMedia); } - $content = ContentElementHelper::createTextareaContentElement( + $content = ContentElementFactory::createTextareaContentElement( $locale, $this->getTranslatableColumnValue(self::CONTENT_COLUMN, $locale, $row), ); @@ -79,7 +79,7 @@ public function import(array $row): void $locales = $this->localeRepository->findAll(); /** @var LocaleInterface $locale */ foreach ($locales as $locale) { - $productsGrid = ContentElementHelper::createProductsGridContentElement( + $productsGrid = ContentElementFactory::createProductsGridContentElement( $locale->getCode(), $this->getColumnValue(self::PRODUCTS_COLUMN, $row), ); diff --git a/src/Importer/Legacy/LegacyPageImporter.php b/src/Importer/Legacy/LegacyPageImporter.php index 0bdd5e595..cb3b3a16d 100644 --- a/src/Importer/Legacy/LegacyPageImporter.php +++ b/src/Importer/Legacy/LegacyPageImporter.php @@ -6,7 +6,7 @@ use Doctrine\ORM\EntityManagerInterface; use Sylius\CmsPlugin\Entity\PageInterface; -use Sylius\CmsPlugin\Helper\ContentElementHelper; +use Sylius\CmsPlugin\Factory\ContentElementFactory; use Sylius\CmsPlugin\Importer\AbstractImporter; use Sylius\CmsPlugin\Resolver\Importer\ImporterChannelsResolverInterface; use Sylius\CmsPlugin\Resolver\Importer\ImporterCollectionsResolverInterface; @@ -60,7 +60,7 @@ public function import(array $row): void $page->setTeaserTitle($this->getTranslatableColumnValue(self::NAME_WHEN_LINKED_COLUMN, $locale, $row)); $page->setTeaserContent($this->getTranslatableColumnValue(self::DESCRIPTION_WHEN_LINKED_COLUMN, $locale, $row)); - $heading = ContentElementHelper::createHeadingContentElement( + $heading = ContentElementFactory::createHeadingContentElement( $locale, 'h2', $this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row), @@ -70,7 +70,7 @@ public function import(array $row): void $page->addContentElement($heading); } - $singleMedia = ContentElementHelper::createSingleMediaContentElement( + $singleMedia = ContentElementFactory::createSingleMediaContentElement( $locale, $this->getTranslatableColumnValue(self::IMAGE_COLUMN, $locale, $row), ); @@ -79,7 +79,7 @@ public function import(array $row): void $page->addContentElement($singleMedia); } - $content = ContentElementHelper::createTextareaContentElement( + $content = ContentElementFactory::createTextareaContentElement( $locale, $this->getTranslatableColumnValue(self::CONTENT_COLUMN, $locale, $row), ); @@ -92,7 +92,7 @@ public function import(array $row): void $locales = $this->localeRepository->findAll(); /** @var LocaleInterface $locale */ foreach ($locales as $locale) { - $productsGrid = ContentElementHelper::createProductsGridContentElement( + $productsGrid = ContentElementFactory::createProductsGridContentElement( $locale->getCode(), $this->getColumnValue(self::PRODUCTS_COLUMN, $row), );