Files
2026-09-15 10:35:56 +08:00

76 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Plugin\Adminhtml;
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
use Magento\Cms\Model\Block\DataProvider;
use Magento\Framework\App\RequestInterface;
class CmsBlockDataProvider
{
private const FIELDS = [
'title' => 'general',
'content' => 'general',
];
public function __construct(
private readonly RequestInterface $request,
private readonly CmsTranslationRepository $translationRepository
) {
}
public function afterGetData(DataProvider $subject, array $result): array
{
$blockId = (int)$this->request->getParam('block_id');
$storeId = (int)$this->request->getParam('store');
if ($blockId <= 0 || !isset($result[$blockId])) {
return $result;
}
$result[$blockId]['translation_store_id'] = $storeId;
if ($storeId <= 0) {
return $result;
}
$translations = $this->getTranslations($blockId, $storeId);
foreach (array_keys(self::FIELDS) as $field) {
if (array_key_exists($field, $translations)) {
$result[$blockId][$field] = $translations[$field];
}
}
return $result;
}
public function afterGetMeta(DataProvider $subject, array $result): array
{
$blockId = (int)$this->request->getParam('block_id');
$storeId = (int)$this->request->getParam('store');
if ($blockId <= 0 || $storeId <= 0) {
return $result;
}
$translations = $this->getTranslations($blockId, $storeId);
foreach (self::FIELDS 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 $blockId, int $storeId): array
{
return $this->translationRepository->getTranslations(
$blockId,
$storeId,
CmsTranslationRepository::ENTITY_BLOCK,
array_keys(self::FIELDS)
);
}
}