custom/plugins/EWHeaderFooter/src/EWHeaderFooter.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace EWHeaderFooter;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  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\System\CustomField\CustomFieldTypes;
  9. class EWHeaderFooter extends Plugin
  10. {
  11.     public function install(InstallContext $installContext): void
  12.     {
  13.         $this->createCustomFields($installContext->getContext());
  14.     }
  15.     public function uninstall(UninstallContext $uninstallContext): void
  16.     {
  17.         if ($uninstallContext->keepUserData()) {
  18.             return;
  19.         }
  20.         $this->removeCustomFields($uninstallContext->getContext());
  21.     }
  22.     private function createCustomFields($context){
  23.         $customFields = [
  24.             [
  25.                 'name' => 'ew_header_footer_fields',
  26.                 'active' => true,
  27.                 'global' => true,
  28.                 'config' => [
  29.                     'label' => [
  30.                         'en-GB' => 'Header and Footer',
  31.                         'de-DE' => 'Header und Footer'
  32.                     ],
  33.                 ],
  34.                 'customFields' => [
  35.                     [
  36.                         'name' => 'ew-hide-header',
  37.                         'type' => CustomFieldTypes::SWITCH,
  38.                         'config' => [
  39.                             'bordered' => false,
  40.                             'label' => [
  41.                                 'en-GB' => 'Hide Header',
  42.                                 'de-DE' => 'Header ausblenden',
  43.                             ],
  44.                             'customFieldPosition' => 1
  45.                         ]
  46.                     ],
  47.                     [
  48.                         'name' => 'ew-minimal-footer',
  49.                         'type' => CustomFieldTypes::SWITCH,
  50.                         'config' => [
  51.                             'bordered' => false,
  52.                             'label' => [
  53.                                 'en-GB' => 'Second Footer (Own Footer for LP)',
  54.                                 'de-DE' => 'Zweiter Footer (Eigener Footer für LP)',
  55.                             ],
  56.                             'customFieldPosition' => 1
  57.                         ]
  58.                     ],
  59.                 ],
  60.                 'relations' => [
  61.                     ['entityName' => 'category']
  62.                 ]
  63.             ],
  64.         ];
  65.         $repo $this->container->get('custom_field_set.repository');
  66.         $customFieldSetEntry $repo->search((new Criteria())->addFilter(new EqualsFilter('name''ew_header_footer_fields')), $context)->first();
  67.         if(!$customFieldSetEntry) {
  68.             foreach ($customFields as $customFieldSet) {
  69.                 $repo->upsert([$customFieldSet], $context);
  70.             }
  71.         }
  72.     }
  73.     private function removeCustomFields($context){
  74.         $cfsRepo $this->container->get('custom_field_set.repository');
  75.         $cfRepo $this->container->get('custom_field.repository');
  76.         $cfsId $cfsRepo->search((new Criteria())->addFilter(new EqualsFilter('name''ew_header_footer_fields')), $context)->first();
  77.         $cfsRepo->delete([['id' => $cfsId->getId()]], $context);
  78.         $cfIds $cfRepo->search((new Criteria())->addFilter(new EqualsFilter('customFieldSetId'$cfsId->getId())), $context)->getIds();
  79.         $ids = [];
  80.         foreach($cfIds as $id){
  81.             $ids[] = ['id' => $id];
  82.         }
  83.         $cfRepo->delete($ids$context);
  84.     }
  85. }