2

I am trying to create linked drop down using sonata admin bundle.

There are

product

product_categories (many to many with product)

product_features (linked with product_categories) (Cardinality: many to many with product and many to one with category i.e. a category can have many features but a feature is link with one category)

Everything is working fine and one multi select enabled combo(for categories) field and another group of check boxes(for product_features) field are being displayed using this

In product entry form:

  protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper            
    ->add('category', 'sonata_type_model', array('required' => false, 'expanded' => false, 'multiple' => true, 'label' => 'Choose your categories'))
   ->add('features', 'sonata_type_model', array('required' => false, 'expanded' => true, 'multiple' => true, 'label' => 'Choose Features'))

But what I want is when I select value in first combo i.e. categories, the second list of checkboxes i.e. features should be dynamically loaded accordingly (only features linked with that category)

As the first select box is multi select enabled it will be even better is second list is cumulative i.e when category 1 is selected shows features related to category1 then when category 2 is selected(with 1 also selected) it shows features linked with cat 1 and cat 2

How can I do it?

I have been googing and only good answer I found was this 2 years old solution which seems quite long and bit old. Are there any other solutions? Things must have changed in last 2 years as several versions of sonata-admin were released during that time.

How to use Ajax within Sonata Admin forms?

Any help? Thanks in advance.

3
  • Any ways i finally solved the problem myself.The question earn me a tumbleweed badge however If any body needs it can ping me. Dec 10, 2014 at 12:21
  • 2
    hi, how did you resolved this issue, thanks
    – NoOneElse
    Nov 12, 2015 at 16:53
  • @TheGooooogle Would be nice to know how you handled this case. Sep 18, 2017 at 14:48

1 Answer 1

0

I had a lot of stuggle with this, but I think I came out with a practicable workaround. Hope this saved someone time in the future.

My case is: I have multiple Company entities with 1 to n ContactPerson entities. When I create a new Job, I have to select the Company and then I want to filter the list of ContactPersons by the selected company. As you can see, its working. Depending on the selected Company the matching list if ContactPersons is loaded:

Filter screenshot 1 Filter screenshot 2

So get this working, I just disabled the mapping flag for Customer and ContactPerson to map the data by myself in postPersist and postUpdate in my admin class (extends BaseAdmin).

Then I use a sonata_type_choice_field_mask for all my Company entities. I build a data-array by myself. After that, I add a prefiltered (by passing a matching filter) ContactPerson dropdown using the type sonata_type_model.

This is working very well - but of course, it will fail if you have thousands of companies and customers. In my case this will not happed so this is a practicable solution.

Code (removed unnecessary stuff):

<?php

namespace AppBundle\Admin;

use AppBundle\Entity\Customer;
use AppBundle\Entity\Job;
use AppBundle\Form\Types;
use AppBundle\Repository\CustomerRepository;
use AppBundle\Service\EntityManager;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;

class JobAdmin extends BaseAdmin
{
    const CONTACT_PERSON_DROPDOWN_PREFIX = 'contactPersons';

    /**
     * @param Job $job
     * @return null|string
     */
    protected function getPreselectedCustomerId(Job $job)
    {
        if ($job) {
            $customer = $job->getCustomer();

            if ($customer) {
                $customerId = $customer->getId();

                return $customerId;
            }
        }

        return null;
    }

    /**
     * @return \Doctrine\ORM\EntityRepository
     */
    protected function getEnabledCustomers()
    {
        $customerRepository = $this->getCustomerRepository();
        $customers          = $customerRepository->findByEnabled();

        return $customers;
    }

    /**
     * @param $enabledCustomers
     * @return array
     */
    protected function getFilterMap($enabledCustomers)
    {
        $filterMap = [];

        /**
         * @var $customer Customer
         */
        foreach ($enabledCustomers as $customer) {
            $customerId             = $customer->getId();
            $fieldName              = self::CONTACT_PERSON_DROPDOWN_PREFIX . $customerId;
            $filterMap[$customerId] = [$fieldName];
        }

        return $filterMap;
    }

