Browse Source

Make export fields translateable (#3856)

Christian Gripp 8 years ago
parent
commit
99e2dc9545
3 changed files with 66 additions and 1 deletions
  1. 16 1
      Admin/AbstractAdmin.php
  2. 7 0
      Resources/doc/reference/action_export.rst
  3. 43 0
      Tests/Admin/AdminTest.php

+ 16 - 1
Admin/AbstractAdmin.php

@@ -584,7 +584,22 @@ abstract class AbstractAdmin implements AdminInterface, DomainObjectInterface
         $datagrid = $this->getDatagrid();
         $datagrid = $this->getDatagrid();
         $datagrid->buildPager();
         $datagrid->buildPager();
 
 
-        return $this->getModelManager()->getDataSourceIterator($datagrid, $this->getExportFields());
+        $fields = array();
+
+        foreach ($this->getExportFields() as $key => $field) {
+            $label = $this->getTranslationLabel($field, 'export', 'label');
+            $transLabel = $this->trans($label);
+
+            // NEXT_MAJOR: Remove this hack, because all field labels will be translated with the major release
+            // No translation key exists
+            if ($transLabel == $label) {
+                $fields[$key] = $field;
+            } else {
+                $fields[$transLabel] = $field;
+            }
+        }
+
+        return $this->getModelManager()->getDataSourceIterator($datagrid, $fields);
     }
     }
 
 
     /**
     /**

+ 7 - 0
Resources/doc/reference/action_export.rst

@@ -12,6 +12,13 @@ This document will cover the Export action and related configuration options.
 Basic configuration
 Basic configuration
 -------------------
 -------------------
 
 
+Translation
+~~~~~~~~~~~
+
+All field names are translated by default.
+An internal mechanism checks if a field matching the translator strategy label exists in the current translation file
+and will use the field name as a fallback.
+
 .. note::
 .. note::
 
 
     **TODO**:
     **TODO**:

+ 43 - 0
Tests/Admin/AdminTest.php

@@ -1816,6 +1816,49 @@ class AdminTest extends \PHPUnit_Framework_TestCase
         $this->assertSame('a query', $admin->createQuery('list'));
         $this->assertSame('a query', $admin->createQuery('list'));
     }
     }
 
 
+    public function testGetDataSourceIterator()
+    {
+        $datagrid = $this->getMock('Sonata\AdminBundle\Datagrid\DatagridInterface');
+        $datagrid->method('buildPager');
+
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $modelManager->method('getExportFields')->will($this->returnValue(array(
+            'field',
+            'foo',
+            'bar',
+        )));
+        $modelManager->expects($this->once())->method('getDataSourceIterator')
+            ->with($this->equalTo($datagrid), $this->equalTo(array(
+                'Feld' => 'field',
+                1 => 'foo',
+                2 => 'bar',
+            )));
+
+        $admin = $this->getMockBuilder('Sonata\AdminBundle\Admin\AbstractAdmin')
+            ->disableOriginalConstructor()
+            ->setMethods(array('getDatagrid', 'getTranslationLabel', 'trans'))
+            ->getMockForAbstractClass();
+        $admin->method('getDatagrid')->will($this->returnValue($datagrid));
+        $admin->setModelManager($modelManager);
+
+        $admin->expects($this->any())
+            ->method('getTranslationLabel')
+            ->will($this->returnCallback(function ($label, $context = '', $type = '') {
+                return $context.'.'.$type.'_'.$label;
+            }));
+        $admin->expects($this->any())
+            ->method('trans')
+            ->will($this->returnCallback(function ($label) {
+                if ($label == 'export.label_field') {
+                    return 'Feld';
+                }
+
+                return $label;
+            }));
+
+        $admin->getDataSourceIterator();
+    }
+
     private function createTagAdmin(Post $post)
     private function createTagAdmin(Post $post)
     {
     {
         $postAdmin = $this->getMockBuilder('Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin')
         $postAdmin = $this->getMockBuilder('Sonata\AdminBundle\Tests\Fixtures\Admin\PostAdmin')