Forráskód Böngészése

Pass admin through to autocompleteItems callback:
~ modified HelperController::retrieveAutocompleteItemsAction() to pass $targetAdmin through to callback instead of $datagrid
~ updated documentation of the callback

caponica 10 éve
szülő
commit
a159bd9540

+ 2 - 2
Controller/HelperController.php

@@ -344,10 +344,10 @@ class HelperController
 
 
         if ($callback !== null) {
         if ($callback !== null) {
             if (!is_callable($callback)) {
             if (!is_callable($callback)) {
-                throw new \RuntimeException('Callback doesn`t contain callable function.');
+                throw new \RuntimeException('Callback does not contain callable function.');
             }
             }
 
 
-            call_user_func($callback, $datagrid, $property, $searchText);
+            call_user_func($callback, $targetAdmin, $property, $searchText);
         } else {
         } else {
             if (is_array($property)) {
             if (is_array($property)) {
                 // multiple properties
                 // multiple properties

+ 10 - 2
Resources/doc/reference/form_types.rst

@@ -176,15 +176,23 @@ model_manager
 
 
 callback
 callback
   defaults to null. Callable function that can be used to modify the query which is used to retrieve autocomplete items.
   defaults to null. Callable function that can be used to modify the query which is used to retrieve autocomplete items.
+  The callback should receive three parameters - the Admin instance, the property (or properties) defined as searchable and the 
+  search value entered by the user.
+  
+  From the ``$admin`` parameter it is possible to get the ``Datagrid`` and the ``Request``:
 
 
 .. code-block:: php
 .. code-block:: php
 
 
     $formMapper
     $formMapper
         ->add('category', 'sonata_type_model_autocomplete', array(
         ->add('category', 'sonata_type_model_autocomplete', array(
             'property'=>'title',
             'property'=>'title',
-            'callback' => function ($datagrid, $property, $value) {
+            'callback' => function ($admin, $property, $value) {
+                $datagrid = $admin->getDatagrid();
                 $queryBuilder = $datagrid->getQuery();
                 $queryBuilder = $datagrid->getQuery();
-                $queryBuilder->andWhere($queryBuilder->getRootAlias() . '.enabled=1 ');
+                $queryBuilder
+                    ->andWhere($queryBuilder->getRootAlias() . '.foo=:barValue')
+                    ->setParameter('barValue', $admin->getRequest()->get('bar'))
+                ;
                 $datagrid->setValue($property, null, $value);
                 $datagrid->setValue($property, null, $value);
             },
             },
         )
         )