소스 검색

Merge pull request #2353 from caponica/autocomplete_callback_with_admin_v2

Pass admin through to autocompleteItems callback:
Andrej Hudec 11 년 전
부모
커밋
f831d0733f
2개의 변경된 파일12개의 추가작업 그리고 4개의 파일을 삭제
  1. 2 2
      Controller/HelperController.php
  2. 10 2
      Resources/doc/reference/form_types.rst

+ 2 - 2
Controller/HelperController.php

@@ -344,10 +344,10 @@ class HelperController
 
         if ($callback !== null) {
             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 {
             if (is_array($property)) {
                 // multiple properties

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

@@ -176,15 +176,23 @@ model_manager
 
 callback
   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
 
     $formMapper
         ->add('category', 'sonata_type_model_autocomplete', array(
             'property'=>'title',
-            'callback' => function ($datagrid, $property, $value) {
+            'callback' => function ($admin, $property, $value) {
+                $datagrid = $admin->getDatagrid();
                 $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);
             },
         )