Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disabling some exporters #214

Open
joelclouddistrict opened this issue Dec 3, 2019 · 8 comments
Open

Disabling some exporters #214

joelclouddistrict opened this issue Dec 3, 2019 · 8 comments

Comments

@joelclouddistrict
Copy link

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
Copy link
Member

oallain commented Dec 3, 2019

IMO, it's not possible by config

@joelclouddistrict
Copy link
Author

Thanks!, CompilerPass it is, then.

@florian-ct
Copy link
Contributor

Hi,

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

Thanks !

@joelclouddistrict
Copy link
Author

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
Copy link
Contributor

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
Copy link

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
Copy link
Author

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
Copy link
Member

oallain commented Apr 29, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants