diff --git a/src/Tests/TokenMenuTest.php b/src/Tests/TokenMenuTest.php index 914f9cf..3528e61 100644 --- a/src/Tests/TokenMenuTest.php +++ b/src/Tests/TokenMenuTest.php @@ -202,6 +202,35 @@ function testMenuTokens() { return strpos((string) $item[0], 'Child link') !== FALSE; }); $this->assertEqual(1, count($options)); + + // Now add a new node with no menu. + $this->drupalGet('node/add/page'); + $this->drupalPostForm(NULL, [ + 'title[0][value]' => 'Node menu adding menu later test', + 'body[0][value]' => 'Going to add a menu link on edit', + 'menu[enabled]' => 0, + ], t('Save and publish')); + $node = $this->drupalGetNodeByTitle('Node menu adding menu later test'); + // Now edit it and add a menu item. + $this->drupalGet('node/' . $node->id() . '/edit'); + $this->drupalPostForm(NULL, [ + 'title[0][value]' => 'Node menu adding menu later test', + 'body[0][value]' => 'This is a [node:menu-link:parent:url:path] token to the menu link parent', + 'menu[enabled]' => 1, + 'menu[title]' => 'Child link', + 'menu[menu_parent]' => 'main-menu:' . $parent_link->getPluginId(), + ], t('Save and keep published')); + $node = $this->drupalGetNodeByTitle('Node menu adding menu later test', TRUE); + $this->assertEqual('This is a /admin/config token to the menu link parent', $node->body->value); + // And make sure the menu link exists with the right URI. + $link = menu_ui_get_menu_link_defaults($node); + $this->assertTrue(!empty($link['entity_id'])); + $query = \Drupal::entityQuery('menu_link_content') + ->condition('link.uri', 'entity:node/' . $node->id()) + ->sort('id', 'ASC') + ->range(0, 1); + $result = $query->execute(); + $this->assertTrue($result); } } diff --git a/token.module b/token.module index 9d8be42..3f476e0 100644 --- a/token.module +++ b/token.module @@ -710,13 +710,22 @@ function token_node_menu_link_submit($entity_type, NodeInterface $node, &$form, $entity = MenuLinkContent::load($entity_id); } else { - // Create a new menu_link_content entity. - $entity = MenuLinkContent::create(array( - // Lets just reference the UUID for now, the link is not important for - // token generation. - 'link' => ['uri' => 'internal:/node/' . $node->uuid()], - 'langcode' => $node->language()->getId(), - )); + if ($node->isNew()) { + // Create a new menu_link_content entity. + $entity = MenuLinkContent::create(array( + // Lets just reference the UUID for now, the link is not important for + // token generation. + 'link' => ['uri' => 'internal:/node/' . $node->uuid()], + 'langcode' => $node->language()->getId(), + )); + } + else { + // Create a new menu_link_content entity. + $entity = MenuLinkContent::create(array( + 'link' => ['uri' => 'entity:node/' . $node->id()], + 'langcode' => $node->language()->getId(), + )); + } } $entity->enabled->value = 1; $entity->title->value = trim($values['title']);