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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ProxaNavigationMenu;
  3. use Shopware\Core\Content\Category\CategoryDefinition;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  10. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  11. use Shopware\Core\System\CustomField\CustomFieldTypes;
  12. class ProxaNavigationMenu extends Plugin
  13. {
  14.     const PREFIX 'proxa_menu';
  15.     public function activate(ActivateContext $activateContext): void
  16.     {
  17.         $this->makeCustomFields();
  18.     }
  19.     public function update(UpdateContext $updateContext): void
  20.     {
  21.         $this->makeCustomFields();
  22.     }
  23.     private function makeCustomFields()
  24.     {
  25.         $this->removeCustomFields();
  26.         foreach ($this->getFields() as $key => $fields) {
  27.             $setName self::PREFIX '__' $key;
  28.             $fieldsSet $this->getCustomFieldSet($setName$fields);
  29.             $this->container->get('custom_field_set.repository')->upsert([$fieldsSet], Context::createDefaultContext());
  30.         }
  31.     }
  32.     private function getFields(): array
  33.     {
  34.         return [
  35.             CategoryDefinition::ENTITY_NAME => [
  36.                 'relations' => [
  37.                     ['entityName' => CategoryDefinition::ENTITY_NAME]
  38.                 ],
  39.                 'customFields' => [
  40.                     [
  41.                         'id' => md5(self::PREFIX '_slider_media'),
  42.                         'name' => self::PREFIX '_slider_media',
  43.                         'type' => CustomFieldTypes::TEXT,
  44.                         'config' => [
  45.                             'customFieldType' => CustomFieldTypes::MEDIA,
  46.                             'componentName' => 'sw-media-field',
  47.                             'label' => [
  48.                                 'de-DE' => 'Slider Image',
  49.                                 'en-GB' => 'Slider Image',
  50.                             ],
  51.                         ]
  52.                     ],
  53.                     [
  54.                         'id' => md5(self::PREFIX '_slider_sub'),
  55.                         'name' => self::PREFIX '_slider_sub',
  56.                         'type' => CustomFieldTypes::SELECT,
  57.                         'config' => [
  58.                             'customFieldType' => CustomFieldTypes::ENTITY,
  59.                             'componentName' => 'sw-entity-multi-id-select',
  60.                             'entity' => CategoryDefinition::ENTITY_NAME,
  61.                             'label' => [
  62.                                 'de-DE' => 'Slider Categories',
  63.                                 'en-GB' => 'Slider Categories',
  64.                             ],
  65.                         ]
  66.                     ],
  67.                     [
  68.                         'id' => md5(self::PREFIX '_slider_desc'),
  69.                         'name' => self::PREFIX '_slider_desc',
  70.                         'type' => CustomFieldTypes::TEXT,
  71.                         'config' => [
  72.                             'customFieldType' => CustomFieldTypes::TEXT,
  73.                             'label' => [
  74.                                 'de-DE' => 'Slider Description',
  75.                                 'en-GB' => 'Slider Description',
  76.                             ],
  77.                         ]
  78.                     ],
  79.                     [
  80.                         'id' => md5(self::PREFIX '_category_icon_media'),
  81.                         'name' => self::PREFIX '_category_icon_media',
  82.                         'type' => CustomFieldTypes::TEXT,
  83.                         'config' => [
  84.                             'customFieldType' => CustomFieldTypes::MEDIA,
  85.                             'componentName' => 'sw-media-field',
  86.                             'label' => [
  87.                                 'de-DE' => 'Category Icon',
  88.                                 'en-GB' => 'Category Icon',
  89.                             ],
  90.                         ]
  91.                     ],
  92.                 ]
  93.             ],
  94.         ];
  95.     }
  96.     private function removeCustomFields()
  97.     {
  98.         foreach ($this->getFields() as $key => $fields) {
  99.             $criteria = (new Criteria())->addFilter(new ContainsFilter('name'self::PREFIX '__' $key));
  100.             $customFieldRepo $this->container->get('custom_field_set.repository');
  101.             /** @var IdSearchResult $customField */
  102.             $customFieldIds $customFieldRepo->searchIds($criteriaContext::createDefaultContext());
  103.             $ids array_map(static function ($id) {
  104.                 return ['id' => $id];
  105.             }, $customFieldIds->getIds());
  106.             if (!empty($ids)) {
  107.                 $customFieldRepo->delete($idsContext::createDefaultContext());
  108.             }
  109.         }
  110.     }
  111.     private function getCustomFieldSet(string $setName, array $fields): array
  112.     {
  113.         return array_merge([
  114.             'id' => md5($setName),
  115.             'name' => $setName,
  116.             'active' => true,
  117.             'config' => [
  118.                 'label' => [
  119.                     'de-DE' => 'Navigation Menu',
  120.                     'en-GB' => 'Navigation Menu'
  121.                 ]
  122.             ]
  123.         ], $fields);
  124.     }
  125. }