Document Multilingual Creation of D8 Node in PHP

Created on 21 May 2014, almost 11 years ago
Updated 26 March 2025, about 2 months ago

Problem/Motivation

This issue is linked from the docs page "Entity Translation API" https://drupal.org/node/2040721

The code is here so it can be reviewed and get feedback.

Proposed resolution

Find a home for example code.

Remaining tasks

  • Give feedback on the code
  • Figure out where something like this would be useful to post (maybe a different docs page)
  • post the code as a patch (so we can interdiff it as we discuss/change it)
  • add steps to the issue summary for how this code can be used (should it go in a custom module?)

User interface changes

No.

API changes

No.

Original report by @Greg Sims β†’

Please find below a code sample that creates a node in English and translates it to Spanish. The code includes the handling of aliases, menu links and displaying the resulting node. Please add to this issue if you see problems with the code or know how to address the "todo"s that it contains. I'm sure this will evolve as D8 stabilizes.

I hope you find this useful, Greg


// requirements:
//   (1) Enable Language and Content Translation Module
//   (2) Add Spanish language in addition to the base language of English
//   (3) enable translation for basic page content type
//   (4) enable translation of ALL fields for the basic page
//
// create a minimum node
//
$settings = array(
  'revision'  => 1,
  'type'      => 'page',
);
$node = entity_create('node', $settings);
$node->setNewRevision();

// create an English node of type basic page
//
$langcode = 'en';
$node->langcode = $langcode;                // the base language of the node
$node = $node->getTranslation($langcode);
set_base_node_fields($node);

// create title & body for the node and save it
//
$node->title->value = "This is the Title of the English node";
$node->body->value  = "This is the Body of the English node";
$node->body->format = 'basic_html';
$node->save();

// create an alias for the node we just saved
//
$source = "node/" . $node->id();
$alias = "alias_of_english_node";
$path = \Drupal::service('path.alias_storage')->save($source, $alias, $langcode);

// create a menu entry
//
$menu_link = entity_create('menu_link', array(
   'menu_name' => 'main',
   'link_path' => $source,
   'link_title' => $node->title->value,
   'weight'     => 0,
));
menu_link_save($menu_link);


// and now create the Spanish translation
//
$langcode = 'es';
$node = $node->getTranslation($langcode);
set_base_node_fields($node);
// todo how do you set the source language of a translation?

// create title & body for the node and save it
//
$node->title->value = "This is the Title of the Spanish node";
$node->body->value  = "This is the Body of the Spanish node";
$node->body->format = 'basic_html';
$node->save();

// create an alias for this node
//
$source = "node/" . $node->id();
$alias = "alias_of_spanish_node";
$path = \Drupal::service('path.alias_storage')->save($source, $alias, $langcode);


// create a menu entry for Spanish
//
//  todo this code throws an exception in 8.0-alpha11 that the method getTranslation does not exist
//
//var_dump($menu_link);
//$menu_link = $menu_link->getTranslation($langcode);
//$menu_link->menu_name = 'main';
//$menu_link->link_path = $source;
//$menu_link->link_title = $node->title;
//$menu_link->weight = 0;
//$menu_link->save();


// let's see the node
//
print "*** English ***\n";
$node = $node->getTranslation('en');
print_entity($node);

print "\n\n*** Spanish ***\n";
$node = $node->getTranslation('es');
print_entity($node);

//var_dump($node);

exit;

function set_base_node_fields($node) {
  $node->uid = 1;                                  // author
  $node->status = NODE_PUBLISHED;
  $node->changed = REQUEST_TIME;
  $node->promote = NODE_NOT_PROMOTED;
  $node->sticky = NODE_NOT_STICKY;
  $node->log = '';
}

function print_entity($entity) {
  foreach ($entity as $field_name => $field_items) {
    foreach ($field_items as $item) {
      print ">>>> " . $field_name . ":\n" . $item->value . "\n";
    }
  }
}

πŸ“Œ Task
Status

Closed: outdated

Component

Missing documentation

Created by

πŸ‡ΊπŸ‡ΈUnited States Greg Sims

Live updates comments and jobs are added and updated live.
  • D8MI

    (Drupal 8 Multilingual Initiative) is the tag used by the multilingual initiative to mark core issues (and some contributed module issues). For versions other than Drupal 8, use the i18n (Internationalization) tag on issues which involve or affect multilingual / multinational support. That is preferred over Translation.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024