Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameswilson/338e44cd13afba682dcd to your computer and use it in GitHub Desktop.
Save jameswilson/338e44cd13afba682dcd to your computer and use it in GitHub Desktop.
Programmatically create a placeholder node with translations and localized main menu links.
<?php
// The following code snippet is used to add a placeholder page and companion
// localized menu items to a site with i18n_menu and entity_translation enabled.
/**
* Create an About page.
*/
function mymodule_create_about_page() {
// Config data.
$type = 'article';
$title = array(
'en' => 'About',
'ar' => 'حول',
'he' => 'אודות',
'fa' => 'درباره',
);
$body = array(
'en' => 'Placeholder for about page.',
'ar' => 'نائب لصفحة حول هذا الموقع.',
'he' => 'מציין מיקום של דף על אתר זה.',
'fa' => 'مکان نگه دار برای یک صفحه در مورد این وب سایت.',
);
// Prepare objects with defaults.
$node = new stdClass();
$node->nid = NULL;
$node->type = $type;
$node->uid = 1;
node_object_prepare($node);
// Create the node in the site's default language.
$lang_default = language_default('language');
$node->language = $lang_default;
// Set title and body fields.
$node->title = isset($title[$lang_default]) ? $title[$lang_default] : $title['en'] . " [$lang]";
$node->body[$lang_default][]['value'] = isset($body[$lang_default]) ? $body[$lang_default] : $body['en'] . " [$lang]";
// Create a menu entry.
$link = array(
'enabled' => TRUE,
'link_title' => $node->title,
'description' => '',
'menu_name' => 'main-menu',
'language' => $lang_default,
);
$node->menu = $link;
// Save node.
node_save($node);
// Stop here unless there's more than one language enabled.
if (count(language_list()) == 1) {
return;
}
// Enable menu translation and dependencies, if not already enabled.
// Note: ensure the menu is set to "Translate and Localize", otherwise
// all links will appear in all languages!
if (!module_exists('i18n_menu')) {
module_enable(array('i18n_menu'), TRUE);
}
// Create a translation set to link the original menu items created above
// to the menu translations that we will create further below.
$menu_translation_set = i18n_translation_set_create('menu_link');
$default_menu_item = menu_link_load($node->menu['mlid']);
$default_menu_item['i18n_tsid'] = $menu_translation_set->tsid;
$default_menu_item['customized'] = 1;
menu_link_save($default_menu_item);
// Add a translation for each enabled language on the site.
foreach (language_list() as $lang => $language) {
// Skip the default language.
if ($lang == $lang_default) {
continue;
}
// Add translated title field (assumes title module is present).
$node->title_field[$lang][]['value'] = isset($title[$lang]) ? $title[$lang] : $title['en'] . " [$lang]";
// Add translated body field.
$node->body[$lang][]['value'] = isset($body[$lang]) ? $body[$lang] : $body['en'] . " [$lang]";
// Add node translation.
$node->translations->data[$lang]['entity_type'] = 'node';
$node->translations->data[$lang]['entity_id'] = $node->nid;
$node->translations->data[$lang]['language'] = $lang;
$node->translations->data[$lang]['source'] = $node->language;
$node->translations->data[$lang]['uid'] = $node->uid;
$node->translations->data[$lang]['status'] = $node->status;
$node->translations->data[$lang]['translate'] = 0;
$node->translations->data[$lang]['created'] = $node->created;
$node->translations->data[$lang]['changed'] = $node->changed;
// Save node translation.
node_save($node);
// Create a translated menu item grouping it with the translation set id
// created above.
$translated_menu_item = array(
'enabled' => TRUE,
'link_title' => $node->title_field[$lang][0]['value'],
'menu_name' => 'main-menu',
'customized' => 1,
'link_path' => 'node/' . $node->nid,
'language' => $lang,
'i18n_tsid' => $menu_translation_set->tsid,
);
menu_link_save($translated_menu_item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment