Forráskód Böngészése

Add TranslatableChoiceType

Thomas Rabaix 13 éve
szülő
commit
c719b5e70e

+ 75 - 0
Form/Type/TranslatableChoiceType.php

@@ -0,0 +1,75 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (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\Form\Type;
+
+use Symfony\Component\Translation\TranslatorInterface;
+use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
+use Symfony\Component\Form\FormInterface;
+use Symfony\Component\Form\FormView;
+use Symfony\Component\Form\FormBuilder;
+
+class TranslatableChoiceType extends ChoiceType
+{
+    protected $translator;
+
+    /**
+     * @param \Symfony\Component\Translation\TranslatorInterface $translator
+     */
+    public function __construct(TranslatorInterface $translator)
+    {
+        $this->translator = $translator;
+    }
+
+    /**
+     * @param array $options
+     * @return array
+     */
+    public function getDefaultOptions(array $options)
+    {
+        $multiple = isset($options['multiple']) && $options['multiple'];
+        $expanded = isset($options['expanded']) && $options['expanded'];
+
+        return array(
+            'multiple'          => false,
+            'expanded'          => false,
+            'choice_list'       => null,
+            'choices'           => array(),
+            'preferred_choices' => array(),
+            'catalogue'         => 'messages',
+            'empty_data'        => $multiple || $expanded ? array() : '',
+            'empty_value'       => $multiple || $expanded || !isset($options['empty_value']) ? null : '',
+            'error_bubbling'    => false,
+        );
+    }
+
+    public function buildForm(FormBuilder $builder, array $options)
+    {
+        parent::buildForm($builder, $options);
+
+        $builder->setAttribute('catalogue', $options['catalogue']);
+    }
+
+    public function buildView(FormView $view, FormInterface $form)
+    {
+        parent::buildView($view, $form);
+
+        $choices = array();
+        $catalogue = $form->getAttribute('catalogue');
+
+        foreach ($view->get('choices') as $name => $value) {
+            $choices[$name] = $this->translator->trans($value, array(), $catalogue);
+        }
+
+        $view->set('choices', $choices);
+    }
+}

+ 4 - 0
Resources/config/form_types.xml

@@ -29,7 +29,11 @@
 
         <service id="sonata.admin.form.type.boolean" class="Sonata\AdminBundle\Form\Type\BooleanType">
             <tag name="form.type" alias="sonata_type_boolean" />
+            <argument type="service" id="translator" />
+        </service>
 
+        <service id="sonata.admin.form.type.translatable_choice" class="Sonata\AdminBundle\Form\Type\TranslatableChoiceType">
+            <tag name="form.type" alias="sonata_type_translatable_choice" />
             <argument type="service" id="translator" />
         </service>
 

+ 1 - 0
Resources/doc/index.rst

@@ -33,6 +33,7 @@ Reference Guide
    reference/dashboard
    reference/routing
    reference/saving_hooks
+   reference/form_types
    reference/conditional_validation
    reference/templates
    reference/translation

+ 102 - 0
Resources/doc/reference/form_types.rst

@@ -0,0 +1,102 @@
+Form Types
+==========
+
+Admin related form types
+------------------------
+
+// todo
+
+
+Other form types
+----------------
+
+The bundle comes with some handy form types which are available from outside the scope of the ``SonataAdminBundle``::
+
+sonata_type_immutable_array
+^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+The ``Immutable Array`` allows to edit an array property by defining a type per key.
+
+The type has a ``keys`` parameter which contains the definition for each key. A definition is an array with 3 options :
+* the key name
+* the type : a type name or the a ``FormType`` instance
+* the related type parameters : please refer to the related form documentation.
+
+Let's say a ``Page`` have a options property with some fixed key-pair values, each value has a type different : integer,
+url, or string for instance.
+
+.. code-block:: php
+
+    <?php
+    class Page
+    {
+        protected $options = array(
+            'ttl'       => 1,
+            'redirect'  => ''
+        );
+
+        public function setOptions(array $options)
+        {
+            $this->options = $options;
+        }
+
+        public function getOptions()
+        {
+            return $this->options;
+        }
+    }
+
+Now, the property can be edited by setting a type for each type
+
+.. code-block:: php
+
+        <?php
+        $form->add('options', 'sonata_type_immutable_array', array(
+            'keys' => array(
+                array('ttl',        'text', array('required' => false)),
+                array('redirect',   'url',  array('required' => true)),
+            )
+        ));
+
+
+sonata_type_boolean
+^^^^^^^^^^^^^^^^^^^
+
+The ``boolean`` type is a specialized ``ChoiceType`` where the choices list is fixed to 'yes' or 'no'.
+
+
+sonata_type_translatable_choice
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+The translatable type is a specialized ``ChoiceType`` where the choices values are translated with the Symfony
+Translator component.
+
+The type has one extra parameter :
+ * ``catalogue`` : the catalogue name to translate the value
+
+
+.. code-block:: php
+
+    <?php
+
+    // The delivery list
+    class Delivery
+    {
+        public static function getStatusList()
+        {
+            return array(
+                self::STATUS_OPEN      => 'status_open',
+                self::STATUS_PENDING   => 'status_pending',
+                self::STATUS_VALIDATED => 'status_validated',
+                self::STATUS_CANCELLED => 'status_cancelled',
+                self::STATUS_ERROR     => 'status_error',
+                self::STATUS_STOPPED   => 'status_stopped',
+            );
+        }
+    }
+
+    // form usage
+    $form->add('deliveryStatus', 'sonata_type_translatable_choice', array(
+        'choices' => Delivery::getStatusList(),
+        'catalogue' => 'SonataOrderBundle'
+    ))