2017 © Pedro Peláez
 

symfony-bundle dependentautocompletebundle

Extends Sonata Autocomplete Form Type and filters results using other form fields values

image

localinks/dependentautocompletebundle

Extends Sonata Autocomplete Form Type and filters results using other form fields values

  • Monday, January 15, 2018
  • by kershin
  • Repository
  • 0 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 200 % Grown

The README.md

LocalinksDependentAutoCompleteBundle

What for ?

LocalinksDependentAutoCompleteBundle allows, in a Sonata Admin form, to filter the results of a "sonata_type_model_autocomplete" field using the value of one (or more) other field present in the same form., (*1)

Installation

Step 1: Download the DependentAutoCompleteBundle

Using Composer, (*2)

Add the following to the "require" section of your composer.json file:, (*3)

    "localinks/dependentautocompletebundle": "dev-master"

And update your dependencies, (*4)

    php composer.phar update

Step 2: Enable the bundle

Registers the bundle in your app/AppKernel.php:, (*5)

<?php
...
public function registerBundles()
{
    $bundles = array(
        ...
        new Localinks\DependentAutoCompleteBundle\LocalinksDependentAutoCompleteBundle(),
        ...
    );
...

Usage

Imagine that you have an autocomplete "country" form field and another one: "city". You want your "city" field to be filtered according to the content of the "country" field., (*6)

For this you need to update your Admin class:, (*7)

<?php
...
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('country', 'sonata_type_model_autocomplete', array(
                'label' => 'Country',
                'property' => 'name',
                'attr' => array('data-dependent-id' => 'country'),
                'required' => false
            ))
            ->add('city', 'sonata_type_model_autocomplete', array(
                    'label' => 'City',
                    'dependencies' => array('country' => 'country_id'),
                    'property' => 'name',
                    'callback' => function (Admin $admin, $property, $value) {
                        $request = Request::createFromGlobals();
                        $dependencies = $request->get('dependencies');

                        $datagrid = $admin->getDatagrid();
                        $queryBuilder = $datagrid->getQuery();

                        if(!is_null($dependencies['country_id']) && $dependencies['country_id'] !== "") {
                            $queryBuilder
                                ->leftJoin($queryBuilder->getRootAlias() . '.country', 'cco')
                                ->where($queryBuilder->getRootAlias() . '.' .$property . ' LIKE :value')
                                ->andWhere('cco.id = :country_id')
                                ->setParameters(array(
                                    'country_id' => $dependencies['country_id'],
                                    'value' => $value . '%'
                                ));
                            }
                        else {
                            $queryBuilder
                                ->where($queryBuilder->getRootAlias() . '.' .$property . ' LIKE :value')
                                ->setParameters(array(
                                    'value' => $value . '%'
                                ));
                        }
                    },
                    'required' => false
            ))
        ;
    }

Please note the "data-dependent-id" value in the "country" field. This value will be used to identify the field. To avoid any error, use the same value than the field name (for instance "country")., (*8)

In the "city" field, add a "dependencies" option, as above. In this array, use the same "data-dependent-id" value than above as key, and any variable name you want as value., (*9)

You still have to create the "callback" function: the variable that you named above is accessible via the Request object (see example). From there, you can create the query necessary to filter your field., (*10)

Requirements

LocalinksDependentAutoCompleteBundle needs SonataAdminBundle in order to work, (*11)

License

This bundle is under GNU GENERAL PUBLIC LICENSE 3 license, (*12)

Author

Mathieu Hautenauve mathieu@proxymart.be www.localinks.be, (*13)

The Versions

15/01 2018

dev-master

9999999-dev https://github.com/localinks/LocalinksDependentAutoCompleteBundle

Extends Sonata Autocomplete Form Type and filters results using other form fields values

  Sources   Download

The Requires

 

filter symfony admin sonata