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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Selfdelve\Cms;
  4. use Shopware\Core\Checkout\Shipping\ShippingMethodDefinition;
  5. use Shopware\Core\Content\Category\CategoryDefinition;
  6. use Shopware\Core\Content\Product\ProductDefinition;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  10. use Shopware\Core\System\CustomField\CustomFieldTypes;
  11. use Shopware\Core\Framework\Uuid\Uuid;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\Context;
  15. class SelfdelveCms extends Plugin
  16. {
  17.     private const CUSTOM_FIELDS_SET_CATEGORY 'selfdelve_theme_category';
  18.     private const CUSTOM_FIELDS_SET_CUSTOMER 'selfdelve_theme_customer';
  19.     private const CUSTOM_FIELDS_SET_PRODUCT 'selfdelve_theme_product';
  20.     private const CUSTOM_FIELDS_SET_SHIPPING_METHOD 'selfdelve_theme_shippingmethod';
  21.     /**
  22.      * Installation tasks.
  23.      * @param InstallContext $installContext
  24.      */
  25.     public function install(InstallContext $installContext): void
  26.     {
  27.         $this->addCustomFieldSets($installContext->getContext());
  28.     }
  29.     /**
  30.      * Uninstall tasks.
  31.      * @param UninstallContext $uninstallContext
  32.      */
  33.     public function uninstall(UninstallContext $uninstallContext): void
  34.     {
  35.         parent::uninstall($uninstallContext);
  36.         if ($uninstallContext->keepUserData()) {
  37.             return;
  38.         }
  39.         $this->removeCustomFieldSets($uninstallContext->getContext());
  40.     }
  41.     /**
  42.      * Insert the custom field sets if not already defined.
  43.      * @param Context $context
  44.      */
  45.     private function addCustomFieldSets(Context $context): void
  46.     {
  47.         $customFieldSetRepo $this->container->get('custom_field_set.repository');
  48.         $criteria = (new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELDS_SET_CATEGORY));
  49.         if ($customFieldSetRepo->search($criteria$context)->count() === 0) {
  50.             $customFieldSetRepo->create([
  51.                 [
  52.                     'name' => self::CUSTOM_FIELDS_SET_CATEGORY,
  53.                     'position' => 100,
  54.                     'config' => [
  55.                         'label' => [
  56.                             'en-GB' => 'SelfDelve - Category',
  57.                             'de-DE' => 'SelfDelve - Kategorie'
  58.                         ]
  59.                     ],
  60.                     'customFields' => [
  61.                         // Alternative name on category listings
  62.                         [
  63.                             'name' => self::CUSTOM_FIELDS_SET_CATEGORY '_name_alternative',
  64.                             'type' => CustomFieldTypes::TEXT,
  65.                             'config' => [
  66.                                 'label' => [
  67.                                     'en-GB' => 'Alternative Name',
  68.                                     'de-DE' => 'Alternativer Name'
  69.                                 ],
  70.                                 'helpText' => [
  71.                                     'en-GB' => 'Alternative titles of category listings.',
  72.                                     'de-DE' => 'Alternativer Titel auf Kategorielisten.'
  73.                                 ],
  74.                                 'customFieldType' => 'text',
  75.                                 'customFieldPosition' => 10
  76.                             ]
  77.                         ],
  78.                         // Token for categories that are hidden in navigations
  79.                         [
  80.                             'name' => self::CUSTOM_FIELDS_SET_CATEGORY '_access_token',
  81.                             'type' => CustomFieldTypes::TEXT,
  82.                             'config' => [
  83.                                 'label' => [
  84.                                     'en-GB' => 'Access Key',
  85.                                     'de-DE' => 'Zugriffsschlüssel'
  86.                                 ],
  87.                                 'helpText' => [
  88.                                     'en-GB' => 'If a category is hidden in a navigation, this token can hide it completely.',
  89.                                     'de-DE' => 'Wenn eine Kategorie in einer Navigation versteckt ist, kann über diesen Schlüssel ein zusätzlicher Zugriffsschutz hergestellt werden.'
  90.                                 ],
  91.                                 'customFieldType' => 'text',
  92.                                 'customFieldPosition' => 20
  93.                             ]
  94.                         ]
  95.                     ],
  96.                     'relations' => [
  97.                         [
  98.                             'id' => Uuid::randomHex(),
  99.                             'entityName' => CategoryDefinition::ENTITY_NAME
  100.                         ]
  101.                     ]
  102.                 ]
  103.             ], $context);
  104.         }
  105.         $criteria = (new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELDS_SET_PRODUCT));
  106.         if ($customFieldSetRepo->search($criteria$context)->count() === 0) {
  107.             $customFieldSetRepo->create([
  108.                 [
  109.                     'name' => self::CUSTOM_FIELDS_SET_PRODUCT,
  110.                     'position' => 200,
  111.                     'config' => [
  112.                         'label' => [
  113.                             'en-GB' => 'SelfDelve - Products',
  114.                             'de-DE' => 'SelfDelve - Produkt'
  115.                         ]
  116.                     ],
  117.                     'customFields' => [
  118.                         // Can be used for top ratings
  119.                         [
  120.                             'name' => self::CUSTOM_FIELDS_SET_PRODUCT '_priority',
  121.                             'type' => CustomFieldTypes::INT,
  122.                             'config' => [
  123.                                 'label' => [
  124.                                     'en-GB' => 'Priority',
  125.                                     'de-DE' => 'Priorität'
  126.                                 ],
  127.                                 'helpText' => [
  128.                                     'en-GB' => 'Can be used for product orderings in groups.',
  129.                                     'de-DE' => 'Kann für Sortierung in Produktgruppen genutzt werden.'
  130.                                 ],
  131.                                 'customFieldType' => 'number',
  132.                                 'customFieldPosition' => 5,
  133.                                 'numberType' => 'int',
  134.                                 'step' => 1,
  135.                                 'min' => 0,
  136.                                 'max' => 10000
  137.                             ]
  138.                         ],
  139.                         // Custom tariffs number for documents
  140.                         [
  141.                             'name' => self::CUSTOM_FIELDS_SET_PRODUCT '_customs_tariff_number',
  142.                             'type' => CustomFieldTypes::TEXT,
  143.                             'config' => [
  144.                                 'label' => [
  145.                                     'en-GB' => 'Custom Tariffs Number',
  146.                                     'de-DE' => 'Zolltarifnummer'
  147.                                 ],
  148.                                 'helpText' => [
  149.                                     'en-GB' => 'Custom tariffs number for documents.',
  150.                                     'de-DE' => 'Zolltarifnummer für den Ausdruck auf Belegen.'
  151.                                 ],
  152.                                 'customFieldType' => 'text',
  153.                                 'customFieldPosition' => 10,
  154.                             ]
  155.                         ],
  156.                         // Highlight a product as custom product
  157.                         [
  158.                             'name' => self::CUSTOM_FIELDS_SET_PRODUCT '_is_custom',
  159.                             'type' => CustomFieldTypes::BOOL,
  160.                             'config' => [
  161.                                 'label' => [
  162.                                     'en-GB' => 'Custom Product',
  163.                                     'de-DE' => 'Sonderanfertigung'
  164.                                 ],
  165.                                 'helpText' => [
  166.                                     'en-GB' => 'Internal mark as custom product.',
  167.                                     'de-DE' => 'Interne Markierung von Sonderanfertigungen.'
  168.                                 ],
  169.                                 'customFieldType' => 'switch',
  170.                                 'componentName' => 'sw-field',
  171.                                 'customFieldPosition' => 20,
  172.                             ]
  173.                         ],
  174.                         // Provision channel
  175.                         [
  176.                             'name' => self::CUSTOM_FIELDS_SET_PRODUCT '_commission',
  177.                             'type' => CustomFieldTypes::SELECT,
  178.                             'config' => [
  179.                                 'label' => [
  180.                                     'en-GB' => 'Provision Channel',
  181.                                     'de-DE' => 'Provisionskanal'
  182.                                 ],
  183.                                 'helpText' => [
  184.                                     'en-GB' => 'Used for sales channel.',
  185.                                     'de-DE' => 'Provisionskanal.'
  186.                                 ],
  187.                                 'customFieldType' => 'select',
  188.                                 'customFieldPosition' => 30,
  189.                                 'componentName' => 'sw-single-select',
  190.                                 'options' => [
  191.                                     [
  192.                                         'value' => 'engel_design',
  193.                                         'label' => [
  194.                                             'en-GB' => 'Engel Design',
  195.                                             'de-DE' => 'Engel Design'
  196.                                         ],
  197.                                     ],
  198.                                     [
  199.                                         'value' => 'hoegl_borowski',
  200.                                         'label' => [
  201.                                             'en-GB' => 'Studio Högl Borowski',
  202.                                             'de-DE' => 'Studio Högl Borowski'
  203.                                         ],
  204.                                     ],
  205.                                     [
  206.                                         'value' => 'sexclusivitaeten',
  207.                                         'label' => [
  208.                                             'en-GB' => 'Sexclusivitäten',
  209.                                             'de-DE' => 'Sexclusivitäten'
  210.                                         ],
  211.                                     ]
  212.                                 ]
  213.                             ]
  214.                         ],
  215.                         // Product-side assignment to product group
  216.                         [
  217.                             'name' => self::CUSTOM_FIELDS_SET_PRODUCT '_dyngroup_manual',
  218.                             'type' => CustomFieldTypes::SELECT,
  219.                             'config' => [
  220.                                 'label' => [
  221.                                     'en-GB' => 'Manual Assignment to Product Group',
  222.                                     'de-DE' => 'Manuelle Zuordnung zu Produktgruppe'
  223.                                 ],
  224.                                 'helpText' => [
  225.                                     'en-GB' => 'Product-side assignment to product group.',
  226.                                     'de-DE' => 'Produktseitige Zuordnung zu einer Produktgruppe (Kriterium kann dort genutzt werden).'
  227.                                 ],
  228.                                 'customFieldType' => 'select',
  229.                                 'customFieldPosition' => 40,
  230.                                 'componentName' => 'sw-single-select',
  231.                                 'options' => [
  232.                                     [
  233.                                         'value' => 'thick',
  234.                                         'label' => [
  235.                                             'en-GB' => 'Thick toys',
  236.                                             'de-DE' => 'Sehr dicke Toys'
  237.                                         ],
  238.                                     ],
  239.                                     [
  240.                                         'value' => 'narrow',
  241.                                         'label' => [
  242.                                             'en-GB' => 'Narrow toys',
  243.                                             'de-DE' => 'Extra schlanke Toys'
  244.                                         ],
  245.                                     ],
  246.                                     [
  247.                                         'value' => 'harness',
  248.                                         'label' => [
  249.                                             'en-GB' => 'Harness toys',
  250.                                             'de-DE' => 'Harnesstaugliche Toys'
  251.                                         ],
  252.                                     ],
  253.                                     [
  254.                                         'value' => 'pimples',
  255.                                         'label' => [
  256.                                             'en-GB' => 'Pimples toys',
  257.                                             'de-DE' => 'Toys mit Noppen'
  258.                                         ],
  259.                                     ],
  260.                                     [
  261.                                         'value' => 'helices',
  262.                                         'label' => [
  263.                                             'en-GB' => 'Helices',
  264.                                             'de-DE' => 'Spiralstrukturen'
  265.                                         ],
  266.                                     ]
  267.                                 ]
  268.                             ]
  269.                         ],
  270.                         // Base body for referencing the configuration product
  271.                         [
  272.                             'name' => self::CUSTOM_FIELDS_SET_PRODUCT '_base_body',
  273.                             'type' => CustomFieldTypes::TEXT,
  274.                             'config' => [
  275.                                 'label' => [
  276.                                     'en-GB' => 'Base Body',
  277.                                     'de-DE' => 'Grundkörper'
  278.                                 ],
  279.                                 'helpText' => [
  280.                                     'en-GB' => 'Base body for referencing the configuration product.',
  281.                                     'de-DE' => 'Wenn dieses Feld ausgefüllt ist, wird auf das Konfigurationsformular verlinkt und das Feld Grundkörper mit diesem Wert gesetzt.'
  282.                                 ],
  283.                                 'customFieldType' => 'text',
  284.                                 'customFieldPosition' => 50,
  285.                             ]
  286.                         ],
  287.                         // Product for canonical entry
  288.                         [
  289.                             'name' => self::CUSTOM_FIELDS_SET_PRODUCT '_canonical_product',
  290.                             'type' => CustomFieldTypes::SELECT,
  291.                             'config' => [
  292.                                 'label' => [
  293.                                     'en-GB' => 'Product for canonical entry',
  294.                                     'de-DE' => 'Produkt für Canonical-Eintrag'
  295.                                 ],
  296.                                 'helpText' => [
  297.                                     'en-GB' => 'Product for canonical entry in meta data.',
  298.                                     'de-DE' => 'Dieses Produkt wird für den Canonical-Eintrag in den Meta-Angaben genutzt.'
  299.                                 ],
  300.                                 'customFieldType' => 'entity',
  301.                                 'customFieldPosition' => 60,
  302.                                 'componentName' => 'sw-entity-single-select',
  303.                                 'entity' => 'product'
  304.                             ]
  305.                         ],
  306.                         // Flag a product as configuration with special templating
  307.                         [
  308.                             'name' => self::CUSTOM_FIELDS_SET_PRODUCT '_is_configurator',
  309.                             'type' => CustomFieldTypes::BOOL,
  310.                             'config' => [
  311.                                 'label' => [
  312.                                     'en-GB' => 'Configurator',
  313.                                     'de-DE' => 'Konfigurator'
  314.                                 ],
  315.                                 'helpText' => [
  316.                                     'en-GB' => 'Product represents a configuration.',
  317.                                     'de-DE' => 'Artikel repräsentiert eine Konfiguration.'
  318.                                 ],
  319.                                 'customFieldType' => 'switch',
  320.                                 'componentName' => 'sw-field',
  321.                                 'customFieldPosition' => 70,
  322.                             ]
  323.                         ],
  324.                         // Configuration header
  325.                         [
  326.                             'name' => self::CUSTOM_FIELDS_SET_PRODUCT '_configuration_listing_header',
  327.                             'type' => CustomFieldTypes::TEXT,
  328.                             'config' => [
  329.                                 'label' => [
  330.                                     'en-GB' => 'Headline of the configurator',
  331.                                     'de-DE' => 'Titel des Konfigurators'
  332.                                 ],
  333.                                 'helpText' => [
  334.                                     'en-GB' => 'Headline in listings.',
  335.                                     'de-DE' => 'Titel des Konfigurator-Banners in Kategorielisten.'
  336.                                 ],
  337.                                 'customFieldType' => 'text',
  338.                                 'customFieldPosition' => 80,
  339.                             ]
  340.                         ],
  341.                         // Configuration text
  342.                         [
  343.                             'name' => self::CUSTOM_FIELDS_SET_PRODUCT '_configuration_listing_text',
  344.                             'type' => CustomFieldTypes::TEXT,
  345.                             'config' => [
  346.                                 'label' => [
  347.                                     'en-GB' => 'Teaser text of the configurator',
  348.                                     'de-DE' => 'Teaser-Text des Konfigurators'
  349.                                 ],
  350.                                 'helpText' => [
  351.                                     'en-GB' => 'Teaser text in listings.',
  352.                                     'de-DE' => 'Teaser-Text des Konfigurator-Banners in Kategorielisten.'
  353.                                 ],
  354.                                 'customFieldType' => 'text',
  355.                                 'customFieldPosition' => 90,
  356.                             ]
  357.                         ],
  358.                         // Configuration button text
  359.                         [
  360.                             'name' => self::CUSTOM_FIELDS_SET_PRODUCT '_configuration_listing_linktext',
  361.                             'type' => CustomFieldTypes::TEXT,
  362.                             'config' => [
  363.                                 'label' => [
  364.                                     'en-GB' => 'Button text of the configurator',
  365.                                     'de-DE' => 'Button-Text des Konfigurators'
  366.                                 ],
  367.                                 'helpText' => [
  368.                                     'en-GB' => 'Button text in listings.',
  369.                                     'de-DE' => 'Button-Text des Konfigurator-Banners in Kategorielisten.'
  370.                                 ],
  371.                                 'customFieldType' => 'text',
  372.                                 'customFieldPosition' => 100,
  373.                             ]
  374.                         ],
  375.                     ],
  376.                     'relations' => [
  377.                         [
  378.                             'id' => Uuid::randomHex(),
  379.                             'entityName' => ProductDefinition::ENTITY_NAME
  380.                         ]
  381.                     ]
  382.                 ]
  383.             ], $context );
  384.         }
  385.         $criteria = (new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELDS_SET_SHIPPING_METHOD));
  386.         if ($customFieldSetRepo->search($criteria$context)->count() === 0) {
  387.             $customFieldSetRepo->create([
  388.                 [
  389.                     'name' => self::CUSTOM_FIELDS_SET_SHIPPING_METHOD,
  390.                     'position' => 300,
  391.                     'config' => [
  392.                         'label' => [
  393.                             'en-GB' => 'SelfDelve - Shipping Methods',
  394.                             'de-DE' => 'SelfDelve - Versandarten'
  395.                         ]
  396.                     ],
  397.                     'customFields' => [
  398.                         // Force phone number on method selection
  399.                         [
  400.                             'name' => self::CUSTOM_FIELDS_SET_SHIPPING_METHOD '_mandatory_phonenumber',
  401.                             'type' => CustomFieldTypes::BOOL,
  402.                             'config' => [
  403.                                 'label' => [
  404.                                     'en-GB' => 'Phone number is mandatory',
  405.                                     'de-DE' => 'Telefonnummer muss angegeben werden'
  406.                                 ],
  407.                                 'customFieldType' => 'switch',
  408.                                 'componentName' => 'sw-field',
  409.                                 'customFieldPosition' => 0
  410.                             ]
  411.                         ]
  412.                     ],
  413.                     'relations' => [
  414.                         [
  415.                             'id' => Uuid::randomHex(),
  416.                             'entityName' => ShippingMethodDefinition::ENTITY_NAME
  417.                         ]
  418.                     ]
  419.                 ]
  420.             ], $context);
  421.         }
  422.     }
  423.     /**
  424.      * Remove the custom field sets.
  425.      * @param Context $context
  426.      */
  427.     private function removeCustomFieldSets(Context $context): void
  428.     {
  429.         $customFieldSetRepo $this->container->get('custom_field_set.repository');
  430.         $setNames = [
  431.             self::CUSTOM_FIELDS_SET_CATEGORY,
  432.             self::CUSTOM_FIELDS_SET_CUSTOMER,
  433.             self::CUSTOM_FIELDS_SET_PRODUCT
  434.         ];
  435.         foreach ($setNames as $setName) {
  436.             $id $customFieldSetRepo->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$setName)), $context)->firstId();
  437.             if ($id) {
  438.                 $customFieldSetRepo->delete([
  439.                     ['id' => $id]
  440.                 ], $context);
  441.             }
  442.         }
  443.     }
  444. }