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";
    }
  }
}

Comments

YesCT’s picture

Title: Multilingual Creation of D8 Node in PHP » Find a home for example code for Multilingual Creation of D8 Node in PHP
Component: language.module » documentation
Issue summary: View changes
Issue tags: +D8MI

Thanks a ton!

Now we can discuss it, and figure out how to best make use of this example. :)

jhodgdon’s picture

Project: Drupal core » Documentation
Version: 8.0-alpha11 »
Component: documentation » Missing documentation

Suggestions:

a) Examples for Developers project https://drupal.org/project/examples -- in which case, please move this issue to that project.

b) On some Drupal Community Documentation page. In which case, please either just create the page or move this issue to the Documentation project's issue queue.

Either way, I do not think this belongs in the Drupal Core issue queue, so I'm moving it to Documentation for the moment.

Greg Sims’s picture

Title: Find a home for example code for Multilingual Creation of D8 Node in PHP » Multilingual Creation of D8 Node in PHP
jhodgdon’s picture

Title: Multilingual Creation of D8 Node in PHP » Document Multilingual Creation of D8 Node in PHP