Commit 02511ddc authored by catch's avatar catch

Issue #2599594 by agoradesign, mdespeuilles, nagwani: Multilingual content:...

Issue #2599594 by agoradesign, mdespeuilles, nagwani: Multilingual content: Menu link is not correctly stored on translation
parent 321b0db1
...@@ -139,12 +139,20 @@ function _menu_ui_node_save(NodeInterface $node, array $values) { ...@@ -139,12 +139,20 @@ function _menu_ui_node_save(NodeInterface $node, array $values) {
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */ /** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
if (!empty($values['entity_id'])) { if (!empty($values['entity_id'])) {
$entity = MenuLinkContent::load($values['entity_id']); $entity = MenuLinkContent::load($values['entity_id']);
if ($entity->isTranslatable()) {
if (!$entity->hasTranslation($node->language()->getId())) {
$entity = $entity->addTranslation($node->language()->getId(), $entity->toArray());
}
else {
$entity = $entity->getTranslation($node->language()->getId());
}
}
} }
else { else {
// Create a new menu_link_content entity. // Create a new menu_link_content entity.
$entity = MenuLinkContent::create(array( $entity = MenuLinkContent::create(array(
'link' => ['uri' => 'entity:node/' . $node->id()], 'link' => ['uri' => 'entity:node/' . $node->id()],
'langcode' => $node->getUntranslated()->language()->getId(), 'langcode' => $node->language()->getId(),
)); ));
$entity->enabled->value = 1; $entity->enabled->value = 1;
} }
...@@ -216,6 +224,7 @@ function menu_ui_get_menu_link_defaults(NodeInterface $node) { ...@@ -216,6 +224,7 @@ function menu_ui_get_menu_link_defaults(NodeInterface $node) {
} }
if ($id) { if ($id) {
$menu_link = MenuLinkContent::load($id); $menu_link = MenuLinkContent::load($id);
$menu_link = \Drupal::service('entity.repository')->getTranslationFromContext($menu_link);
$defaults = array( $defaults = array(
'entity_id' => $menu_link->id(), 'entity_id' => $menu_link->id(),
'id' => $menu_link->getPluginId(), 'id' => $menu_link->getPluginId(),
......
...@@ -8,7 +8,9 @@ ...@@ -8,7 +8,9 @@
namespace Drupal\menu_ui\Tests; namespace Drupal\menu_ui\Tests;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\menu_link_content\Entity\MenuLinkContent; use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\node\Entity\Node;
/** /**
* Add, edit, and delete a node with menu link. * Add, edit, and delete a node with menu link.
...@@ -29,7 +31,7 @@ class MenuNodeTest extends WebTestBase { ...@@ -29,7 +31,7 @@ class MenuNodeTest extends WebTestBase {
* *
* @var array * @var array
*/ */
public static $modules = array('menu_ui', 'test_page_test', 'node', 'block'); public static $modules = array('menu_ui', 'test_page_test', 'node', 'block', 'locale', 'language', 'content_translation');
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
...@@ -46,6 +48,10 @@ protected function setUp() { ...@@ -46,6 +48,10 @@ protected function setUp() {
'create page content', 'create page content',
'edit any page content', 'edit any page content',
'delete any page content', 'delete any page content',
'create content translations',
'update content translations',
'delete content translations',
'translate any entity',
)); ));
$this->drupalLogin($this->editor); $this->drupalLogin($this->editor);
} }
...@@ -235,4 +241,105 @@ function testMenuNodeFormWidget() { ...@@ -235,4 +241,105 @@ function testMenuNodeFormWidget() {
// Assert that unallowed Administration menu is not available in options. // Assert that unallowed Administration menu is not available in options.
$this->assertNoOption('edit-menu-menu-parent', 'admin:'); $this->assertNoOption('edit-menu-menu-parent', 'admin:');
} }
/**
* Testing correct loading and saving of menu links via node form widget in a multilingual environment.
*/
function testMultilingualMenuNodeFormWidget() {
// Setup languages.
$langcodes = array('de');
foreach ($langcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)->save();
}
array_unshift($langcodes, \Drupal::languageManager()->getDefaultLanguage()->getId());
$config = \Drupal::service('config.factory')->getEditable('language.negotiation');
// Ensure path prefix is used to determine the language.
$config->set('url.source', 'path_prefix');
// Ensure that there's a path prefix set for english as well.
$config->set('url.prefixes.' . $langcodes[0], $langcodes[0]);
$config->save();
$this->rebuildContainer();
$languages = array();
foreach ($langcodes as $langcode) {
$languages[$langcode] = ConfigurableLanguage::load($langcode);
}
// Use a UI form submission to make the node type and menu link content entity translatable.
$this->drupalLogout();
$this->drupalLogin($this->rootUser);
$edit = array(
'entity_types[node]' => TRUE,
'entity_types[menu_link_content]' => TRUE,
'settings[node][page][settings][language][language_alterable]' => TRUE,
'settings[node][page][translatable]' => TRUE,
'settings[node][page][fields][title]' => TRUE,
'settings[menu_link_content][menu_link_content][translatable]' => TRUE,
);
$this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
// Log out and back in as normal user.
$this->drupalLogout();
$this->drupalLogin($this->editor);
// Create a node.
$node_title = $this->randomMachineName(8);
$node = Node::create([
'type' => 'page',
'title' => $node_title,
'body' => $this->randomMachineName(16),
'uid' => $this->editor->id(),
'status' => 1,
'langcode' => $langcodes[0],
]);
$node->save();
// Create translation.
$translated_node_title = $this->randomMachineName(8);
$node->addTranslation($langcodes[1], ['title' => $translated_node_title, 'body' => $this->randomMachineName(16), 'status' => 1]);
$node->save();
// Edit the node and create a menu link.
$edit = array(
'menu[enabled]' => 1,
'menu[title]' => $node_title,
'menu[weight]' => 17,
);
$options = array('language' => $languages[$langcodes[0]]);
$url = $node->toUrl('edit-form', $options);
$this->drupalPostForm($url, $edit, t('Save') . ' ' . t('(this translation)'));
// Edit the node in a different language and translate the menu link.
$edit = array(
'menu[enabled]' => 1,
'menu[title]' => $translated_node_title,
'menu[weight]' => 17,
);
$options = array('language' => $languages[$langcodes[1]]);
$url = $node->toUrl('edit-form', $options);
$this->drupalPostForm($url, $edit, t('Save') . ' ' . t('(this translation)'));
// Assert that the original link exists in the frontend.
$this->drupalGet('node/' . $node->id(), array('language' => $languages[$langcodes[0]]));
$this->assertLink($node_title);
// Assert that the translated link exists in the frontend.
$this->drupalGet('node/' . $node->id(), array('language' => $languages[$langcodes[1]]));
$this->assertLink($translated_node_title);
// Revisit the edit page in original language, check the loaded menu item title and save.
$options = array('language' => $languages[$langcodes[0]]);
$url = $node->toUrl('edit-form', $options);
$this->drupalGet($url);
$this->assertFieldById('edit-menu-title', $node_title);
$this->drupalPostForm(NULL, [], t('Save') . ' ' . t('(this translation)'));
// Revisit the edit page of the translation and check the loaded menu item title.
$options = array('language' => $languages[$langcodes[1]]);
$url = $node->toUrl('edit-form', $options);
$this->drupalGet($url);
$this->assertFieldById('edit-menu-title', $translated_node_title);
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment