vendor/store.shopware.com/atlseourlmanager/src/AtlSeoUrlManager.php line 14

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Atl\SeoUrlManager;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  5. use Shopware\Core\Framework\Plugin;
  6. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  7. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  8. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Shopware\Core\System\CustomField\CustomFieldTypes;
  11. class AtlSeoUrlManager extends Plugin
  12. {
  13.     public const SEO_URL_MANAGER_PRODUCT_SET 'atl_seo_url_manager_product';
  14.     public const SEO_URL_MANAGER_PRODUCT_RELATION 'atl_seo_url_manager_product_relation';
  15.     public const SEO_URL_MANAGER_PRODUCT_BOX_LINK_INHERITANCE_OVERRIDE 'atl_seo_url_manager_product_box_link_inheritance_override';
  16.     public const SEO_URL_MANAGER_PRODUCT_CANONICAL_INHERITANCE_OVERRIDE 'atl_seo_url_manager_product_canonical_inheritance_override';
  17.     /**
  18.      * @param InstallContext $installContext
  19.      * @return void
  20.      */
  21.     public function install(InstallContext $installContext): void
  22.     {
  23.         parent::install($installContext);
  24.         $this->installCustomFields($installContext->getContext());
  25.     }
  26.     /**
  27.      * @param UninstallContext $uninstallContext
  28.      * @return void
  29.      */
  30.     public function uninstall(UninstallContext $uninstallContext): void
  31.     {
  32.         parent::uninstall($uninstallContext);
  33.         if ($uninstallContext->keepUserData()) {
  34.             return;
  35.         }
  36.         $criteria = new Criteria();
  37.         $criteria->addFilter(new ContainsFilter('name''atl_seo_url_manager'));
  38.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  39.         $result $customFieldSetRepository->search($criteria$uninstallContext->getContext());
  40.         $ids $result->getIds();
  41.         if (empty($ids)) {
  42.             return;
  43.         }
  44.         $data = [];
  45.         foreach ($ids as $id) {
  46.             $data[] = [
  47.                 'id' => $id
  48.             ];
  49.         }
  50.         $customFieldSetRepository->delete($data$uninstallContext->getContext());
  51.     }
  52.     /**
  53.      * @param UpdateContext $updateContext
  54.      * @return void
  55.      */
  56.     public function postUpdate(UpdateContext $updateContext): void
  57.     {
  58.         parent::postUpdate($updateContext);
  59.         $this->installCustomFields($updateContext->getContext());
  60.     }
  61.     /**
  62.      * @param $context
  63.      * @return void
  64.      */
  65.     private function installCustomFields($context): void
  66.     {
  67.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  68.         $customFieldSetRepository->upsert([
  69.             [
  70.                 'id' => Uuid::fromStringToHex(self::SEO_URL_MANAGER_PRODUCT_SET),
  71.                 'name' => self::SEO_URL_MANAGER_PRODUCT_SET,
  72.                 'config' => [
  73.                     'label' => [
  74.                         'en-GB' => 'Product (SEO URL Manager)',
  75.                         'de-DE' => 'Produkt (SEO URL Manager)'
  76.                     ],
  77.                     'translated' => true
  78.                 ],
  79.                 'relations' => [
  80.                     [
  81.                         'id' => Uuid::fromStringToHex(self::SEO_URL_MANAGER_PRODUCT_RELATION),
  82.                         'entityName' => 'product'
  83.                     ]
  84.                 ],
  85.                 'customFields' => [
  86.                     [
  87.                         'id' => Uuid::fromStringToHex(self::SEO_URL_MANAGER_PRODUCT_BOX_LINK_INHERITANCE_OVERRIDE),
  88.                         'name' => self::SEO_URL_MANAGER_PRODUCT_BOX_LINK_INHERITANCE_OVERRIDE,
  89.                         'type' => CustomFieldTypes::BOOL,
  90.                         'customFieldSetId' => Uuid::fromStringToHex(self::SEO_URL_MANAGER_PRODUCT_SET),
  91.                         'config' => [
  92.                             'type' => 'switch',
  93.                             'label' => [
  94.                                 'en-GB' => 'Unlink product box on parent for this product',
  95.                                 'de-DE' => 'Produkt-Box-Verlinkung auf Parent für dieses Produkt aufheben'
  96.                             ],
  97.                             'helpText' => [
  98.                                 'en-GB' => 'With this option you can override the global app setting to link from product boxes to the parent for this product. <br><br> This automatically reverts to the setting you made under "Variants > Storefront presentation > Product listings".',
  99.                                 'de-DE' => 'Mit dieser Option kannst du die globale App-Einstellung, aus Produkt Boxen auf den Parent Artikel zu verlinken, für dieses Produkt aufheben. <br><br> Damit greift automatisch wieder die Einstellung, welche du unter "Varianten > Storefront-Darstellung > Produktliste" vornimmst.'
  100.                             ],
  101.                             'componentName' => 'sw-field',
  102.                             'customFieldType' => 'switch',
  103.                             'customFieldPosition' => 1
  104.                         ]
  105.                     ],
  106.                     [
  107.                         'id' => Uuid::fromStringToHex(self::SEO_URL_MANAGER_PRODUCT_CANONICAL_INHERITANCE_OVERRIDE),
  108.                         'name' => self::SEO_URL_MANAGER_PRODUCT_CANONICAL_INHERITANCE_OVERRIDE,
  109.                         'type' => CustomFieldTypes::BOOL,
  110.                         'customFieldSetId' => Uuid::fromStringToHex(self::SEO_URL_MANAGER_PRODUCT_SET),
  111.                         'config' => [
  112.                             'type' => 'switch',
  113.                             'label' => [
  114.                                 'en-GB' => 'Unlink parent canonical URL for variants for this product',
  115.                                 'de-DE' => 'Parent Canonical-URL bei Varianten für dieses Produkt aufheben'
  116.                             ],
  117.                             'helpText' => [
  118.                                 'en-GB' => 'This option allows you to override the global app setting to use the parent canonical URL on variants for this product. <br><br> This automatically makes the canonical URL the URL of the respective variant again. In addition, the setting you made under "SEO > Use single canonical URL for all variants" also applies again.',
  119.                                 'de-DE' => 'Mit dieser Option kannst du die globale App-Einstellung, die Parent Canonical-URL bei Varianten zu verwenden, für dieses Produkt aufheben. <br><br> Damit ist die Canonical-URL automatisch wieder die URL der jeweiligen Variante. Zusätzlich greift auch wieder die Einstellung, welche du unter "SEO > Verwende dieselbe Canonical-URL für alle Varianten" vornimmst.'
  120.                             ],
  121.                             'componentName' => 'sw-field',
  122.                             'customFieldType' => 'switch',
  123.                             'customFieldPosition' => 2
  124.                         ]
  125.                     ]
  126.                 ]
  127.             ]
  128.         ], $context);
  129.     }
  130. }