FriendsOfSylius / SyliusImportExportPlugin

Sylius plugin to import / export data
MIT License
118 stars 82 forks source link

Disabling some exporters #214

Open joelclouddistrict opened 4 years ago

joelclouddistrict commented 4 years ago

Is it possible to disable exporters? I need to export orders, but I don't want the export button to appear on the other resources that are currently available (countries, tax_categories, customer and products).

I could register a CompilerPass and remove the definitions I don't want, but I would like to know if there is another way.

oallain commented 4 years ago

IMO, it's not possible by config

joelclouddistrict commented 4 years ago

Thanks!, CompilerPass it is, then.

florian-ct commented 4 years ago

Hi,

Could you explain how you did it please ? I'm trying to disable the .json format of the orders export.

Thanks !

joelclouddistrict commented 4 years ago

I registered a compiler pass that only added the exporter I wanted to the admin (order export in xlsx format). I looked into the base exporter pass for reference.

You also need to set exporter.web_ui to false in the package config.

<?php

namespace App\DependencyInjection\Compiler;

use FriendsOfSylius\SyliusImportExportPlugin\Exporter\ExporterRegistry;
use FriendsOfSylius\SyliusImportExportPlugin\Listener\ExportButtonGridListener;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
 * This CompilerPass adds the export button only in the Order's admin list
 * It is needed because FriendsOfSylius\SyliusImportExportPlugin RegisterExporterPass adds
 * the export button on all registered resources (customer, products...)
 */
final class RegisterOrderExportButtonPass implements CompilerPassInterface
{
    private const EXPORTER_TYPE = 'sylius.order';
    private const EXPORTER_FORMAT = 'xlsx';
    private const EXPORTER_ID = 'sylius.exporter.orders.xlsx';
    private const CONTROLLER_NAME = 'sylius.controller.export_data_order';
    private const EVENT_NAME = 'sylius.grid.admin_order';

    /**
     * {@inheritdoc}
     */
    public function process(ContainerBuilder $container): void
    {
        if ($container->has(self::EXPORTER_ID) == false) {
            return;
        }

        $type = self::EXPORTER_TYPE;
        $formats = [self::EXPORTER_FORMAT];

        $eventHookName = ExporterRegistry::buildGridButtonsEventHookName($type, $formats) . '_export';

        if ($container->has($eventHookName)) {
            return;
        }

        if (!$container->has(self::CONTROLLER_NAME)) {
            return;
        }

        $container
            ->register(
                $eventHookName,
                ExportButtonGridListener::class
            )
            ->setAutowired(false)
            ->addArgument($type)
            ->addArgument($formats)
            ->addMethodCall('setRequest', [new Reference('request_stack')])
            ->addTag(
                'kernel.event_listener',
                [
                    'event' => self::EVENT_NAME,
                    'method' => 'onSyliusGridAdmin',
                ]
            );
    }

    private function getControllerName(string $type): string
    {
        if (strpos($type, '.') !== false) {
            $type = substr($type, strpos($type, '.') + 1);
        }

        return \sprintf('sylius.controller.export_data_%s', $type);
    }
}
florian-ct commented 4 years ago

Thanks a lot !

Prior to your answer, I took the plugin service (sylius.exporter.orders.json), put it in my services.yaml, and commented everything but the class. And it worked !

krystal25 commented 4 years ago

I registered a compiler pass that only added the exporter I wanted to the admin (order export in xlsx format). I looked into the base exporter pass for reference.

You also need to set exporter.web_ui to false in the package config.

<?php

namespace App\DependencyInjection\Compiler;

use FriendsOfSylius\SyliusImportExportPlugin\Exporter\ExporterRegistry;
use FriendsOfSylius\SyliusImportExportPlugin\Listener\ExportButtonGridListener;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
 * This CompilerPass adds the export button only in the Order's admin list
 * It is needed because FriendsOfSylius\SyliusImportExportPlugin RegisterExporterPass adds
 * the export button on all registered resources (customer, products...)
 */
final class RegisterOrderExportButtonPass implements CompilerPassInterface
{
    private const EXPORTER_TYPE = 'sylius.order';
    private const EXPORTER_FORMAT = 'xlsx';
    private const EXPORTER_ID = 'sylius.exporter.orders.xlsx';
    private const CONTROLLER_NAME = 'sylius.controller.export_data_order';
    private const EVENT_NAME = 'sylius.grid.admin_order';

    /**
     * {@inheritdoc}
     */
    public function process(ContainerBuilder $container): void
    {
        if ($container->has(self::EXPORTER_ID) == false) {
            return;
        }

        $type = self::EXPORTER_TYPE;
        $formats = [self::EXPORTER_FORMAT];

        $eventHookName = ExporterRegistry::buildGridButtonsEventHookName($type, $formats) . '_export';

        if ($container->has($eventHookName)) {
            return;
        }

        if (!$container->has(self::CONTROLLER_NAME)) {
            return;
        }

        $container
            ->register(
                $eventHookName,
                ExportButtonGridListener::class
            )
            ->setAutowired(false)
            ->addArgument($type)
            ->addArgument($formats)
            ->addMethodCall('setRequest', [new Reference('request_stack')])
            ->addTag(
                'kernel.event_listener',
                [
                    'event' => self::EVENT_NAME,
                    'method' => 'onSyliusGridAdmin',
                ]
            );
    }

    private function getControllerName(string $type): string
    {
        if (strpos($type, '.') !== false) {
            $type = substr($type, strpos($type, '.') + 1);
        }

        return \sprintf('sylius.controller.export_data_%s', $type);
    }
}

Hi,

Where did you register this? I noticed that FOSSyliusImportExportPlugin registers these exporterPass by $container->addCompilerPass(new RegisterExporterPass()); and I can't seem to figure how to override it to point to my own exporterPass

joelclouddistrict commented 4 years ago

You need to add it in your src/Kernel.php file, on the build method:

final class Kernel extends BaseKernel
{
    protected function build(ContainerBuilder $container): void
    {
        $container->addCompilerPass(new RegisterOrderExportButtonPass());
    }
}
oallain commented 4 years ago

Hi, If somebody want to make a PR to add parameter to add this feature, your help is welcome :wink: