2017 © Pedro Peláez
 

symfony-bundle sonata-importer-bundle

Easier handling of Import in Sonata Admin.

image

kunicmarko/sonata-importer-bundle

Easier handling of Import in Sonata Admin.

  • Wednesday, July 25, 2018
  • by kunicmarko20
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

SonataImporterBundle

Easier handling of Import in Sonata Admin., (*1)

Built on top of Importer., (*2)

PHP Version Latest Stable Version Latest Unstable Version, (*3)

Build Status Coverage Status, (*4)

Import Action, (*5)

Import Form, (*6)

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., (*7)

1. Install kunicmarko/importer, (*8)

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

2. Add dependency with composer, (*10)

composer require kunicmarko/sonata-importer-bundle

3. Register the bundle in your Kernel, (*11)

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

Configuration

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

# 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., (*13)

Prepare Admin Class

Your Admin class has to implement KunicMarko\SonataImporterBundle\Admin\AdminWithImport., (*14)

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., (*15)

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., (*16)

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., (*17)

That will look like:, (*18)

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

The Versions