浏览代码

Allows an admin tag to configure Admin class dependencies.
Add documentation about Label Strategies

Thomas Rabaix 13 年之前
父节点
当前提交
1997ffaec8

+ 33 - 0
DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php

@@ -50,6 +50,7 @@ class AddDependencyCallsCompilerPass implements CompilerPassInterface
                     $definition->replaceArgument(2, 'SonataAdminBundle:CRUD');
                 }
 
+                $this->applyConfigurationFromAttribute($definition, $attributes);
                 $this->applyDefaults($container, $id, $attributes);
 
                 $arguments = $definition->getArguments();
@@ -110,6 +111,38 @@ class AddDependencyCallsCompilerPass implements CompilerPassInterface
         $routeLoader->replaceArgument(1, $admins);
     }
 
+    /**
+     * This method read the attribute keys and configure admin class to use the related dependency
+     *
+     * @param \Symfony\Component\DependencyInjection\Definition $definition
+     * @param array $attributes
+     */
+    public function applyConfigurationFromAttribute(Definition $definition, array $attributes) {
+        $keys = array(
+            'model_manager',
+            'form_contractor',
+            'show_builder',
+            'list_builder',
+            'datagrid_builder',
+            'translator',
+            'configuration_pool',
+            'router',
+            'validator',
+            'security_handler',
+            'menu_factory',
+            'label_translator_strategy',
+        );
+
+        foreach ($keys as $key) {
+            $method = 'set'.$this->camelize($key);
+            if (!isset($attributes[$key]) || $definition->hasMethodCall($method)) {
+                continue;
+            }
+
+            $definition->addMethodCall($method, array(new Reference($attributes[$key])));
+        }
+    }
+
     /**
      * Apply the default values required by the AdminInterface to the Admin service definition
      *

+ 35 - 2
Resources/doc/reference/translation.rst

@@ -69,6 +69,39 @@ By default, the label is the the field name. However a label can be defined as a
     {
         public function configureFormFields(FormMapper $formMapper)
         {
-            $formMapper->add('name', null, array('required' => false, 'label' => 'label.name'));
+            $formMapper->add('isValid', null, array('required' => false, 'label' => 'label.is_valid'));
         }
-    }
+    }
+
+There is another option for rapid prototyping or to avoid spending too much time adding the ``label`` key to all option
+fields: ``Label Strategies``. By default labels are generated by using by using a simple rule ::
+
+    isValid => Isvalid
+
+This is not perfect and hard to read. So in order to solve this, the ``AdminBundle`` comes with different key label generation
+strategies:
+
+* ``sonata.admin.label.strategy.form_component`` : The default behavior from the Form Component - ``isValid`` => ``Isvalid``)
+* ``sonata.admin.label.strategy.underscore`` : Add undescore to the label  - ``isValid`` => ``label_is_valid``
+* ``sonata.admin.label.strategy.native`` : Make the string human readable readable - ``isValid`` => ``Is Valid``
+* ``sonata.admin.label.strategy.noop`` : does not alter the string - ``isValid`` => ``isValid``
+
+``sonata.admin.label.strategy.underscore`` will be better for i18n applications and ``sonata.admin.label.strategy.native`
+will be better for native language based on the field name. So it is possible to start with the ``native`` strategy and then
+when the application need to be translated using generic keys the configuration can be switched to used the ``sonata.admin.label.strategy.underscore``.
+
+The strategy can be quickly configured when the Admin class is registered into the Container:
+
+.. code-block:: xml
+
+        <service id="ekino.project.admin.security_feed" class="AcmeBundle\ProjectBundle\Admin\ProjectAdmin">
+            <tag name="sonata.admin" manager_type="orm" group="Project" label="Project" label_translator_strategy="sonata.admin.label.strategy.native" />
+            <argument />
+            <argument>AcmeBundle\ProjectBundle\Entity\ProjectFeed</argument>
+            <argument />
+        </service>
+
+.. note::
+
+    In all cases the label will be used by the ``Translator``. The strategy is just a quick way to generate translable keys
+    depends on the project's requirements.