64 lines
1.6 KiB
PHP
64 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\MultiLocaleTranslation\Plugin\Cms;
|
|
|
|
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
|
|
use Magento\Cms\Model\Block;
|
|
use Magento\Framework\App\State;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Magento\Store\Model\StoreManagerInterface;
|
|
|
|
class ApplyStoreTranslationToBlock
|
|
{
|
|
private const FIELDS = [
|
|
'title',
|
|
'content'
|
|
];
|
|
|
|
public function __construct(
|
|
private readonly CmsTranslationRepository $translationRepository,
|
|
private readonly StoreManagerInterface $storeManager,
|
|
private readonly State $appState
|
|
) {
|
|
}
|
|
|
|
public function afterLoad(Block $subject, Block $result): Block
|
|
{
|
|
if (!$this->isFrontendArea()) {
|
|
return $result;
|
|
}
|
|
|
|
$storeId = (int)$this->storeManager->getStore()->getId();
|
|
if ($storeId <= 0 || !$subject->getId()) {
|
|
return $result;
|
|
}
|
|
|
|
$translations = $this->translationRepository->getTranslations(
|
|
(int)$subject->getId(),
|
|
$storeId,
|
|
CmsTranslationRepository::ENTITY_BLOCK,
|
|
self::FIELDS
|
|
);
|
|
|
|
foreach (self::FIELDS as $field) {
|
|
if (!array_key_exists($field, $translations)) {
|
|
continue;
|
|
}
|
|
$subject->setData($field, $translations[$field]);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
private function isFrontendArea(): bool
|
|
{
|
|
try {
|
|
return $this->appState->getAreaCode() === 'frontend';
|
|
} catch (LocalizedException) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|