    /**
     * @param $enabledCustomers
     * @return array
     */
    protected function getCustomerDropDown($enabledCustomers)
    {
        $customerDropDown = [];

        /**
         * @var $customer Customer
         */
        foreach ($enabledCustomers as $customer) {
            $customerId              = $customer->getId();
            $name                    = $customer->getName();
            $customerDropDown[$name] = $customerId;
        }

        return $customerDropDown;
    }

    /**
     * @param Customer $customer
     * @return \Doctrine\ORM\QueryBuilder
     */
    protected function getFilterQueryForCustomer(Customer $customer)
    {
        $entityManager      = $this->getEntityManager();
        $contactPersonQuery = $entityManager->createQueryBuilder('cp')
            ->select('cp')
            ->from('AppBundle:ContactPerson', 'cp')
            ->where('cp.customer = :customer')
            ->setParameter('customer', $customer)
            ->orderBy('cp.name', 'ASC')
        ;

        return $contactPersonQuery;
    }

    /**
     * @param FormMapper $formMapper
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $job              = $this->getSubject();
        $enabledCustomers = $this->getEnabledCustomers();
        $filterMap        = $this->getFilterMap($enabledCustomers);
        $customerDropDown = $this->getCustomerDropDown($enabledCustomers);
        $preselectedId    = $this->getPreselectedCustomerId($job);

        $formMapper
            ->tab('BaseData')
                ->with('JobData')
                    ->add('customer', 'sonata_type_choice_field_mask', array(
                        'choices'      => $customerDropDown,
                        'data'         => $preselectedId,
                        'map'          => $filterMap,
                        'by_reference' => false,
                        'required'     => true,
                        'mapped'       => false,
                        )
                    )
                ->end()
            ->end()
        ;

        /**
         * @var $customer Customer
         */
        foreach ($enabledCustomers as $customer) {
            $customerId  = $customer->getId();
            $fieldName   = self::CONTACT_PERSON_DROPDOWN_PREFIX . $customerId;
            $filterQuery = $this->getFilterQueryForCustomer($customer);

            $formMapper
                ->tab('BaseData')
                    ->with('JobData')
                        ->add($fieldName, 'sonata_type_model', array(
                            'class'  => 'AppBundle\Entity\ContactPerson',
                            'query'  => $filterQuery,
                            'mapped' => false,
                            'label'  => 'Contact Person'
                        ))
                    ->end()
                ->end()
            ;
        }

        $formMapper
            ->tab('BaseData')
                ->with('JobData')
                    ->add('name')
                    // Add more fields
                ->end()
            ->end()
        ;
    }

    /**
     * @param EntityManager $entityManager
     * @return \Doctrine\ORM\EntityRepository
     */
    protected function getCustomerRepository(EntityManager $entityManager = null)
    {
        if (!$entityManager) {
            $entityManager = $this->getEntityManager();
        }

        $customerRepository = $entityManager->getRepository(Customer::class);

        return $customerRepository;
    }

    /**
     * @return EntityManager
     */
    protected function getEntityManager()
    {
        $entityManager = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();

        return $entityManager;
    }

    /**
     * @param Job $job
     */
    protected function mapUnmappedFields (Job $job)
    {
        $customerId = $this->getForm()->get('customer')->getData();

        if ($customerId)
        {
            $entityManager      = $this->getEntityManager();
            $contactPerson      = $this->getForm()->get(self::CONTACT_PERSON_DROPDOWN_PREFIX.$customerId)->getData();
            $customerRepository = $this->getCustomerRepository($entityManager);
            $customer           = $customerRepository->findById($customerId);

            if ($contactPerson) {
                $job->setContactPerson($contactPerson);
            }

            if ($customer) {
                $job->setCustomer($customer);
            }

            $entityManager->persist($job);
            $entityManager->flush($job);
        }
    }

    /**
     * @param mixed $object
     */
    public function postPersist($object)
    {
        $this->mapUnmappedFields($object);

        parent::postPersist($object);
    }

    /**
     * @param mixed $object
     */
    public function postUpdate($object)
    {
        $this->mapUnmappedFields($object);

        parent::postUpdate($object);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.