Explorar o código

Add Admin.toString method

Thomas Rabaix %!s(int64=13) %!d(string=hai) anos
pai
achega
3563609bcf
Modificáronse 3 ficheiros con 34 adicións e 1 borrados
  1. 13 0
      Admin/Admin.php
  2. 1 1
      Resources/views/CRUD/base_edit.html.twig
  3. 20 0
      Tests/Admin/AdminTest.php

+ 13 - 0
Admin/Admin.php

@@ -2190,4 +2190,17 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     {
         return $this->routeBuilder;
     }
+
+    /**
+     * @param $object
+     * @return string
+     */
+    public function toString($object)
+    {
+        if (method_exists($object, '__toString')) {
+            return (string) $object;
+        }
+
+        return '';
+    }
 }

+ 1 - 1
Resources/views/CRUD/base_edit.html.twig

@@ -13,7 +13,7 @@ file that was distributed with this source code.
 
 {% block title %}
     {% if admin.id(object) %}
-        {{ "title_edit"|trans({'%name%': object }, 'SonataAdminBundle') }}
+        {{ "title_edit"|trans({'%name%': admin.toString(object) }, 'SonataAdminBundle') }}
     {% else %}
         {{ "title_create"|trans({}, 'SonataAdminBundle') }}
     {% endif %}

+ 20 - 0
Tests/Admin/AdminTest.php

@@ -14,6 +14,14 @@ namespace Sonata\AdminBundle\Tests\Admin;
 use Sonata\AdminBundle\Admin\Admin;
 use Sonata\AdminBundle\Route\RouteCollection;
 
+class FooTest_AdminTest
+{
+    public function __toString()
+    {
+        return 'salut';
+    }
+}
+
 class PostAdmin extends Admin
 {
     protected $metadataClass = null;
@@ -201,4 +209,16 @@ class AdminTest extends \PHPUnit_Framework_TestCase
 
         $this->assertEquals($uniqid, $admin->getUniqid());
     }
+
+    public function testToString()
+    {
+        $admin = new PostAdmin('sonata.post.admin.post', 'NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
+
+        $s = new \stdClass();
+
+        $this->assertEquals('', $admin->toString($s));
+
+        $s = new FooTest_AdminTest;
+        $this->assertEquals('Salut', $admin->toString($s));
+    }
 }