custom/plugins/SvenDasMenu/src/SvenDasMenu.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Sven\DasMenu;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\System\CustomField\CustomFieldTypes;
  12. class SvenDasMenu extends Plugin
  13. {
  14.     public function install(InstallContext $installContext): void
  15.     {
  16.         parent::install($installContext);
  17.         /** @var EntityRepositoryInterface $repository */
  18.         $repository $this->container->get('custom_field_set.repository');
  19.         $context $installContext->getContext();
  20.         $criteriaCategorie = new Criteria();
  21.         $criteriaCategorie->addFilter(new EqualsFilter('name''sven_dasmenu_category'));
  22.         $criteriaCategorie->addAssociation('customFields');
  23.         $fieldsCategorie $repository->search($criteriaCategorie$installContext->getContext())->first();
  24.         if ($fieldsCategorie === null) {
  25.             $this->createCategoryCustomField($context);
  26.         }
  27.     }
  28.     public function uninstall(UninstallContext $context): void
  29.     {
  30.         parent::uninstall($context);
  31.         if ($context->keepUserData()) {
  32.             return;
  33.         }
  34.         $connection $this->container->get(Connection::class);
  35.         $connection->executeUpdate('DELETE FROM `system_config` WHERE configuration_key LIKE "%SvenDasMenu.config%"');
  36.         $connection->executeUpdate('DELETE FROM `custom_field` WHERE name = "sven_dasmenu_category_image"');
  37.         $connection->executeUpdate('DELETE FROM `custom_field_set` WHERE name = "sven_dasmenu_category"');
  38.     }
  39.     private function createCategoryCustomField(Context $context)
  40.     {
  41.         /** @var EntityRepositoryInterface $repository */
  42.         $repository $this->container->get('custom_field_set.repository');
  43.         $repository->upsert(     [
  44.             [
  45.                 'name' => 'sven_dasmenu_category',
  46.                 'config' => [
  47.                     'label' => [
  48.                         'de-DE' => 'DasMenu Bild',
  49.                         'en-GB' => 'DasMenu Image'
  50.                     ],
  51.                 ],
  52.                 'customFields' => [
  53.                     [
  54.                         'name' => 'sven_dasmenu_category_image',
  55.                         'type' => CustomFieldTypes::MEDIA,
  56.                         'label' => [
  57.                             'de-DE' => 'DasMenu Bild',
  58.                             'en-GB' => 'DasMenu Image'
  59.                         ],
  60.                         'config' => [
  61.                             'componentName' => 'sw-media-field',
  62.                             'customFieldType' => 'media',
  63.                             'customFieldPosition' => 1
  64.                         ]
  65.                     ],
  66.                 ],
  67.                 'relations' => [
  68.                     [
  69.                         'entityName' => 'category',
  70.                     ],
  71.                 ],
  72.             ],
  73.         ], $context);
  74.     }
  75. }