Преглед изворни кода

Merge pull request #791 from thanosp/short-object-description-refactoring

Added configurable template for the short-object-description
Thomas пре 13 година
родитељ
комит
75dfb335e7

+ 7 - 0
Admin/AdminInterface.php

@@ -540,4 +540,11 @@ interface AdminInterface
      * @return void
      */
     function setParent(AdminInterface $admin);
+    
+    /**
+     * @param string $name
+     *
+     * @return null|string
+     */
+    public function getTemplate($name);
 }

+ 11 - 3
Controller/HelperController.php

@@ -178,9 +178,17 @@ class HelperController
             }
         }
 
-        $description = sprintf('<a href="%s" target="new">%s</a>', $admin->generateUrl('edit', array('id' => $objectId)), $description);
-
-        return new Response($description);
+        $url = $admin->generateUrl('edit', array('id' => $objectId));
+        
+        $htmlOutput = $this->twig->render($admin->getTemplate('short_object_description'),
+            array(
+                'description' => $description,
+                'object' => $object,
+                'url' => $url
+            )
+        );
+
+        return new Response($htmlOutput);
     }
 
     /**

+ 11 - 10
DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php

@@ -246,16 +246,17 @@ class AddDependencyCallsCompilerPass implements CompilerPassInterface
 
         // make sure the default templates are defined
         $definedTemplates = array_merge(array(
-            'user_block'        => 'SonataAdminBundle:Core:user_block.html.twig',
-            'layout'            => 'SonataAdminBundle::standard_layout.html.twig',
-            'ajax'              => 'SonataAdminBundle::ajax_layout.html.twig',
-            'dashboard'         => 'SonataAdminBundle:Core:dashboard.html.twig',
-            'list'              => 'SonataAdminBundle:CRUD:list.html.twig',
-            'show'              => 'SonataAdminBundle:CRUD:show.html.twig',
-            'edit'              => 'SonataAdminBundle:CRUD:edit.html.twig',
-            'history'           => 'SonataAdminBundle:CRUD:history.html.twig',
-            'history_revision'  => 'SonataAdminBundle:CRUD:history_revision.html.twig',
-            'action'            => 'SonataAdminBundle:CRUD:action.html.twig',
+            'user_block'               => 'SonataAdminBundle:Core:user_block.html.twig',
+            'layout'                   => 'SonataAdminBundle::standard_layout.html.twig',
+            'ajax'                     => 'SonataAdminBundle::ajax_layout.html.twig',
+            'dashboard'                => 'SonataAdminBundle:Core:dashboard.html.twig',
+            'list'                     => 'SonataAdminBundle:CRUD:list.html.twig',
+            'show'                     => 'SonataAdminBundle:CRUD:show.html.twig',
+            'edit'                     => 'SonataAdminBundle:CRUD:edit.html.twig',
+            'history'                  => 'SonataAdminBundle:CRUD:history.html.twig',
+            'history_revision'         => 'SonataAdminBundle:CRUD:history_revision.html.twig',
+            'action'                   => 'SonataAdminBundle:CRUD:action.html.twig',
+            'short_object_description' => 'SonataAdminBundle:Helper:short-object-description.html.twig',
         ), $definedTemplates);
 
         $definition->addMethodCall('setTemplates', array($definedTemplates));

+ 1 - 0
DependencyInjection/Configuration.php

@@ -135,6 +135,7 @@ class Configuration implements ConfigurationInterface
                         ->scalarNode('history')->defaultValue('SonataAdminBundle:CRUD:history.html.twig')->cannotBeEmpty()->end()
                         ->scalarNode('history_revision')->defaultValue('SonataAdminBundle:CRUD:history_revision.html.twig')->cannotBeEmpty()->end()
                         ->scalarNode('action')->defaultValue('SonataAdminBundle:CRUD:action.html.twig')->cannotBeEmpty()->end()
+                        ->scalarNode('short_object_description')->defaultValue('SonataAdminBundle:Helper:short-object-description.html.twig')->cannotBeEmpty()->end()
                     ->end()
                 ->end()
             ->end()

+ 4 - 0
Resources/doc/reference/templates.rst

@@ -16,6 +16,9 @@ By default, an Admin class uses a set of templates, it is possible to tweak the
             show:    SonataAdminBundle:CRUD:show.html.twig
             edit:    SonataAdminBundle:CRUD:edit.html.twig
             history:  SonataAdminBundle:CRUD:history.html.twig
+            
+            # default values of helper templates
+            short_object_description: SonataAdminBundle:Helper:short-object-description.html.twig
 
 
 Usage of each template :
@@ -27,6 +30,7 @@ Usage of each template :
 * show : the template to use for the show action
 * edit : the template to use for the edit and create action
 * history : the template to use for the history / audit action
+* short_object_description: used to represent the entity in one-to-one/many-to-one relations
 
 The default values will be set only if the ``Admin::setTemplates`` is not called by the Container.
 

+ 1 - 0
Resources/views/Helper/short-object-description.html.twig

@@ -0,0 +1 @@
+<a href="{{ url }}" target="new">{{ description }}</a>

+ 11 - 1
Tests/Controller/AdminHelperTest.php

@@ -93,8 +93,11 @@ class AdminHelperTest extends \PHPUnit_Framework_TestCase
 
     public function testgetShortObjectDescriptionActionObject()
     {
+        $mockTemplate = 'AdminHelperTest:mock-short-object-description.html.twig';
+        
         $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
         $admin->expects($this->once())->method('setUniqid');
+        $admin->expects($this->once())->method('getTemplate')->will($this->returnValue($mockTemplate));
         $admin->expects($this->once())->method('getObject')->will($this->returnValue(new AdminControllerHelper_Foo));
         $admin->expects($this->once())->method('generateUrl')->will($this->returnCallback(function($name, $parameters) {
             if ($name != 'edit') {
@@ -110,7 +113,14 @@ class AdminHelperTest extends \PHPUnit_Framework_TestCase
         $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
         $container->expects($this->any())->method('get')->will($this->returnValue($admin));
 
-        $twig = new Twig;
+        $twig = $this->getMock('Twig_Environment');
+        
+        $twig->expects($this->once())->method('render')
+            ->with($mockTemplate)
+            ->will($this->returnCallback(function($templateName, $templateParams) {
+                return sprintf('<a href="%s" target="new">%s</a>', $templateParams['url'], $templateParams['description']);
+            }));
+            
         $request = new Request(array(
             'code'     => 'sonata.post.admin',
             'objectId' => 42,