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

tobiberlin created an issue. See original summary.

tobiberlin’s picture

I think I found the answer:

  $form = array();
  global $language;
  $langcode = $language->language;

  $fields = array(
    'field_name1',
    'field_name2',
    'field_name3',
    'field_name4'
  );

  foreach ($fields as $fieldname) {
    field_attach_form('node', $node, $form, $form_state, $langcode, array('field_name' => $fieldname));
  }

  $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;

Added the $langcode to the field_attach_form() call.

tobiberlin’s picture

Status: Active » Fixed
tobiberlin’s picture

Status: Fixed » Active

seems 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 field has been disabled because you do not have sufficient permissions to edit it.

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?!

stefanos.petrakis@gmail.com’s picture

Status: Active » Postponed (maintainer needs more info)

Hi Tobi,

You should move the handler->setActiveLanguage() call before the foreach statement, as the entity_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!