Explorar o código

[Form] Fix twig theme inheritance

Victor Berchet %!s(int64=14) %!d(string=hai) anos
pai
achega
bee505a4bf

+ 4 - 2
src/Symfony/Bridge/Twig/Extension/FormExtension.php

@@ -248,16 +248,18 @@ class FormExtension extends \Twig_Extension
     {
         if (!$this->templates->contains($view)) {
             // defaults
-            $all = $this->resources;
+            $all = array();
 
             // themes
             $parent = $view;
             do {
                 if (isset($this->themes[$parent])) {
-                    $all = array_merge($all, $this->themes[$parent]);
+                    $all = array_merge($this->themes[$parent], $all);
                 }
             } while ($parent = $parent->getParent());
 
+            $all = array_merge($this->resources, $all);
+
             $templates = array();
             foreach ($all as $resource) {
                 if (!$resource instanceof \Twig_Template) {

+ 42 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/FormExtensionDivLayoutTest.php

@@ -48,6 +48,48 @@ class FormExtensionDivLayoutTest extends AbstractDivLayoutTest
         $this->extension->initRuntime($environment);
     }
 
+    public function testThemeInheritance()
+    {
+        $child = $this->factory->createNamedBuilder('form', 'child')
+            ->add('field', 'text')
+            ->getForm();
+
+        $view = $this->factory->createNamedBuilder('form', 'parent')
+            ->add('field', 'text')
+            ->getForm()
+            ->add($child)
+            ->createView()
+        ;
+
+        $this->extension->setTheme($view, array('parent_label.html.twig'));
+        $this->extension->setTheme($view['child'], array('child_label.html.twig'));
+
+        $this->assertWidgetMatchesXpath($view, array(),
+'/div
+    [
+        ./input[@type="hidden"]
+        /following-sibling::div
+            [
+                ./label[.="parent"]
+                /following-sibling::input[@type="text"]
+            ]
+        /following-sibling::div
+            [
+                ./label
+                /following-sibling::div
+                    [
+                        ./div
+                            [
+                                ./label[.="child"]
+                                /following-sibling::input[@type="text"]
+                            ]
+                    ]
+            ]
+    ]
+'
+        );
+    }
+
     protected function renderEnctype(FormView $view)
     {
         return (string)$this->extension->renderEnctype($view);

+ 3 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/child_label.html.twig

@@ -0,0 +1,3 @@
+{% block field_label %}
+    <label>child</label>
+{% endblock field_label %}

+ 3 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/parent_label.html.twig

@@ -0,0 +1,3 @@
+{% block field_label %}
+    <label>parent</label>
+{% endblock field_label %}