Browse Source

Return classname when using the new form API (#4113)

The new form API expects classnames instead of form type names,
and a deprecation warning can be triggered when using
a filter that does not override the default field type.
François-Xavier de Guillebon 8 years ago
parent
commit
d8d42dc89d
3 changed files with 12 additions and 3 deletions
  1. 6 1
      Filter/Filter.php
  2. 3 1
      Tests/Datagrid/DatagridMapperTest.php
  3. 3 1
      Tests/Filter/FilterTest.php

+ 6 - 1
Filter/Filter.php

@@ -95,7 +95,12 @@ abstract class Filter implements FilterInterface
      */
     public function getFieldType()
     {
-        return $this->getOption('field_type', 'text');
+        // NEXT_MAJOR: Remove ternary and keep 'Symfony\Component\Form\Extension\Core\Type\TextType'
+        // (when requirement of Symfony is >= 2.8)
+        return $this->getOption('field_type', method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
+            ? 'Symfony\Component\Form\Extension\Core\Type\TextType'
+            : 'text'
+        );
     }
 
     /**

+ 3 - 1
Tests/Datagrid/DatagridMapperTest.php

@@ -104,7 +104,9 @@ class DatagridMapperTest extends \PHPUnit_Framework_TestCase
         $this->assertInstanceOf('Sonata\AdminBundle\Filter\FilterInterface', $filter);
         $this->assertSame('foo.name', $filter->getName());
         $this->assertSame('foo__name', $filter->getFormName());
-        $this->assertSame('text', $filter->getFieldType());
+        $this->assertSame(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
+            ? 'Symfony\Component\Form\Extension\Core\Type\TextType'
+            : 'text', $filter->getFieldType());
         $this->assertSame('fooLabel', $filter->getLabel());
         $this->assertSame(array('required' => false), $filter->getFieldOptions());
         $this->assertSame(array(

+ 3 - 1
Tests/Filter/FilterTest.php

@@ -19,7 +19,9 @@ class FilterTest extends \PHPUnit_Framework_TestCase
     {
         $filter = new FooFilter();
 
-        $this->assertSame('text', $filter->getFieldType());
+        $this->assertSame(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')
+            ? 'Symfony\Component\Form\Extension\Core\Type\TextType'
+            : 'text', $filter->getFieldType());
         $this->assertSame(array('required' => false), $filter->getFieldOptions());
         $this->assertNull($filter->getLabel());