vendor/crehler/inspirations/src/CrehlerInspirations.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\Inspirations;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  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\DeactivateContext;
  11. use Shopware\Core\System\CustomField\CustomFieldTypes;
  12. use Symfony\Component\Config\FileLocator;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  15. class CrehlerInspirations extends Plugin
  16. {
  17.     public const CATEGORY_CUSTOM_FIELDS = [
  18.         'name' => 'crehler_category_is_inspirations',
  19.         'type' => CustomFieldTypes::BOOL,
  20.     ];
  21.     public function build(ContainerBuilder $container): void
  22.     {
  23.         parent::build($container);
  24.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__ '/DependencyInjection/'));
  25.         $loader->load('entity.xml');
  26.         $loader->load('subscriber.xml');
  27.         $loader->load('service.xml');
  28.         $loader->load('snippets.xml');
  29.         $loader->load('message.xml');
  30.     }
  31.     public function activate(ActivateContext $activateContext): void
  32.     {
  33.         $this->activateCustomField(self::CATEGORY_CUSTOM_FIELDS$activateContext->getContext(), $this->container->get('custom_field.repository'));
  34.     }
  35.     public function deactivate(DeactivateContext $deactivateContext): void
  36.     {
  37.     }
  38.     private function activateCustomField(array $customFieldContext $contextEntityRepositoryInterface $customFieldRepository): void
  39.     {
  40.         $customFieldIds $this->getCustomFieldIds($customField$context$customFieldRepository);
  41.         if ($customFieldIds->getTotal() !== 0) {
  42.             return;
  43.         }
  44.         $customFieldRepository->upsert([$customField], $context);
  45.     }
  46.     private function getCustomFieldIds(array $customFieldContext $contextEntityRepositoryInterface $customFieldRepository): IdSearchResult
  47.     {
  48.         $criteria = new Criteria();
  49.         $criteria->addFilter(new EqualsFilter('name'$customField['name']));
  50.         return $customFieldRepository->searchIds($criteria$context);
  51.     }
  52. }