custom/plugins/Tarador/src/Tarador.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Tarador;
  3. use Shopware\Core\Framework\Plugin;
  4. use Shopware\Storefront\Framework\ThemeInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  9. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  10. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  14. use Shopware\Core\System\CustomField\CustomFieldTypes;
  15. class Tarador extends Plugin implements ThemeInterface
  16. {
  17.     public function install(InstallContext $installContext): void
  18.     {
  19.         $this->createCustomFields($installContext->getContext());
  20.         parent::install($installContext); // TODO: Change the autogenerated stub
  21.     }
  22.     public function uninstall(UninstallContext $uninstallContext): void
  23.     {
  24.         if ($uninstallContext->keepUserData()) {
  25.             parent::uninstall($uninstallContext);
  26.             return;
  27.         }
  28.         $this->removeCustomFields($uninstallContext->getContext());
  29.         parent::uninstall($uninstallContext);
  30.     }
  31.     public function activate(ActivateContext $activateContext): void
  32.     {
  33.         parent::activate($activateContext); // TODO: Change the autogenerated stub
  34.     }
  35.     public function deactivate(DeactivateContext $deactivateContext): void
  36.     {
  37.         parent::deactivate($deactivateContext); // TODO: Change the autogenerated stub
  38.     }
  39.     public function update(UpdateContext $updateContext): void
  40.     {
  41.         parent::update($updateContext); // TODO: Change the autogenerated stub
  42.     }
  43.     private function createCustomFields($context){
  44.         $customFields = [
  45.             [
  46.                 'name' => 'custom_property_group',
  47.                 'active' => true,
  48.                 'config' => [
  49.                     'label' => [
  50.                         'en-GB' => 'Property group custom fields',
  51.                         'de-DE' => 'Property group custom fields'
  52.                     ],
  53.                 ],
  54.                 'customFields' => [
  55.                     [
  56.                         'name' => 'custom_property_group_special',
  57.                         'type' => CustomFieldTypes::SWITCH,
  58.                         'config' => [
  59.                             'label' => [
  60.                                 'en-GB' => 'Special property group',
  61.                                 'de-DE' => 'Special property group',
  62.                             ],
  63.                             'customFieldType' => CustomFieldTypes::SWITCH,
  64.                         ]
  65.                     ],
  66.                     [
  67.                         'name' => 'custom_property_group_slider',
  68.                         'type' => CustomFieldTypes::SWITCH,
  69.                         'config' => [
  70.                             'label' => [
  71.                                 'en-GB' => 'Slider property group',
  72.                                 'de-DE' => 'Slider property group',
  73.                             ],
  74.                             'customFieldType' => CustomFieldTypes::SWITCH,
  75.                         ]
  76.                     ],
  77.                     [
  78.                         'name' => 'custom_property_group_feed',
  79.                         'type' => CustomFieldTypes::SWITCH,
  80.                         'config' => [
  81.                             'label' => [
  82.                                 'en-GB' => 'Feed recommendations group',
  83.                                 'de-DE' => 'Feed recommendations group',
  84.                             ],
  85.                             'customFieldType' => CustomFieldTypes::SWITCH,
  86.                         ]
  87.                     ],
  88.                     [
  89.                         'name' => 'custom_property_group_image',
  90.                         'type' => 'text',
  91.                         'config' => [
  92.                             'label' => [
  93.                                 'en-GB' => 'Property group image',
  94.                                 'de-DE' => 'Property group image',
  95.                             ],
  96.                             'customFieldType' => CustomFieldTypes::MEDIA,
  97.                             'componentName' => 'sw-media-field'
  98.                         ]
  99.                     ],
  100.                 ],
  101.                 'relations' => [
  102.                     ['entityName' => 'property_group']
  103.                 ]
  104.             ],
  105.             [
  106.                 'name' => 'custom_product_field',
  107.                 'active' => true,
  108.                 'config' => [
  109.                     'label' => [
  110.                         'en-GB' => 'Product custom fields',
  111.                         'de-DE' => 'Produktbezogene Zusatzfelder'
  112.                     ]
  113.                 ],
  114.                 'customFields' => [
  115.                     [
  116.                         'name' => 'box_description',
  117.                         'type' => CustomFieldTypes::TEXT,
  118.                         'position' => 2,
  119.                         'config' => [
  120.                             'label' => [
  121.                                 'en-GB' => 'Product box description',
  122.                                 'de-DE' => 'Claim',
  123.                             ],
  124.                         ]
  125.                     ],
  126.                     [
  127.                         'name' => 'additional_description',
  128.                         'type' => CustomFieldTypes::HTML,
  129.                         'position' => 10,
  130.                         'config' => [
  131.                             'label' => [
  132.                                 'en-GB' => 'Description on the right',
  133.                                 'de-DE' => 'Beschreibung auf der rechten Seite',
  134.                             ],
  135.                             'customFieldType' => CustomFieldTypes::HTML,
  136.                             'componentName' => 'sw-text-editor'
  137.                         ]
  138.                     ],
  139.                     [
  140.                         'name' => 'nutritional_additives',
  141.                         'type' => CustomFieldTypes::HTML,
  142.                         'position' => 5,
  143.                         'config' => [
  144.                             'label' => [
  145.                                 'en-GB' => 'Nutritional additives',
  146.                                 'de-DE' => 'Ernährungsphysiologische Zusatzstoffe',
  147.                             ],
  148.                             'customFieldType' => CustomFieldTypes::HTML,
  149.                             'componentName' => 'sw-text-editor'
  150.                         ]
  151.                     ],
  152.                     [
  153.                         'name' => 'analytical_components',
  154.                         'type' => CustomFieldTypes::HTML,
  155.                         'position' => 7,
  156.                         'config' => [
  157.                             'label' => [
  158.                                 'en-GB' => 'Analytical components',
  159.                                 'de-DE' => 'Unsere guten Zutaten',
  160.                             ],
  161.                             'customFieldType' => CustomFieldTypes::HTML,
  162.                             'componentName' => 'sw-text-editor'
  163.                         ]
  164.                     ],
  165.                     [
  166.                         'name' => 'feeding_recommendations_top',
  167.                         'type' => CustomFieldTypes::TEXT,
  168.                         'position' => 8,
  169.                         'config' => [
  170.                             'label' => [
  171.                                 'en-GB' => 'Feeding recommendations top text',
  172.                                 'de-DE' => 'Fütterungsempfehlungen oben',
  173.                             ],
  174.                         ]
  175.                     ],
  176.                     [
  177.                         'name' => 'feeding_recommendations_left',
  178.                         'type' => CustomFieldTypes::TEXT,
  179.                         'position' => 9,
  180.                         'config' => [
  181.                             'label' => [
  182.                                 'en-GB' => 'Feeding recommendations left text',
  183.                                 'de-DE' => 'Fütterungsempfehlungen links',
  184.                             ],
  185.                         ]
  186.                     ],
  187.                     [
  188.                         'name' => 'feeding_recommendations_right',
  189.                         'type' => CustomFieldTypes::TEXT,
  190.                         'position' => 10,
  191.                         'config' => [
  192.                             'label' => [
  193.                                 'en-GB' => 'Feeding recommendations right text',
  194.                                 'de-DE' => 'Fütterungsempfehlungen rechts',
  195.                             ],
  196.                         ]
  197.                     ],
  198.                     [
  199.                         'name' => 'ingredients_notes',
  200.                         'type' => CustomFieldTypes::HTML,
  201.                         'position' => 6,
  202.                         'config' => [
  203.                             'label' => [
  204.                                 'en-GB' => 'Ingredients Notes',
  205.                                 'de-DE' => 'Hinweise',
  206.                             ],
  207.                             'customFieldType' => CustomFieldTypes::HTML,
  208.                             'componentName' => 'sw-text-editor'
  209.                         ]
  210.                     ],
  211.                     [
  212.                         'name' => 'age',
  213.                         'type' => CustomFieldTypes::TEXT,
  214.                         'position' => 12,
  215.                         'config' => [
  216.                             'label' => [
  217.                                 'en-GB' => 'Age',
  218.                                 'de-DE' => 'Alter',
  219.                             ],
  220.                         ]
  221.                     ],
  222.                     [
  223.                         'name' => 'size',
  224.                         'type' => CustomFieldTypes::TEXT,
  225.                         'position' => 11,
  226.                         'config' => [
  227.                             'label' => [
  228.                                 'en-GB' => 'Size',
  229.                                 'de-DE' => 'Größe',
  230.                             ],
  231.                         ]
  232.                     ],
  233.                     [
  234.                         'name' => 'activity',
  235.                         'type' => CustomFieldTypes::TEXT,
  236.                         'position' => 20,
  237.                         'config' => [
  238.                             'label' => [
  239.                                 'en-GB' => 'Activity',
  240.                                 'de-DE' => 'Aktivität',
  241.                             ],
  242.                         ]
  243.                     ]
  244.                 ],
  245.                 'relations' => [
  246.                     ['entityName' => 'product']
  247.                 ]
  248.             ] 
  249.         ];
  250.         $repo $this->container->get('custom_field_set.repository');
  251.         //check if entries exist to prevent re-installation error
  252.         $propertyCustomFieldSetEntry false;
  253.         $propertyCustomFieldSetEntry $repo->search((new Criteria())
  254.             ->addFilter(
  255.                 new EqualsFilter('name''custom_property_group'),
  256.             ), $context)->first();
  257.         if(!$propertyCustomFieldSetEntry) {
  258.             foreach ($customFields as $customFieldSet) {
  259.                 $repo->upsert([$customFieldSet], $context);
  260.             }
  261.         }
  262.         $productCustomFieldSetEntry false;
  263.         $productCustomFieldSetEntry $repo->search((new Criteria())
  264.             ->addFilter(
  265.                 new EqualsFilter('name''custom_product_field'),
  266.             ), $context)->first();
  267.         if(!$productCustomFieldSetEntry) {
  268.             foreach ($customFields as $customFieldSet) {
  269.                 $repo->upsert([$customFieldSet], $context);
  270.             }
  271.         }
  272.     }
  273.     private function removeCustomFields($context){
  274.         $cfsRepo $this->container->get('custom_field_set.repository');
  275.         $cfRepo $this->container->get('custom_field.repository');
  276.         $sRepo $this->container->get('snippet.repository');
  277.         //delete custom_field_set entry
  278.         $propertyCfsId $cfsRepo->search((new Criteria())
  279.                ->addFilter(
  280.                     new EqualsFilter('name''custom_property_group'),
  281.                 ), $context
  282.         )->first();
  283.         if ($propertyCfsId) {
  284.             $cfsRepo->delete([
  285.                 ['id' => $propertyCfsId->getId()]
  286.             ], $context);
  287.             //delete custom_field entries
  288.             $propertyCfsId $cfRepo->search((new Criteria())
  289.                  ->addFilter(
  290.                      new EqualsFilter('customFieldSetId'$propertyCfsId->getId())
  291.                  ), $context)->getIds();
  292.             $ids = [];
  293.             if ($propertyCfsId) {
  294.                 foreach ($propertyCfsId as $id) {
  295.                     $ids[] = ['id' => $id];
  296.                 }
  297.                 $cfRepo->delete($ids$context);
  298.             }
  299.         }
  300.         $productCfsId $cfsRepo->search((new Criteria())
  301.                ->addFilter(
  302.                     new EqualsFilter('name''custom_product_field'),
  303.                 ), $context
  304.         )->first();
  305.         if ($productCfsId) {
  306.             $cfsRepo->delete([
  307.                 ['id' => $productCfsId->getId()]
  308.             ], $context);
  309.             //delete custom_field entries
  310.             $productCfsId $cfRepo->search((new Criteria())
  311.                  ->addFilter(
  312.                      new EqualsFilter('customFieldSetId'$productCfsId->getId())
  313.                  ), $context)->getIds();
  314.             $ids = [];
  315.             if ($productCfsId) {
  316.                 foreach ($productCfsId as $id) {
  317.                     $ids[] = ['id' => $id];
  318.                 }
  319.                 $cfRepo->delete($ids$context);
  320.             }
  321.         }
  322.         // delete snippet entries
  323.         $sIds $sRepo->search((new Criteria())
  324.             ->addFilter(
  325.                 new MultiFilter(
  326.                     MultiFilter::CONNECTION_OR,
  327.                     [
  328.                         new ContainsFilter('translationKey''custom_property_group_special'),
  329.                         new ContainsFilter('translationKey''custom_property_group_slider'),
  330.                         new ContainsFilter('translationKey''custom_property_group_image'),
  331.                         new ContainsFilter('translationKey''custom_property_group_feed'),
  332.                         new ContainsFilter('translationKey''box_description'),
  333.                         new ContainsFilter('translationKey''additional_description'),
  334.                         new ContainsFilter('translationKey''feeding_recommendations_top'),
  335.                         new ContainsFilter('translationKey''feeding_recommendations_left'),
  336.                         new ContainsFilter('translationKey''feeding_recommendations_right'),
  337.                         new ContainsFilter('translationKey''nutritional_additives'),
  338.                         new ContainsFilter('translationKey''analytical_components'),
  339.                         new ContainsFilter('translationKey''ingredients_notes'),
  340.                         new ContainsFilter('translationKey''age'),
  341.                         new ContainsFilter('translationKey''size'),
  342.                         new ContainsFilter('translationKey''activity'),
  343.                     ]
  344.                 )
  345.             ), $context)->getIds();
  346.         $snippetIds = [];
  347.         if ($sIds) {
  348.             foreach ($sIds as $id) {
  349.                 $snippetIds[] = ['id' => $id];
  350.             }
  351.             $sRepo->delete($snippetIds$context);
  352.         }
  353.     }
  354. }