init
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Model\Entity;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
|
||||
use Esun\MultiLocaleTranslation\Model\Config\SkippedFields;
|
||||
use Esun\MultiLocaleTranslation\Model\TranslatableEntityInterface;
|
||||
use Magento\Cms\Model\Block;
|
||||
use Magento\Cms\Model\BlockFactory;
|
||||
|
||||
class CmsBlockEntity implements TranslatableEntityInterface
|
||||
{
|
||||
private const FIELDS = [
|
||||
'title' => ['label' => 'Block Title', 'is_html' => false],
|
||||
'content' => ['label' => 'Content', 'is_html' => true],
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly BlockFactory $blockFactory,
|
||||
private readonly CmsTranslationRepository $translationRepository,
|
||||
private readonly SkippedFields $skippedFields
|
||||
) {
|
||||
}
|
||||
|
||||
public function getFields(int $entityId, int $sourceStoreId, int $targetStoreId): array
|
||||
{
|
||||
$source = $this->loadBlock($entityId, $sourceStoreId);
|
||||
$targetValues = $this->translationRepository->getTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_BLOCK,
|
||||
array_keys(self::FIELDS)
|
||||
);
|
||||
|
||||
$fields = [];
|
||||
foreach (self::FIELDS as $field => $meta) {
|
||||
if ($this->skippedFields->isSkipped('cms_block', $field)) {
|
||||
continue;
|
||||
}
|
||||
$fields[] = [
|
||||
'code' => $field,
|
||||
'label' => $meta['label'],
|
||||
'source_value' => (string)($source->getData($field) ?? ''),
|
||||
'target_value' => (string)($targetValues[$field] ?? $source->getData($field) ?? ''),
|
||||
'is_html' => (bool)$meta['is_html'],
|
||||
'use_default' => !array_key_exists($field, $targetValues),
|
||||
];
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
public function apply(int $entityId, int $targetStoreId, array $fields, array $useDefault = []): void
|
||||
{
|
||||
$defaultFields = [];
|
||||
foreach (array_keys(array_filter($useDefault)) as $field) {
|
||||
if (isset(self::FIELDS[$field]) && !$this->skippedFields->isSkipped('cms_block', $field)) {
|
||||
$defaultFields[] = $field;
|
||||
}
|
||||
}
|
||||
$this->translationRepository->deleteTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_BLOCK,
|
||||
$defaultFields
|
||||
);
|
||||
|
||||
$fields = array_diff_key($fields, array_flip($defaultFields));
|
||||
$this->translationRepository->saveTranslations(
|
||||
$entityId,
|
||||
$targetStoreId,
|
||||
CmsTranslationRepository::ENTITY_BLOCK,
|
||||
$this->filterFields($fields)
|
||||
);
|
||||
}
|
||||
|
||||
private function loadBlock(int $entityId, int $storeId): Block
|
||||
{
|
||||
$block = $this->blockFactory->create();
|
||||
$block->setStoreId($storeId > 0 ? $storeId : 0)->load($entityId);
|
||||
return $block;
|
||||
}
|
||||
|
||||
private function filterFields(array $fields): array
|
||||
{
|
||||
$result = [];
|
||||
foreach (array_keys(self::FIELDS) as $field) {
|
||||
if ($this->skippedFields->isSkipped('cms_block', $field)
|
||||
|| !array_key_exists($field, $fields)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
$result[$field] = (string)$fields[$field];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user