Skip to content

Commit

Permalink
Fix missing translations and missing file code (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
em411 authored Aug 4, 2021
1 parent eec2e93 commit b49cb9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
23 changes: 21 additions & 2 deletions src/Importer/PageImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -110,9 +111,10 @@ public function import(array $row): void
$page->setDescriptionWhenLinked($this->getTranslatableColumnValue(self::DESCRIPTION_WHEN_LINKED_COLUMN, $locale, $row));

$url = $this->getTranslatableColumnValue(self::IMAGE_COLUMN, $locale, $row);
$imageCode = $this->getTranslatableColumnValue(self::IMAGE_CODE_COLUMN, $locale, $row);

if (null !== $url) {
$this->resolveImage($page, $url ?? '', $locale);
$this->resolveImage($page, $url ?? '', $locale, $imageCode);
}
}

Expand All @@ -131,13 +133,15 @@ public function getResourceCode(): string
return 'page';
}

private function resolveImage(PageInterface $page, string $url, string $locale): void
private function resolveImage(PageInterface $page, string $url, string $locale, string $imageCode): void
{
$downloadedImage = $this->imageDownloader->download($url);

/** @var MediaInterface $image */
$image = $this->mediaFactory->createNew();
$image->setFile($downloadedImage);
$image->setType($this->getFileType($downloadedImage));
$image->setCode($imageCode);

/** @var PageTranslationInterface $pageTranslation */
$pageTranslation = $page->getTranslation($locale);
Expand All @@ -147,12 +151,27 @@ private function resolveImage(PageInterface $page, string $url, string $locale):
$this->entityManager->persist($image);
}

private function getFileType(File $file): string
{
switch ($file->getExtension()) {
case 'png':
case 'jpg':
case 'jpeg':
case 'gif':
return MediaInterface::IMAGE_TYPE;
case 'mp4':
return MediaInterface::VIDEO_TYPE;
}
return MediaInterface::FILE_TYPE;
}

private function getTranslatableColumns(): array
{
return [
self::SLUG_COLUMN,
self::NAME_COLUMN,
self::IMAGE_COLUMN,
self::IMAGE_CODE_COLUMN,
self::META_KEYWORDS_COLUMN,
self::META_DESCRIPTION_COLUMN,
self::CONTENT_COLUMN,
Expand Down
10 changes: 6 additions & 4 deletions src/Importer/PageImporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ interface PageImporterInterface extends ImporterInterface

public const IMAGE_COLUMN = 'image__locale__';

public const META_KEYWORDS_COLUMN = 'meta_keywords__locale__';
public const IMAGE_CODE_COLUMN = 'imagecode__locale__';

public const META_DESCRIPTION_COLUMN = 'meta_description__locale__';
public const META_KEYWORDS_COLUMN = 'metakeywords__locale__';

public const META_DESCRIPTION_COLUMN = 'metadescription__locale__';

public const CONTENT_COLUMN = 'content__locale__';

public const BREADCRUMB_COLUMN = 'breadcrumb__locale__';

public const NAME_WHEN_LINKED_COLUMN = 'name_when_linked__locale__';
public const NAME_WHEN_LINKED_COLUMN = 'namewhenlinked__locale__';

public const DESCRIPTION_WHEN_LINKED_COLUMN = 'description_when_linked__locale__';
public const DESCRIPTION_WHEN_LINKED_COLUMN = 'descriptionwhenlinked__locale__';
}

0 comments on commit b49cb9c

Please sign in to comment.