3

I have a multilingual setup, and the content translation works well and smooth.

But when I try to edit a node on the other language - I have the same link from the content admin menu, which renders the default language form. I.e. I have /node/21/edit both for En and De versions.

For the test I've try to alter the node object on the form build step (couldn't found a correct hook, so just try it in the FormController::getContentResult() ) - no luck:

$translated = $form_object->getEntity()->getTranslation('de');
$form_object->setEntity($translated);

form object states the active language as 'de', but the rendered form still has en version fields.

Also the form has native language switcher (Languages), which in my mind should do language switching, but for me it shows only English and does nothing.

1 Answer 1

2

The entity passed in the $form_object is simply ignored, as the whole $form_object thing - it is used only to determine the form id. You may add entity to the form_state, but with the original drupal form generation it is ignored too.

To set the node edit translation you need to set $form_state->set('langcode', $langcode);

The full thing emulating drupal's node edit form looks like so:

public function customEdit($type = 'node', $id, $langcode) {
    $request = \Drupal::request();
    $entity = Node::load($id);
    $form_object = \Drupal::entityManager()->getFormObject('node', 'edit')->setEntity($entity);

    // Add the form and form_state to trick the getArguments method of the
    // controller resolver.
    $form_state = new FormState();
    $request->attributes->set('form', array());
    $request->attributes->set('form_state', $form_state);

    $args = \Drupal::service('controller_resolver')->getArguments($request, array($form_object, 'buildForm'));

    $request->attributes->remove('form');
    $request->attributes->remove('form_state');

    // Remove $form and $form_state from the arguments, and re-index them.
    unset($args[0], $args[1]);
    $form_state->addBuildInfo('args', array_values($args));

    $form_state->set('langcode', $langcode);

    $render_array = \Drupal::service('form_builder')->buildForm($form_object, $form_state);

    return $render_array;
  }

The answer is actual for the Drupal 8 beta 2, I'm sure the ability to edit different language node versions would be fixed before release.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.