I have a translatable entity using the translatable behaviour of doctrine2.
I'm trying to build a form that looks like this:
| French |English| Spanish |
+--+--------| |---------+------------+
| |
| name: [___my_english_name___] |
| |
| title: [___my_english_title__] |
| |
+------------------------------------------+
Order: [___1___]
Online: (x) Yes
( ) No
So basically, there are the order & online attributes of the object that are not translatable, and the name & title attribute that have the translatable behaviour.
In case my drawing is not clear: the form contain a 1 tab per locale that hold the field that are translatable.
The problem I have is that by default, Symfony2 bind a form to an entity, but the doctrine translatable behaviour force me to have one entity per locale. Personally the doctrine behaviour is fine (and I like it), but I'm unable to make a form that allow me to edit the entity in all the locale -- in the same form.
So far, I've the main form:
namespace myApp\ProductBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
/**
* Form for the productGroup.
*/
class ProductType extends AbstractType
{
/**
* Decide what field will be present in the form.
*
* @param FormBuilder $builder FormBuilder instance.
* @param array $options Custom options.
*
* @return null;
*/
public function buildForm(FormBuilder $builder, array $options)
{
//Todo: get the available locale from the service.
$arrAvailableLocale = array('en_US', 'en_CA', 'fr_CA', 'es_ES');
//Render a tab for each locale
foreach ($arrAvailableLocale as $locale) {
$builder->add(
'localeTab_' . $locale,
new ProductLocaleType(),
array('property_path' => false, //Do not map the type to an attribute.
));
}
//Uni-locale attributes of the entity.
$builder
->add('isOnline')
->add('sortOrder');
}
/**
* Define the defaults options for the form building process.
*
* @param array $options Custom options.
*
* @return array Options with the defaults values applied.
*/
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'myApp\ProductBundle\Entity\Product',
);
}
/**
* Define the unique name of the form.
*
* @return string
*/
public function getName()
{
return 'myapp_productbundle_producttype';
}
}
And the tab-form:
<?php
namespace MyApp\ProductBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use invalidArgumentException;
/**
* Form for the productGroupLocale tabs.
*/
class ProductLocaleType extends AbstractType
{
/**
* Decide what field will be present in the form.
*
* @param FormBuilder $builder FormBuilder instance.
* @param array $options Custom options.
*
* @return null;
*/
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name', 'text', array('data' => ???));
$builder->add('title', 'text', array('data' => ???));
}
/**
* Define the defaults options for the form building process.
*
* @param array $options Custom options.
*
* @return array Options with the defaults values applied.
*/
public function getDefaultOptions(array $options)
{
return array(
//'data_class' => 'MyApp\ProductBundle\Entity\Product',
'name' => '',
'title' => '',
);
}
/**
* Define the unique name of the form.
*
* @return string
*/
public function getName()
{
return 'myapp_productbundle_productlocaletype';
}
}
But as you can't see, I've no idea how to get the name and title values from the translated entity, and neither I know how to persist them once the form will be submitted.
Hi if you use gedmo extensions Translatable is not meant to handle multiple translations per request. Try using knplabs alternative may be a better option to handle it in more general ways.
You may be interested in TranslationFormBundle, which add a form type to work with DoctrineTranslatable extension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With