init
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Plugin\Cms;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
|
||||
use Magento\Cms\Model\Page;
|
||||
use Magento\Framework\App\State;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class ApplyStoreTranslationToPage
|
||||
{
|
||||
private const FIELDS = [
|
||||
'title',
|
||||
'content_heading',
|
||||
'content',
|
||||
'meta_title',
|
||||
'meta_keywords',
|
||||
'meta_description',
|
||||
'layout_update_xml',
|
||||
'custom_layout_update_xml',
|
||||
'page_layout',
|
||||
'custom_theme',
|
||||
'custom_root_template',
|
||||
'sort_order',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly CmsTranslationRepository $translationRepository,
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
private readonly State $appState
|
||||
) {
|
||||
}
|
||||
|
||||
public function afterLoad(Page $subject, Page $result): Page
|
||||
{
|
||||
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_PAGE,
|
||||
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