kunicmarko/sonata-importer-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.

Easier handling of Import in Sonata Admin.

0.1.3 2020-10-18 18:36 UTC

This package is auto-updated.

Last update: 2020-11-16 04:33:41 UTC


README

Easier handling of Import in Sonata Admin.

Built on top of Importer.

PHP Version Latest Stable Version Latest Unstable Version

Build Status Coverage Status

Import Action

Import Form

Documentation

Installation

Because Symfony Flex auto-detects and then registers bundles on its own, you first need to install kunicmarko/importer, add it to bundles.php, and then do the same thing for kunicmarko/sonata-importer-bundle.

1. Install kunicmarko/importer

First you need to install kunicmarko/importer, and register the bundle by following this guide.

2. Add dependency with composer

composer require kunicmarko/sonata-importer-bundle

3. Register the bundle in your Kernel

return [
    //...
    KunicMarko\SonataImporterBundle\SonataImporterBundle::class => ['all' => true],
];

Configuration

Currently, you can only change the template files used in bundle, default config looks like:

# config/packages/sonata_importer.yaml
sonata_importer:
    templates:
        form:                 '@SonataImporter/form.html.twig'
        action_button:        '@SonataImporter/action_button.html.twig'
        dashboard_action:     '@SonataImporter/dashboard_action.html.twig'

How to use

If you haven't already go and read Importer documentation. I will assume you are already familiar with ImportConfiguration and I will just explain what is different in this bundle.

Prepare Admin Class

Your Admin class has to implement KunicMarko\SonataImporterBundle\Admin\AdminWithImport.

Prepare Controller

By default if you don't set Controller in your Admin service definition we will replace it with instance of KunicMarko\SonataImporterBundle\Controller\ImportCRUDController.

Custom Controller

If you are using your own custom controller make sure it implements KunicMarko\SonataImporterBundle\Controller\ControllerWithImport, also you will have to add KunicMarko\SonataImporterBundle\Controller\ImportActionTrait trait to your controller.

AutoConfigure ImportConfiguration

To be able to auto-configure your ImportConfiguration they will have to implement KunicMarko\SonataImporterBundle\SonataImportConfiguration and configure format and adminClass methods along with other methods.

That will look like:

class CategoryCSVImportConfiguration implements SonataImportConfiguration
{
    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public static function adminClass(): string
    {
        return CategoryAdmin::class;
    }

    public static function format(): string
    {
        return 'csv';
    }

    public function map(array $item, array $additionalData)
    {
        $category = new Category();

        $category->setName($item[0]);

        $this->entityManager->persist($category);
    }

    public function save(array $items, array $additionalData): void
    {
        $this->entityManager->flush();
    }
}