init
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Translation\Model\Product;
|
||||
|
||||
use Esun\Translation\Model\Config;
|
||||
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
|
||||
|
||||
class TranslatableFieldProvider
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Config $config,
|
||||
private readonly ProductRepositoryInterface $productRepository,
|
||||
private readonly ProductAttributeRepositoryInterface $attributeRepository
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $targetStoreIds
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function getFields(int $productId, array $targetStoreIds = []): array
|
||||
{
|
||||
$sourceProduct = $this->productRepository->getById($productId, false, $this->config->getSourceStoreId());
|
||||
$targetProduct = $targetStoreIds !== []
|
||||
? $this->productRepository->getById($productId, false, (int)reset($targetStoreIds))
|
||||
: null;
|
||||
$fields = [];
|
||||
|
||||
foreach ($this->getAttributes() as $attribute) {
|
||||
$code = (string)$attribute->getAttributeCode();
|
||||
$sourceValue = (string)($sourceProduct->getData($code) ?? '');
|
||||
$targetValue = $targetProduct ? (string)($targetProduct->getData($code) ?? '') : '';
|
||||
|
||||
$fields[] = [
|
||||
'code' => $code,
|
||||
'label' => (string)$attribute->getDefaultFrontendLabel() ?: $code,
|
||||
'source_value' => $sourceValue,
|
||||
'target_value' => $targetValue,
|
||||
'is_html' => $this->isHtmlAttribute($attribute),
|
||||
];
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Attribute[]
|
||||
*/
|
||||
public function getAttributes(): array
|
||||
{
|
||||
$attributes = [];
|
||||
foreach ($this->config->getTranslatableAttributeCodes() as $code) {
|
||||
try {
|
||||
$attribute = $this->attributeRepository->get($code);
|
||||
} catch (\Throwable) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$attribute instanceof Attribute) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!in_array((string)$attribute->getFrontendInput(), ['text', 'textarea'], true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($attribute->isScopeGlobal()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attributes[$code] = $attribute;
|
||||
}
|
||||
|
||||
return array_values($attributes);
|
||||
}
|
||||
|
||||
public function isAttributeAllowed(string $code): bool
|
||||
{
|
||||
foreach ($this->getAttributes() as $attribute) {
|
||||
if ((string)$attribute->getAttributeCode() === $code) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isHtmlAttributeCode(string $code): bool
|
||||
{
|
||||
foreach ($this->getAttributes() as $attribute) {
|
||||
if ((string)$attribute->getAttributeCode() === $code) {
|
||||
return $this->isHtmlAttribute($attribute);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function isHtmlAttribute(Attribute $attribute): bool
|
||||
{
|
||||
return (bool)$attribute->getIsWysiwygEnabled()
|
||||
|| in_array((string)$attribute->getAttributeCode(), ['description', 'short_description'], true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user