Browse Source

Allow to specify admin when using urlsafeid twig helper

Romain Mouillard 10 years ago
parent
commit
891e7ca1a9

+ 2 - 2
Resources/doc/cookbook/recipe_customizing_a_mosaic_list.rst

@@ -63,9 +63,9 @@ The ``list_outer_rows_mosaic.html.twig`` is the name of one mosaic's tile. You s
 
     {% block sonata_mosaic_description %}
         {% if admin.isGranted('EDIT', object) and admin.hasRoute('edit') %}
-            <a href="{{ admin.generateUrl('edit', {'id' : object|sonata_urlsafeid }) }}">{{ meta.title|truncate(40) }}</a>
+            <a href="{{ admin.generateUrl('edit', {'id' : object|sonata_urlsafeid(admin) }) }}">{{ meta.title|truncate(40) }}</a>
         {% elseif admin.isGranted('SHOW', object) and admin.hasRoute('show') %}
-            <a href="{{ admin.generateUrl('show', {'id' : object|sonata_urlsafeid }) }}">{{ meta.title|truncate(40) }}</a>
+            <a href="{{ admin.generateUrl('show', {'id' : object|sonata_urlsafeid(admin) }) }}">{{ meta.title|truncate(40) }}</a>
         {% else %}
             {{ meta.title|truncate(40) }}
         {% endif %}

+ 2 - 2
Resources/views/CRUD/list_outer_rows_mosaic.html.twig

@@ -59,9 +59,9 @@ This template can be customized to match your needs. You should only extends the
 
                             {% block sonata_mosaic_description %}
                                 {% if admin.isGranted('EDIT', object) and admin.hasRoute('edit') %}
-                                    <a class="mosaic-inner-link" href="{{ admin.generateUrl('edit', {'id' : object|sonata_urlsafeid }) }}">{{ meta.title|truncate(40) }}</a>
+                                    <a class="mosaic-inner-link" href="{{ admin.generateUrl('edit', {'id' : object|sonata_urlsafeid(admin) }) }}">{{ meta.title|truncate(40) }}</a>
                                 {% elseif admin.isGranted('SHOW', object) and admin.hasRoute('show') %}
-                                    <a class="mosaic-inner-link" href="{{ admin.generateUrl('show', {'id' : object|sonata_urlsafeid }) }}">{{ meta.title|truncate(40) }}</a>
+                                    <a class="mosaic-inner-link" href="{{ admin.generateUrl('show', {'id' : object|sonata_urlsafeid(admin) }) }}">{{ meta.title|truncate(40) }}</a>
                                 {% else %}
                                     {{ meta.title|truncate(40) }}
                                 {% endif %}

+ 9 - 5
Twig/Extension/SonataAdminExtension.php

@@ -12,6 +12,7 @@
 namespace Sonata\AdminBundle\Twig\Extension;
 
 use Doctrine\Common\Util\ClassUtils;
+use Sonata\AdminBundle\Admin\AdminInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 use Sonata\AdminBundle\Exception\NoValueException;
 use Sonata\AdminBundle\Admin\Pool;
@@ -298,15 +299,18 @@ class SonataAdminExtension extends \Twig_Extension
     /**
      * Get the identifiers as a string that is save to use in an url.
      *
-     * @param object $model
+     * @param object         $model
+     * @param AdminInterface $admin
      *
      * @return string string representation of the id that is save to use in an url
      */
-    public function getUrlsafeIdentifier($model)
+    public function getUrlsafeIdentifier($model, AdminInterface $admin = null)
     {
-        $admin = $this->pool->getAdminByClass(
-            ClassUtils::getClass($model)
-        );
+        if (is_null($admin)) {
+            $admin = $this->pool->getAdminByClass(
+                ClassUtils::getClass($model)
+            );
+        }
 
         return $admin->getUrlsafeIdentifier($model);
     }