ggggino/sonataextrafields-bundle

This bundle contains some useful field types

Installs: 49

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Language:HTML

Type:symfony-bundle

1.0.4 2019-05-28 15:16 UTC

This package is auto-updated.

Last update: 2024-10-29 03:22:58 UTC


README

A set of useful form fields for SonataAdmin

include the base theme

config.yml

twig:
    form_themes:
        ...
        - 'GGGGinoSonataExtraFieldsBundle:Form:extra_fields.html.twig'

routing.yml

ggggino_extrafields:
    resource: '@GGGGinoSonataExtraFieldsBundle/Controller/'
    type: annotation

CollectionBuilder Field

With this class you can create collection items depending on a wizard(non mapped )

EntityAdmin.php

use Allyou\ManagementBundle\Form\Type\CustomCollectionType;

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('medias', CollectionBuilderType::class, array(
                'by_reference' => false,
                'wrapper_class' => 'col-xs-4',
                'formWizard' => function(FormBuilderInterface $builder) {
                    // Here you can build your wizard
                },
                'formWizardPreSetData' => function(FormEvent $event) {
                    // Here you can set particular
                },
                'formWizardPreSubmit' => function(FormEvent $event) {
                    // Here you can manipulate the model before the persist
                },
                'formWizardOnSubmit' => function(FormEvent $event) {
                    // Here you can manipulate the model after the persist
                }
            ), array(
                'edit' => 'inline',
                //'inline' => 'table',
                'sortable' => 'position',
                'admin_code' => 'app.admin.media',
            ))
    ;
}

RealWorldExampleAdmin.php

$formMapper->add('fasce', CollectionBuilderType::class, array(
    'by_reference' => false,
    'wrapper_class' => 'col-xs-4',
    'formWizard' => function(FormBuilderInterface $builder) use ($em) {
        $companies = $em->getRepository(Company::class)->findAll();
        $companyMapped = array("Select a company" => 0);

        /** @var Ditta $value */
        foreach( $companies as $company ) {
            $companyMapped[$company->getLabel()] = $company->getId();
        }

        $builder
            ->add('company', ChoiceType::class, array(
                'choices' => $companyMapped
            ));
    },
    'formWizardOnSubmit' => function(FormEvent $event) use ($em) {
        $data = $event->getData();
        /** @var PersistentCollection $items */
        $items = $data['items'];
        $wizard = $data['wizard'];

        if( !isset($wizard['company']) || !$wizard['company'] )
            return;

        $idCompany = isset($wizard['company']) ? $wizard['company'] : null;
        $companyRef = $em->getReference(Company::class, $idCompany);

        /** @var User[] $usersInCompany */
        $usersInCompany = $em->getRepository(User::class)->findBy(array('company' => $companyRef));

        foreach($usersInCompany as $user) {
            $temp = new GiornataFascia();
            $temp->setOre(0);
            $temp->setUtente($user);

            $items->add($temp);
        }
    }
), array(
    'edit' => 'inline',
    'inline' => 'table',
    'sortable' => 'position',
    'admin_code' => 'app.admin.giornata_fascia',
));

Extra options

inherit all the extra property of the PimpedCollection field

PimpedCollection Field

With this class you can add some useful property to the collection

EntityAdmin.php

use Allyou\ManagementBundle\Form\Type\CustomCollectionType;

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('medias', CustomCollectionType::class, array(
                'by_reference' => false,
                'wrapper_class' => 'col-xs-4'
            ), array(
                'edit' => 'inline',
                //'inline' => 'table',
                'sortable' => 'position',
                'admin_code' => 'app.admin.media',
            ))
    ;
}

Extra options