Translate a node programmatically | Drupal 8

Translate a node programmatically

Submitted by editor on Sat, 11/28/2015 - 19:01
Question

How to translate a node programmatically and how to get a field translated in Drupal 8

Exemples:

$translation = $entity->getTranslation('de');

$translation = \Drupal::entityManager()->getTranslationFromContext($entity);

$node_es = $node->addTranslation('es');
$node_es->title = 'Mi prueba!';
$node_es->body->value = '<p>El cuerpo de mi nodo.</p>';
$node_es->body->format = 'full_html';
$node_es->save();

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.