<?phpdeclare(strict_types=1);namespace Selfdelve\Cms;use Shopware\Core\Checkout\Shipping\ShippingMethodDefinition;use Shopware\Core\Content\Category\CategoryDefinition;use Shopware\Core\Content\Product\ProductDefinition;use Shopware\Core\Framework\Plugin;use Shopware\Core\Framework\Plugin\Context\InstallContext;use Shopware\Core\Framework\Plugin\Context\UninstallContext;use Shopware\Core\System\CustomField\CustomFieldTypes;use Shopware\Core\Framework\Uuid\Uuid;use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;use Shopware\Core\Framework\Context;class SelfdelveCms extends Plugin{ private const CUSTOM_FIELDS_SET_CATEGORY = 'selfdelve_theme_category'; private const CUSTOM_FIELDS_SET_CUSTOMER = 'selfdelve_theme_customer'; private const CUSTOM_FIELDS_SET_PRODUCT = 'selfdelve_theme_product'; private const CUSTOM_FIELDS_SET_SHIPPING_METHOD = 'selfdelve_theme_shippingmethod'; /** * Installation tasks. * @param InstallContext $installContext */ public function install(InstallContext $installContext): void { $this->addCustomFieldSets($installContext->getContext()); } /** * Uninstall tasks. * @param UninstallContext $uninstallContext */ public function uninstall(UninstallContext $uninstallContext): void { parent::uninstall($uninstallContext); if ($uninstallContext->keepUserData()) { return; } $this->removeCustomFieldSets($uninstallContext->getContext()); } /** * Insert the custom field sets if not already defined. * @param Context $context */ private function addCustomFieldSets(Context $context): void { $customFieldSetRepo = $this->container->get('custom_field_set.repository'); $criteria = (new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOM_FIELDS_SET_CATEGORY)); if ($customFieldSetRepo->search($criteria, $context)->count() === 0) { $customFieldSetRepo->create([ [ 'name' => self::CUSTOM_FIELDS_SET_CATEGORY, 'position' => 100, 'config' => [ 'label' => [ 'en-GB' => 'SelfDelve - Category', 'de-DE' => 'SelfDelve - Kategorie' ] ], 'customFields' => [ // Alternative name on category listings [ 'name' => self::CUSTOM_FIELDS_SET_CATEGORY . '_name_alternative', 'type' => CustomFieldTypes::TEXT, 'config' => [ 'label' => [ 'en-GB' => 'Alternative Name', 'de-DE' => 'Alternativer Name' ], 'helpText' => [ 'en-GB' => 'Alternative titles of category listings.', 'de-DE' => 'Alternativer Titel auf Kategorielisten.' ], 'customFieldType' => 'text', 'customFieldPosition' => 10 ] ], // Token for categories that are hidden in navigations [ 'name' => self::CUSTOM_FIELDS_SET_CATEGORY . '_access_token', 'type' => CustomFieldTypes::TEXT, 'config' => [ 'label' => [ 'en-GB' => 'Access Key', 'de-DE' => 'Zugriffsschlüssel' ], 'helpText' => [ 'en-GB' => 'If a category is hidden in a navigation, this token can hide it completely.', 'de-DE' => 'Wenn eine Kategorie in einer Navigation versteckt ist, kann über diesen Schlüssel ein zusätzlicher Zugriffsschutz hergestellt werden.' ], 'customFieldType' => 'text', 'customFieldPosition' => 20 ] ] ], 'relations' => [ [ 'id' => Uuid::randomHex(), 'entityName' => CategoryDefinition::ENTITY_NAME ] ] ] ], $context); } $criteria = (new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOM_FIELDS_SET_PRODUCT)); if ($customFieldSetRepo->search($criteria, $context)->count() === 0) { $customFieldSetRepo->create([ [ 'name' => self::CUSTOM_FIELDS_SET_PRODUCT, 'position' => 200, 'config' => [ 'label' => [ 'en-GB' => 'SelfDelve - Products', 'de-DE' => 'SelfDelve - Produkt' ] ], 'customFields' => [ // Can be used for top ratings [ 'name' => self::CUSTOM_FIELDS_SET_PRODUCT . '_priority', 'type' => CustomFieldTypes::INT, 'config' => [ 'label' => [ 'en-GB' => 'Priority', 'de-DE' => 'Priorität' ], 'helpText' => [ 'en-GB' => 'Can be used for product orderings in groups.', 'de-DE' => 'Kann für Sortierung in Produktgruppen genutzt werden.' ], 'customFieldType' => 'number', 'customFieldPosition' => 5, 'numberType' => 'int', 'step' => 1, 'min' => 0, 'max' => 10000 ] ], // Custom tariffs number for documents [ 'name' => self::CUSTOM_FIELDS_SET_PRODUCT . '_customs_tariff_number', 'type' => CustomFieldTypes::TEXT, 'config' => [ 'label' => [ 'en-GB' => 'Custom Tariffs Number', 'de-DE' => 'Zolltarifnummer' ], 'helpText' => [ 'en-GB' => 'Custom tariffs number for documents.', 'de-DE' => 'Zolltarifnummer für den Ausdruck auf Belegen.' ], 'customFieldType' => 'text', 'customFieldPosition' => 10, ] ], // Highlight a product as custom product [ 'name' => self::CUSTOM_FIELDS_SET_PRODUCT . '_is_custom', 'type' => CustomFieldTypes::BOOL, 'config' => [ 'label' => [ 'en-GB' => 'Custom Product', 'de-DE' => 'Sonderanfertigung' ], 'helpText' => [ 'en-GB' => 'Internal mark as custom product.', 'de-DE' => 'Interne Markierung von Sonderanfertigungen.' ], 'customFieldType' => 'switch', 'componentName' => 'sw-field', 'customFieldPosition' => 20, ] ], // Provision channel [ 'name' => self::CUSTOM_FIELDS_SET_PRODUCT . '_commission', 'type' => CustomFieldTypes::SELECT, 'config' => [ 'label' => [ 'en-GB' => 'Provision Channel', 'de-DE' => 'Provisionskanal' ], 'helpText' => [ 'en-GB' => 'Used for sales channel.', 'de-DE' => 'Provisionskanal.' ], 'customFieldType' => 'select', 'customFieldPosition' => 30, 'componentName' => 'sw-single-select', 'options' => [ [ 'value' => 'engel_design', 'label' => [ 'en-GB' => 'Engel Design', 'de-DE' => 'Engel Design' ], ], [ 'value' => 'hoegl_borowski', 'label' => [ 'en-GB' => 'Studio Högl Borowski', 'de-DE' => 'Studio Högl Borowski' ], ], [ 'value' => 'sexclusivitaeten', 'label' => [ 'en-GB' => 'Sexclusivitäten', 'de-DE' => 'Sexclusivitäten' ], ] ] ] ], // Product-side assignment to product group [ 'name' => self::CUSTOM_FIELDS_SET_PRODUCT . '_dyngroup_manual', 'type' => CustomFieldTypes::SELECT, 'config' => [ 'label' => [ 'en-GB' => 'Manual Assignment to Product Group', 'de-DE' => 'Manuelle Zuordnung zu Produktgruppe' ], 'helpText' => [ 'en-GB' => 'Product-side assignment to product group.', 'de-DE' => 'Produktseitige Zuordnung zu einer Produktgruppe (Kriterium kann dort genutzt werden).' ], 'customFieldType' => 'select', 'customFieldPosition' => 40, 'componentName' => 'sw-single-select', 'options' => [ [ 'value' => 'thick', 'label' => [ 'en-GB' => 'Thick toys', 'de-DE' => 'Sehr dicke Toys' ], ], [ 'value' => 'narrow', 'label' => [ 'en-GB' => 'Narrow toys', 'de-DE' => 'Extra schlanke Toys' ], ], [ 'value' => 'harness', 'label' => [ 'en-GB' => 'Harness toys', 'de-DE' => 'Harnesstaugliche Toys' ], ], [ 'value' => 'pimples', 'label' => [ 'en-GB' => 'Pimples toys', 'de-DE' => 'Toys mit Noppen' ], ], [ 'value' => 'helices', 'label' => [ 'en-GB' => 'Helices', 'de-DE' => 'Spiralstrukturen' ], ] ] ] ], // Base body for referencing the configuration product [ 'name' => self::CUSTOM_FIELDS_SET_PRODUCT . '_base_body', 'type' => CustomFieldTypes::TEXT, 'config' => [ 'label' => [ 'en-GB' => 'Base Body', 'de-DE' => 'Grundkörper' ], 'helpText' => [ 'en-GB' => 'Base body for referencing the configuration product.', 'de-DE' => 'Wenn dieses Feld ausgefüllt ist, wird auf das Konfigurationsformular verlinkt und das Feld Grundkörper mit diesem Wert gesetzt.' ], 'customFieldType' => 'text', 'customFieldPosition' => 50, ] ], // Product for canonical entry [ 'name' => self::CUSTOM_FIELDS_SET_PRODUCT . '_canonical_product', 'type' => CustomFieldTypes::SELECT, 'config' => [ 'label' => [ 'en-GB' => 'Product for canonical entry', 'de-DE' => 'Produkt für Canonical-Eintrag' ], 'helpText' => [ 'en-GB' => 'Product for canonical entry in meta data.', 'de-DE' => 'Dieses Produkt wird für den Canonical-Eintrag in den Meta-Angaben genutzt.' ], 'customFieldType' => 'entity', 'customFieldPosition' => 60, 'componentName' => 'sw-entity-single-select', 'entity' => 'product' ] ], // Flag a product as configuration with special templating [ 'name' => self::CUSTOM_FIELDS_SET_PRODUCT . '_is_configurator', 'type' => CustomFieldTypes::BOOL, 'config' => [ 'label' => [ 'en-GB' => 'Configurator', 'de-DE' => 'Konfigurator' ], 'helpText' => [ 'en-GB' => 'Product represents a configuration.', 'de-DE' => 'Artikel repräsentiert eine Konfiguration.' ], 'customFieldType' => 'switch', 'componentName' => 'sw-field', 'customFieldPosition' => 70, ] ], // Configuration header [ 'name' => self::CUSTOM_FIELDS_SET_PRODUCT . '_configuration_listing_header', 'type' => CustomFieldTypes::TEXT, 'config' => [ 'label' => [ 'en-GB' => 'Headline of the configurator', 'de-DE' => 'Titel des Konfigurators' ], 'helpText' => [ 'en-GB' => 'Headline in listings.', 'de-DE' => 'Titel des Konfigurator-Banners in Kategorielisten.' ], 'customFieldType' => 'text', 'customFieldPosition' => 80, ] ], // Configuration text [ 'name' => self::CUSTOM_FIELDS_SET_PRODUCT . '_configuration_listing_text', 'type' => CustomFieldTypes::TEXT, 'config' => [ 'label' => [ 'en-GB' => 'Teaser text of the configurator', 'de-DE' => 'Teaser-Text des Konfigurators' ], 'helpText' => [ 'en-GB' => 'Teaser text in listings.', 'de-DE' => 'Teaser-Text des Konfigurator-Banners in Kategorielisten.' ], 'customFieldType' => 'text', 'customFieldPosition' => 90, ] ], // Configuration button text [ 'name' => self::CUSTOM_FIELDS_SET_PRODUCT . '_configuration_listing_linktext', 'type' => CustomFieldTypes::TEXT, 'config' => [ 'label' => [ 'en-GB' => 'Button text of the configurator', 'de-DE' => 'Button-Text des Konfigurators' ], 'helpText' => [ 'en-GB' => 'Button text in listings.', 'de-DE' => 'Button-Text des Konfigurator-Banners in Kategorielisten.' ], 'customFieldType' => 'text', 'customFieldPosition' => 100, ] ], ], 'relations' => [ [ 'id' => Uuid::randomHex(), 'entityName' => ProductDefinition::ENTITY_NAME ] ] ] ], $context ); } $criteria = (new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOM_FIELDS_SET_SHIPPING_METHOD)); if ($customFieldSetRepo->search($criteria, $context)->count() === 0) { $customFieldSetRepo->create([ [ 'name' => self::CUSTOM_FIELDS_SET_SHIPPING_METHOD, 'position' => 300, 'config' => [ 'label' => [ 'en-GB' => 'SelfDelve - Shipping Methods', 'de-DE' => 'SelfDelve - Versandarten' ] ], 'customFields' => [ // Force phone number on method selection [ 'name' => self::CUSTOM_FIELDS_SET_SHIPPING_METHOD . '_mandatory_phonenumber', 'type' => CustomFieldTypes::BOOL, 'config' => [ 'label' => [ 'en-GB' => 'Phone number is mandatory', 'de-DE' => 'Telefonnummer muss angegeben werden' ], 'customFieldType' => 'switch', 'componentName' => 'sw-field', 'customFieldPosition' => 0 ] ] ], 'relations' => [ [ 'id' => Uuid::randomHex(), 'entityName' => ShippingMethodDefinition::ENTITY_NAME ] ] ] ], $context); } } /** * Remove the custom field sets. * @param Context $context */ private function removeCustomFieldSets(Context $context): void { $customFieldSetRepo = $this->container->get('custom_field_set.repository'); $setNames = [ self::CUSTOM_FIELDS_SET_CATEGORY, self::CUSTOM_FIELDS_SET_CUSTOMER, self::CUSTOM_FIELDS_SET_PRODUCT ]; foreach ($setNames as $setName) { $id = $customFieldSetRepo->searchIds((new Criteria())->addFilter(new EqualsFilter('name', $setName)), $context)->firstId(); if ($id) { $customFieldSetRepo->delete([ ['id' => $id] ], $context); } } }}