Closed siuho closed 10 years ago
I've changed my routing because I think there were conflict (/admin and /admin/contact/ => /admin_contact) But now I have a problems with my i18n fields. It's available when I edit a contact but not when I created a new one. I think it's because I have to create a ContactI18n object but I don't know when exactly and how to embed this with the existing object Contact??
Here's my translatableCollectionType.php
private $container;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$languages = $options['languages'];
$i18nClass = $options['i18n_class'];
$options['options']['data_class'] = $i18nClass;
$options['options']['columns'] = $options['columns'];
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($builder, $languages, $i18nClass) {
$form = $event->getForm();
$data = $event->getData();
if($data == null)
{
return;
}
//get the class name of the i18nClass
$temp = explode('\\', $i18nClass);
$dataClass = end($temp);
$rootData = $form->getRoot()->getData();
//add a database row for every needed language
foreach($languages as $lang)
{
$found = false;
foreach($data as $i18n)
{
if($i18n->getLocale() == $lang)
{
$found = true;
break;
}
}
if(!$found)
{
$newTranslation = new $i18nClass();
$newTranslation->setLocale($lang);
$addFunction = 'add'.$dataClass;
$rootData->$addFunction($newTranslation);
}
}
});
parent::buildForm($builder, $options);
}
public function getName()
{
return 'translatable_collection';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);
$resolver->setDefaults(array(
'languages' => array(),
'i18n_class' => '',
'attr' => array('class' => 'type_collection'),
'columns' => array(),
'type' => 'translation_type',
'allow_add' => false,
'allow_delete' => false,
'languages' => $this->container->getParameter('languages')
));
}
/**
* Sets the Container.
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
And my TranslationType.php which create the input for the i18n fields
public function buildForm(FormBuilderInterface $builder, array $options)
{
$columns = $options['columns'];
$dataClass = $options['data_class'];
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($builder, $columns, $dataClass) {
$form = $event->getForm();
$data = $event->getData();
if(!$data instanceof $dataClass)
{
return;
}
//loop over all columns and add the input
foreach($columns as $column => $options)
{
if($options == null) $options = array();
$type = 'text';
if(array_key_exists('type', $options))
{
$type = $options['type'];
}
$label = $column;
if(array_key_exists('label', $options))
{
$label = $options['label'];
}
$customOptions = array();
if(array_key_exists('options', $options))
{
$customOptions = $options['options'];
}
$options = array(
'label' => $label.' '.strtoupper($data->getLocale()),
'auto_initialize' => false
);
$options = array_merge($options, $customOptions);
$form->add($builder->getFormFactory()->createNamed($column, $type, null, $options));
}
});
}
public function getName()
{
return 'translation_type';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);
$resolver->setDefaults(array(
'columns' => array(),
));
}
public function getDefaultOptions(array $options)
{
return $options + array(
'columns' => array(),
'language' => ''
);
}
Did I do something wrong?
Thank you (and sorry for my bad english)
I replace
$formOptions = $this->getFormOption('contactI18ns', array( 'allow_add' => true, 'allow_delete' => true, 'by_reference' => false, 'i18n_class' => 'Bundle\\Model\\ContactI18n', 'columns' => array( 'service' =>array( 'type' => 'textarea', ), ), 'label' => 'Traductions', 'translation_domain' => 'Admin',));
$builder->add('contactI18ns', 'translatable_collection', $formOptions);
By
$builder->add('contactI18ns', 'translatable_collection', array( 'required' => false, 'i18n_class' => 'Bundle\\Model\\ContactI18n', 'columns' => array( 'service' =>array( 'type' => 'textarea', ), ), 'label' => 'Traductions', 'translation_domain' => 'Admin', 'options'=> array('label' => false)));
in the new form and it works. Don't know why it does not love the first syntax
I'm tying to create a new object but got this error :
Here's my generator file :
But I know that I have another object call "Admin" and it seems that it search in the wrong place :
instead of BaseContactController? And I can create a new object Admin without problems. What can I do? Thank you