kunicmarko / sonata-importer-bundle
Easier handling of Import in Sonata Admin.
Installs: 9 508
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 1
Open Issues: 1
Type:symfony-bundle
Requires
- php: ^7.4
- kunicmarko/importer: ^0.2.0
- sonata-project/admin-bundle: ^3.77
- sonata-project/twig-extensions: ^1.4
- symfony/config: ^4.4 || ^5.1
- symfony/dependency-injection: ^4.4 || ^5.1
- symfony/form: ^4.4 || ^5.1
- symfony/http-foundation: ^4.4 || ^5.1
- symfony/http-kernel: ^4.4 || ^5.1.5
- symfony/options-resolver: ^4.4 || ^5.1
- symfony/validator: ^4.4 || ^5.1
Requires (Dev)
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.
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(); } }