101 lines
3.4 KiB
PHP
101 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\MultiLocaleTranslation\Observer\Adminhtml;
|
|
|
|
use Esun\MultiLocaleTranslation\Model\BlogPostFields;
|
|
use Esun\MultiLocaleTranslation\Model\CmsTranslationRepository;
|
|
use Esun\MultiLocaleTranslation\Model\Config\SkippedFields;
|
|
use Magento\Framework\App\RequestInterface;
|
|
use Magento\Framework\Event\Observer;
|
|
use Magento\Framework\Event\ObserverInterface;
|
|
use Magento\Framework\Registry;
|
|
use Mageplaza\Blog\Block\Adminhtml\Post\Edit\Tab\Post as PostTab;
|
|
|
|
class PrepareBlogPostForm implements ObserverInterface
|
|
{
|
|
private const SHARED_FIELDS = [
|
|
'url_key',
|
|
'author_id',
|
|
'enabled',
|
|
'store_ids',
|
|
'image',
|
|
'categories_ids',
|
|
'topics_ids',
|
|
'tags_ids',
|
|
'in_rss',
|
|
'allow_comment',
|
|
'publish_date',
|
|
'layout',
|
|
];
|
|
|
|
public function __construct(
|
|
private readonly RequestInterface $request,
|
|
private readonly Registry $registry,
|
|
private readonly CmsTranslationRepository $translationRepository,
|
|
private readonly SkippedFields $skippedFields
|
|
) {
|
|
}
|
|
|
|
public function execute(Observer $observer): void
|
|
{
|
|
$block = $observer->getData('block');
|
|
$post = $this->registry->registry('mageplaza_blog_post');
|
|
$storeId = max(0, (int)$this->request->getParam('store'));
|
|
if (!$block instanceof PostTab || !$post || !$post->getId() || $storeId <= 0) {
|
|
return;
|
|
}
|
|
|
|
$form = $block->getForm();
|
|
$translations = $this->translationRepository->getTranslations(
|
|
(int)$post->getId(),
|
|
$storeId,
|
|
CmsTranslationRepository::ENTITY_BLOG_POST,
|
|
BlogPostFields::codes()
|
|
);
|
|
$fieldset = $form->getElement('base_fieldset');
|
|
if ($fieldset && !$form->getElement('translation_store_id')) {
|
|
$fieldset->addField('translation_store_id', 'hidden', [
|
|
'name' => 'translation_store_id',
|
|
'value' => $storeId,
|
|
]);
|
|
}
|
|
|
|
foreach (BlogPostFields::codes() as $field) {
|
|
$element = $form->getElement($field);
|
|
if (!$element) {
|
|
continue;
|
|
}
|
|
if ($this->skippedFields->isSkipped('blog_post', $field)) {
|
|
$element->setDisabled(true);
|
|
$element->setData('after_element_html', (string)__('Skipped by translation configuration.'));
|
|
continue;
|
|
}
|
|
$hasTranslation = array_key_exists($field, $translations);
|
|
$element->setValue(
|
|
$hasTranslation
|
|
? $translations[$field]
|
|
: BlogPostFields::baseValue($field, $post->getData($field))
|
|
);
|
|
$element->setDisabled(!$hasTranslation);
|
|
$element->setData(
|
|
'after_element_html',
|
|
sprintf(
|
|
'<label class="esun-blog-use-default"><input type="checkbox" name="post[use_default][%1$s]" value="1" data-target="#post_%1$s"%2$s/> %3$s</label>',
|
|
$field,
|
|
$hasTranslation ? '' : ' checked="checked"',
|
|
__('Use Default Value')
|
|
)
|
|
);
|
|
}
|
|
|
|
foreach (self::SHARED_FIELDS as $field) {
|
|
$element = $form->getElement($field);
|
|
if ($element) {
|
|
$element->setDisabled(true);
|
|
}
|
|
}
|
|
}
|
|
}
|