80 lines
2.5 KiB
PHP
80 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\MultiLocaleTranslation\Plugin\Adminhtml;
|
|
|
|
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
|
|
use Magento\Cms\Model\Page\DataProvider;
|
|
use Magento\Framework\App\RequestInterface;
|
|
|
|
class CmsPageDataProvider
|
|
{
|
|
private const FIELDSETS = [
|
|
'title' => 'general',
|
|
'content_heading' => 'content',
|
|
'content' => 'content',
|
|
'meta_title' => 'search_engine_optimisation',
|
|
'meta_keywords' => 'search_engine_optimisation',
|
|
'meta_description' => 'search_engine_optimisation',
|
|
];
|
|
|
|
public function __construct(
|
|
private readonly RequestInterface $request,
|
|
private readonly CmsTranslationRepository $translationRepository
|
|
) {
|
|
}
|
|
|
|
public function afterGetData(DataProvider $subject, array $result): array
|
|
{
|
|
$pageId = (int)$this->request->getParam('page_id');
|
|
$storeId = (int)$this->request->getParam('store');
|
|
if ($pageId <= 0 || !isset($result[$pageId])) {
|
|
return $result;
|
|
}
|
|
|
|
$result[$pageId]['translation_store_id'] = $storeId;
|
|
if ($storeId <= 0) {
|
|
return $result;
|
|
}
|
|
|
|
$translations = $this->getTranslations($pageId, $storeId);
|
|
foreach (array_keys(self::FIELDSETS) as $field) {
|
|
if (array_key_exists($field, $translations)) {
|
|
$result[$pageId][$field] = $translations[$field];
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function afterGetMeta(DataProvider $subject, array $result): array
|
|
{
|
|
$pageId = (int)$this->request->getParam('page_id');
|
|
$storeId = (int)$this->request->getParam('store');
|
|
if ($pageId <= 0 || $storeId <= 0) {
|
|
return $result;
|
|
}
|
|
|
|
$translations = $this->getTranslations($pageId, $storeId);
|
|
foreach (self::FIELDSETS as $field => $fieldset) {
|
|
$config = &$result[$fieldset]['children'][$field]['arguments']['data']['config'];
|
|
$config['service'] = ['template' => 'ui/form/element/helper/service'];
|
|
$config['disabled'] = !array_key_exists($field, $translations);
|
|
unset($config);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
private function getTranslations(int $pageId, int $storeId): array
|
|
{
|
|
return $this->translationRepository->getTranslations(
|
|
$pageId,
|
|
$storeId,
|
|
CmsTranslationRepository::ENTITY_PAGE,
|
|
array_keys(self::FIELDSETS)
|
|
);
|
|
}
|
|
}
|