<?php declare(strict_types=1);
namespace Nxs\NxsAdditionalProductPicture;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexerRegistry;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class NxsAdditionalProductPicture extends Plugin
{
/**
* @param ActivateContext $activateContext
* @throws \Exception
*/
public function activate(ActivateContext $activateContext): void
{
$indexerMessageSender = $this->container->get(EntityIndexerRegistry::class);
$indexerMessageSender->sendIndexingMessage(['product.indexer']);
}
/**
* @param Plugin\Context\InstallContext $installContext
* @throws InconsistentCriteriaIdsException
*/
public function install(Plugin\Context\InstallContext $installContext): void
{
parent::install($installContext);
$criteria = new Criteria();
$criteria->setTerm('nxs_attributes');
$customSet = $this->container->get('custom_field_set.repository')->search($criteria, $installContext->getContext());
if ($customSet->getTotal() === 0) {
$this->container->get('custom_field_set_relation.repository')->create($this->getFields(), $installContext->getContext());
} else {
$this->container->get('custom_field_set_relation.repository')->create($this->getFields(), $installContext->getContext());
}
}
/**
* @return array
*/
private function getFields(): array
{
return [[
'id' => Uuid::randomHex(),
'entityName' => 'product',
'customFieldSet' => [
'name' => 'nxs_attributes',
'config' => [
'label' => [
'de-DE' => 'NXS Attribute',
'en-GB' => 'NXS Attributes'
],
'translated' => true
],
'customFields' => [
[
'name' => 'nxs_additional_product_picture',
'type' => CustomFieldTypes::SELECT,
'config' => [
'type' => 'media',
'label' => [
'de-DE' => 'Zusätzliches Bild',
'en-GB' => 'Additional picture'
],
'placeholder' => [
'de-DE' => '0',
'en-GB' => '0'
],
'componentName' => 'sw-field',
'customFieldType' => 'media',
'customFieldPosition' => 1
]
]
]
]]
];
}
}