Pārlūkot izejas kodu

Configure search result link in admin (#3772)

Christian Gripp 9 gadi atpakaļ
vecāks
revīzija
9ab0c5ce2a

+ 21 - 0
Admin/AbstractAdmin.php

@@ -416,6 +416,13 @@ abstract class AbstractAdmin implements AdminInterface, DomainObjectInterface
 
 
     protected $cacheIsGranted = array();
     protected $cacheIsGranted = array();
 
 
+    /**
+     * Action list for the search result.
+     *
+     * @var string[]
+     */
+    protected $searchResultActions = array('edit', 'show');
+
     protected $listModes = array(
     protected $listModes = array(
         'list' => array(
         'list' => array(
             'class' => 'fa fa-list fa-fw',
             'class' => 'fa fa-list fa-fw',
@@ -2823,6 +2830,20 @@ abstract class AbstractAdmin implements AdminInterface, DomainObjectInterface
     /**
     /**
      * @param FormMapper $form
      * @param FormMapper $form
      */
      */
+    final public function getSearchResultLink($object)
+    {
+        foreach ($this->searchResultActions as $action) {
+            if ($this->hasRoute($action) && $this->isGranted(strtoupper($action), $object)) {
+                return $this->generateObjectUrl($action, $object);
+            }
+        }
+
+        return;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
     protected function configureFormFields(FormMapper $form)
     protected function configureFormFields(FormMapper $form)
     {
     {
     }
     }

+ 10 - 0
Admin/AdminInterface.php

@@ -1050,4 +1050,14 @@ interface AdminInterface
 //     * @return bool
 //     * @return bool
 //     */
 //     */
 //    public function hasAccess($action, $object = null);
 //    public function hasAccess($action, $object = null);
+
+    //TODO: uncomment this method for 4.0
+    /*
+     * Returns the result link for an object.
+     *
+     * @param mixed $object
+     *
+     * @return string|null
+     */
+    //public function getSearchResultLink($object)
 }
 }

+ 1 - 0
CHANGELOG.md

@@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
 ## [3.x]
 ## [3.x]
 ### Added
 ### Added
 - Extract the breadcrumbs building part of the `AbstractAdmin` to a separate class
 - Extract the breadcrumbs building part of the `AbstractAdmin` to a separate class
+- Added `AbstractAdmin::getSearchResultLink` method
 
 
 ## [3.1.0](https://github.com/sonata-project/SonataAdminBundle/compare/3.0.0...3.1.0) - 2016-05-17
 ## [3.1.0](https://github.com/sonata-project/SonataAdminBundle/compare/3.0.0...3.1.0) - 2016-05-17
 ### Added
 ### Added

+ 22 - 0
Resources/doc/reference/search.rst

@@ -55,6 +55,28 @@ You can also configure the block template per admin while defining the admin:
               </call>
               </call>
           </service>
           </service>
 
 
+Configure the default search result action
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+In general the search result generates a link to the edit action of an item or is using the show action, if the edit
+route is disabled or you haven't the required permission. You can change this behaviour by overriding the
+``searchResultActions`` property. The defined action list will we checked successive until a route with the required
+permissions exists. If no route is found, the item will be displayed as a text.
+
+.. code-block:: php
+
+    <?php
+    // src/AppBundle/Admin/PersonAdmin.php
+
+    class PersonAdmin extends AbstractAdmin
+    {
+        // ...
+
+        protected $searchResultActions = array('edit', 'show');
+
+        // ...
+    }
+
 Performance
 Performance
 -----------
 -----------
 
 

+ 3 - 2
Resources/views/Block/block_search_result.html.twig

@@ -40,8 +40,9 @@ file that was distributed with this source code.
                 <div class="box-body no-padding">
                 <div class="box-body no-padding">
                     <ul class="nav nav-stacked sonata-search-result-list">
                     <ul class="nav nav-stacked sonata-search-result-list">
                         {% for result in pager.getResults() %}
                         {% for result in pager.getResults() %}
-                            {% if admin.hasRoute('edit') and admin.isGranted('EDIT', result) %}
-                                <li><a href="{{ admin.generateObjectUrl('edit', result) }}">{{ admin.toString(result) }}</a></li>
+                            {% set link = admin.getSearchResultLink(result) %}
+                            {% if link %}
+                                <li><a href="{{ link }}">{{ admin.toString(result) }}</a></li>
                             {% else %}
                             {% else %}
                                 <li><a>{{ admin.toString(result) }}</a></li>
                                 <li><a>{{ admin.toString(result) }}</a></li>
                             {% endif %}
                             {% endif %}