custom/plugins/dev24OpenGraph/src/dev24OpenGraph.php line 15

Open in your IDE?
  1. <?php
  2. namespace dev24\Opengraph;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  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. class dev24OpenGraph extends Plugin
  12. {
  13.     public function install(InstallContext $installContext): void
  14.     {
  15.         parent::install($installContext); // TODO: Change the autogenerated stub
  16.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  17.         $customFieldSetRepository->create([
  18.             [
  19.                 'name' => 'dev24_open_graph_custom_fields',
  20.                 'global' => false,
  21.                 'config' => [
  22.                     'label' => [
  23.                         'de-DE' => 'Open Graph',
  24.                         'en-GB' => 'Open Graph'
  25.                     ]
  26.                 ],
  27.                 'relations' => [
  28.                     [
  29.                         'entityName' => 'product'
  30.                     ],
  31.                     [
  32.                         'entityName' => 'category'
  33.                     ]
  34.                 ],
  35.                 'customFields' => [
  36.                     [
  37.                         'name' => 'dev24_og_title',
  38.                         'type' => CustomFieldTypes::TEXT,
  39.                         'config' => [
  40.                             'label' => [
  41.                                 'de-DE' => 'Titel',
  42.                                 'en-GB' => 'Title'
  43.                             ],
  44.                             'customFieldPosition' => 1
  45.                         ]
  46.                     ],
  47.                     [
  48.                         'name' => 'dev24_og_description',
  49.                         'type' => CustomFieldTypes::TEXT,
  50.                         'config' => [
  51.                             'label' => [
  52.                                 'de-DE' => 'Beschreibung',
  53.                                 'en-GB' => 'Description'
  54.                             ],
  55.                             'customFieldPosition' => 2
  56.                         ]
  57.                     ],
  58.                     [
  59.                         'name' => 'dev24_og_image',
  60.                         'type' => CustomFieldTypes::TEXT,
  61.                         'config' => [
  62.                             'label' => [
  63.                                 'de-DE' => 'Bild',
  64.                                 'en-GB' => 'Image'
  65.                             ],
  66.                             'componentName' => "sw-media-field",
  67.                             'customFieldType' => "media",
  68.                             'customFieldPosition' => 3
  69.                         ]
  70.                     ]
  71.                 ]
  72.             ]
  73.         ],  $installContext->getContext());
  74.     }
  75.     public function uninstall(UninstallContext $uninstallContext): void
  76.     {
  77.         parent::uninstall($uninstallContext); // TODO: Change the autogenerated stub
  78.         $criteria = new Criteria();
  79.         $criteria->addFilter(
  80.             new MultiFilter(
  81.                 'OR',
  82.                 [
  83.                     new EqualsAnyFilter('name'$this->getCustomFields())
  84.                 ]
  85.             )
  86.         );
  87.         /**
  88.          * @var EntityRepositoryInterface $customFieldRepository
  89.          */
  90.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  91.         $idsToDelete $customFieldSetRepository->searchIds($criteria$uninstallContext->getContext());
  92.         if ($idsToDelete->getTotal() === 0) {
  93.             return;
  94.         }
  95.         $keys array_map(function ($id) {
  96.             return ['id' => $id];
  97.         }, $idsToDelete->getIds());
  98.         if($idsToDelete->getTotal() > 0) {
  99.             $customFieldSetRepository->delete($keys$uninstallContext->getContext());
  100.         }
  101.     }
  102.     protected function getCustomFields(): array
  103.     {
  104.         return [
  105.             'dev24_open_graph_custom_fields'
  106.         ];
  107.     }
  108. }