Browse Source

Simplify the Form Field Extension

Thomas Rabaix 14 years ago
parent
commit
a91eb2e3c4

+ 0 - 61
DependencyInjection/AddFormExtensionCompilerPass.php

@@ -1,61 +0,0 @@
-<?php
-
-/*
- * This file is part of the Sonata project.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Sonata\AdminBundle\DependencyInjection;
-
-use Symfony\Component\DependencyInjection\Definition;
-use Symfony\Component\DependencyInjection\ContainerBuilder;
-use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
-use Symfony\Component\DependencyInjection\Reference;
-use Symfony\Component\DependencyInjection\ContainerInterface;
-
-/**
- * Add all dependencies to the Admin class, this avoid to write to many lines
- * in the configuration files.
- *
- * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
- */
-class AddFormExtensionCompilerPass implements CompilerPassInterface
-{
-    /**
-     * {@inheritDoc}
-     */
-    public function process(ContainerBuilder $container)
-    {
-        $ignores = array('form', 'csrf');
-
-        foreach ($container->findTaggedServiceIds('form.type') as $id => $attributes) {
-            $name = isset($attributes[0]['alias']) ? $attributes[0]['alias'] : false;
-
-            if (!$name || in_array($name, $ignores)) {
-                continue;
-            }
-
-            $definition = new Definition('Sonata\AdminBundle\Form\Extension\Field\Type\FormTypeFieldExtension', array($name));
-            $definition->addTag('form.type_extension', array('alias' => $name));
-
-            $container->setDefinition(sprintf('sonata.admin.form.extension.%s', $name), $definition);
-        }
-
-        // append the extension
-        $typeExtensions = array();
-
-        foreach ($container->findTaggedServiceIds('form.type_extension') as $serviceId => $tag) {
-            $alias = isset($tag[0]['alias'])
-                ? $tag[0]['alias']
-                : $serviceId;
-
-            $typeExtensions[$alias][] = $serviceId;
-        }
-
-        $container->getDefinition('form.extension')->replaceArgument(2, $typeExtensions);
-    }
-}

+ 4 - 11
Form/Extension/Field/Type/FormTypeFieldExtension.php

@@ -21,16 +21,6 @@ use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 
 class FormTypeFieldExtension extends AbstractTypeExtension
 {
-    protected $type;
-
-    /**
-     * @param srting $type
-     */
-    public function __construct($type)
-    {
-        $this->type = $type;
-    }
-
     public function buildForm(FormBuilder $builder, array $options)
     {
         $sonataAdmin = array(
@@ -74,7 +64,10 @@ class FormTypeFieldExtension extends AbstractTypeExtension
             $sonataAdmin = $form->getAttribute('sonata_admin');
             $sonataAdmin['value'] = $form->getData();
 
+            $view->set('sonata_admin_enabled', true);
             $view->set('sonata_admin', $sonataAdmin);
+        } else {
+            $view->set('sonata_admin_enabled', false);
         }
     }
 
@@ -85,7 +78,7 @@ class FormTypeFieldExtension extends AbstractTypeExtension
      */
     function getExtendedType()
     {
-        return $this->type;
+        return 'field';
     }
 
     /**

+ 4 - 0
Resources/config/form_types.xml

@@ -21,6 +21,10 @@
             <tag name="form.type" alias="sonata_type_immutable_array" />
         </service>
 
+        <service id="sonata.admin.form.extension.field" class="Sonata\AdminBundle\Form\Extension\Field\Type\FormTypeFieldExtension">
+            <tag name="form.type_extension" alias="field" />
+        </service>
+
     </services>
 
 </container>

+ 0 - 2
SonataAdminBundle.php

@@ -13,7 +13,6 @@ namespace Sonata\AdminBundle;
 use Symfony\Component\HttpKernel\Bundle\Bundle;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
 use Sonata\AdminBundle\DependencyInjection\AddDependencyCallsCompilerPass;
-use Sonata\AdminBundle\DependencyInjection\AddFormExtensionCompilerPass;
 
 class SonataAdminBundle extends Bundle
 {
@@ -22,6 +21,5 @@ class SonataAdminBundle extends Bundle
         parent::build($container);
 
         $container->addCompilerPass(new AddDependencyCallsCompilerPass());
-        $container->addCompilerPass(new AddFormExtensionCompilerPass());
     }
 }