custom/plugins/CompraCustomFieldsSW6/src/CompraCustomFieldsSW6.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Compra\CustomFieldsSW6;
  3. use Compra\FoundationSW6\Core\System\CustomFieldService;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\Plugin;
  6. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  7. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  8. use Symfony\Component\DependencyInjection\ContainerBuilder;
  9. class CompraCustomFieldsSW6 extends Plugin
  10. {
  11.     public function build(ContainerBuilder $container): void
  12.     {
  13.         $container->setParameter('compra_custom_fields_s_w6.plugin_dir'$this->getPath());
  14.         parent::build($container);
  15.     }
  16.     public function install(InstallContext $installContext): void
  17.     {
  18.         parent::install($installContext);
  19.         $this->addCustomFields($installContext->getContext());
  20.     }
  21.     public function update(UpdateContext $updateContext): void
  22.     {
  23.         parent::update($updateContext);
  24.         $this->addCustomFields($updateContext->getContext());
  25.     }
  26.     /**
  27.      * Function to create the custom fields for the plugin.
  28.      *
  29.      * @param Context $context
  30.      */
  31.     public function addCustomFields(Context $context): void
  32.     {
  33.         $customFieldService $this->container->get(CustomFieldService::class);
  34.         if (!$customFieldService) {
  35.             return;
  36.         }
  37.         try {
  38.             // product
  39.             $this->addProductCustomFields($customFieldService$context);
  40.         } catch (\Exception $e) {}
  41.     }
  42.     /**
  43.      * @param CustomFieldService $customFieldService
  44.      * @param Context $context
  45.      * @throws \Exception
  46.      */
  47.     private function addProductCustomFields(CustomFieldService $customFieldServiceContext $context): void
  48.     {
  49.         $customFieldService->createOrUpdateCustomFieldSets([
  50.             [
  51.                 'name' => 'compra_product_custom_fields',
  52.                 'config' => [
  53.                     'label' => [
  54.                         'de-DE' => 'COMPRA Zusatzfelder für Produkte',
  55.                         'en-GB' => 'COMPRA custom fields for products'
  56.                     ]
  57.                 ],
  58.                 'relations' => [
  59.                     [
  60.                         'entityName' => 'product'
  61.                     ]
  62.                 ],
  63.                 'customFields' => [
  64.                     [
  65.                         'name' => 'compra_product_variantentext',
  66.                         'type' => 'text',
  67.                         'config' => [
  68.                             'label' => [
  69.                                 'de-DE' => 'Variantentext',
  70.                                 'en-GB' => 'Variant text'
  71.                             ],
  72.                             'helpText' => [
  73.                                 'de-DE' => 'Inhalt des Dokuments "Variantentext" aus eEvolution',
  74.                                 'en-GB' => 'Content of the document "Variant text" from eEvolution'
  75.                             ],
  76.                             'customFieldPosition' => 0
  77.                         ]
  78.                     ]
  79.                 ]
  80.             ]], $context);
  81.     }
  82. }