ソースを参照

[Order] Make Form::types and FormView::types use the same order (Parent > Child)

Victor Berchet 14 年 前
コミット
b709551252

+ 5 - 5
src/Symfony/Bridge/Twig/Extension/FormExtension.php

@@ -226,12 +226,12 @@ class FormExtension extends \Twig_Extension
 
         $blocks = $this->getBlocks($view);
         $types = $view->get('types');
-        array_unshift($types, '_'.$view->get('proto_id', $view->get('id')));
+        $types[] = '_'.$view->get('proto_id', $view->get('id'));
 
-        foreach ($types as &$block) {
-            $block = $block.'_'.$section;
+        for ($i = count($types) - 1; $i >= 0; $i--) {
+            $types[$i] .= '_'.$section;
 
-            if (isset($blocks[$block])) {
+            if (isset($blocks[$types[$i]])) {
 
                 $this->varStack[$view] = array_replace(
                     $view->all(),
@@ -239,7 +239,7 @@ class FormExtension extends \Twig_Extension
                     $variables
                 );
 
-                $html = $this->template->renderBlock($block, $this->varStack[$view], $blocks);
+                $html = $this->template->renderBlock($types[$i], $this->varStack[$view], $blocks);
 
                 if ($mainTemplate) {
                     $view->setRendered();

+ 12 - 16
src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

@@ -202,29 +202,25 @@ class FormHelper extends Helper
         }
 
         $template = null;
-        $blocks = $view->get('types');
-        array_unshift($blocks, '_'.$view->get('proto_id', $view->get('id')));
+        $types = $view->get('types');
+        $types[] = '_'.$view->get('proto_id', $view->get('id'));
 
-        foreach ($blocks as &$block) {
-            $block = $block.'_'.$section;
-            $template = $this->lookupTemplate($block);
+        for ($i = count($types) - 1; $i >= 0; $i--) {
+            $types[$i] .= '_'.$section;
+            $template = $this->lookupTemplate($types[$i]);
 
             if ($template) {
-                break;
-            }
-        }
-
-        if (!$template) {
-            throw new FormException(sprintf('Unable to render form as none of the following blocks exist: "%s".', implode('", "', $blocks)));
-        }
+                $html = $this->render($view, $template, $variables);
 
-        $html = $this->render($view, $template, $variables);
+                if ($mainTemplate) {
+                    $view->setRendered();
+                }
 
-        if ($mainTemplate) {
-            $view->setRendered();
+                return $html;
+            }
         }
 
-        return $html;
+        throw new FormException(sprintf('Unable to render form as none of the following blocks exist: "%s".', implode('", "', $types)));
     }
 
     public function render(FormView $view, $template, array $variables = array())

+ 1 - 6
src/Symfony/Component/Form/Extension/Core/Type/FieldType.php

@@ -75,11 +75,6 @@ class FieldType extends AbstractType
             $fullName = $name;
         }
 
-        $types = array();
-        foreach (array_reverse((array) $form->getTypes()) as $type) {
-            $types[] = $type->getName();
-        }
-
         $view
             ->set('form', $view)
             ->set('id', $id)
@@ -95,7 +90,7 @@ class FieldType extends AbstractType
             ->set('label', $form->getAttribute('label'))
             ->set('multipart', false)
             ->set('attr', array())
-            ->set('types', $types)
+            ->set('types', array_map(function ($type) { return $type->getName(); }, $form->getTypes()))
         ;
     }
 

+ 1 - 1
src/Symfony/Component/Form/Form.php

@@ -921,7 +921,7 @@ class Form implements \IteratorAggregate, FormInterface
         if (null !== $prototype = $view->get('prototype')) {
             $protoView = $prototype->getForm()->createView($view);
             $protoTypes = $protoView->get('types');
-            array_unshift($protoTypes, 'prototype');
+            $protoTypes[] = 'prototype';
             $childViews[$prototype->getName()] = $protoView
                 ->set('types', $protoTypes)
                 ->set('proto_id', $view->get('id').'_prototype');