kunicmarko20 / SonataImporterBundle

Easier handling of Import in Sonata Admin.
MIT License
4 stars 3 forks source link
hacktoberfest import php sonata-admin symfony

SonataImporterBundle

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();
    }
}