init
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user