H,
I want to create a custom form where I want to add some fields of a node. The fields are translateable and I call my custom form in the language the node fields should be filled in. I tried the following:
$form = array();
$fields = array(
'field_name1',
'field_name2',
'field_name3',
'field_name4'
);
foreach ($fields as $fieldname) {
field_attach_form('node', $node, $form, $form_state, NULL, array('field_name' => $fieldname));
}
global $language;
$langcode = $language->language;
$handler = entity_translation_get_handler('node', $node);
$handler->setActiveLanguage($langcode);
$form['actions'] = array(
'#type' => 'actions'
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => 'Absenden'
);
return $form;
This translates the field labels but not the values in the field. It shows in the title that the correct language is chosen (title shows "[Français]" and the correct french node title )but the fields have the value of the original language. When I add the $handler stuff before calling field_attach_form() the fields are not editable in French when the original language is English.
Can someone help me how I get the translated fields in this way?
Best and many thanks in advance,
Tobias
Comments
Comment #2
tobiberlinI think I found the answer:
Added the $langcode to the field_attach_form() call.
Comment #3
tobiberlinComment #4
tobiberlinseems as if I mixed up the forms when I tested... unfortunately the above changed nothing. Instead I see a message in the translatable fields:
This is because the $form array ends up with a mix of languages: I see the form in French, the fields arrays have a key "#language" which is "en", but the correct french version is added. When I change this "#language" value to fr the field has the English text instead of the French.
I think I miss some very basic?!
Comment #5
stefanos.petrakis@gmail.comHi Tobi,
You should move the
handler->setActiveLanguage()
call before theforeach
statement, as theentity_translation_field_attach_form()
function will need the entity translation handler to be initialized and the activeLanguage set.Let me know if that worked for you!