Skip to content

Commit

Permalink
Configure search result link in admin (sonata-project#3772)
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 authored and soullivaneuh committed May 23, 2016
1 parent 83f486c commit 9ab0c5c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
21 changes: 21 additions & 0 deletions Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,13 @@ abstract class AbstractAdmin implements AdminInterface, DomainObjectInterface

protected $cacheIsGranted = array();

/**
* Action list for the search result.
*
* @var string[]
*/
protected $searchResultActions = array('edit', 'show');

protected $listModes = array(
'list' => array(
'class' => 'fa fa-list fa-fw',
Expand Down Expand Up @@ -2823,6 +2830,20 @@ public function getDashboardActions()
/**
* @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)
{
}
Expand Down
10 changes: 10 additions & 0 deletions Admin/AdminInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -1050,4 +1050,14 @@ public function checkAccess($action, $object = null);
// * @return bool
// */
// 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 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [3.x]
### Added
- 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
### Added
Expand Down
22 changes: 22 additions & 0 deletions Resources/doc/reference/search.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ You can also configure the block template per admin while defining the admin:
</call>
</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
-----------

Expand Down
5 changes: 3 additions & 2 deletions Resources/views/Block/block_search_result.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ file that was distributed with this source code.
<div class="box-body no-padding">
<ul class="nav nav-stacked sonata-search-result-list">
{% 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 %}
<li><a>{{ admin.toString(result) }}</a></li>
{% endif %}
Expand Down

0 comments on commit 9ab0c5c

Please sign in to comment.