<?php declare(strict_types=1);
namespace Compra\CustomFieldsSW6;
use Compra\FoundationSW6\Core\System\CustomFieldService;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class CompraCustomFieldsSW6 extends Plugin
{
public function build(ContainerBuilder $container): void
{
$container->setParameter('compra_custom_fields_s_w6.plugin_dir', $this->getPath());
parent::build($container);
}
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->addCustomFields($installContext->getContext());
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$this->addCustomFields($updateContext->getContext());
}
/**
* Function to create the custom fields for the plugin.
*
* @param Context $context
*/
public function addCustomFields(Context $context): void
{
$customFieldService = $this->container->get(CustomFieldService::class);
if (!$customFieldService) {
return;
}
try {
// product
$this->addProductCustomFields($customFieldService, $context);
} catch (\Exception $e) {}
}
/**
* @param CustomFieldService $customFieldService
* @param Context $context
* @throws \Exception
*/
private function addProductCustomFields(CustomFieldService $customFieldService, Context $context): void
{
$customFieldService->createOrUpdateCustomFieldSets([
[
'name' => 'compra_product_custom_fields',
'config' => [
'label' => [
'de-DE' => 'COMPRA Zusatzfelder für Produkte',
'en-GB' => 'COMPRA custom fields for products'
]
],
'relations' => [
[
'entityName' => 'product'
]
],
'customFields' => [
[
'name' => 'compra_product_variantentext',
'type' => 'text',
'config' => [
'label' => [
'de-DE' => 'Variantentext',
'en-GB' => 'Variant text'
],
'helpText' => [
'de-DE' => 'Inhalt des Dokuments "Variantentext" aus eEvolution',
'en-GB' => 'Content of the document "Variant text" from eEvolution'
],
'customFieldPosition' => 0
]
]
]
]], $context);
}
}