Issue #3379293 by benjifisher, lauriii, pooja saraah, smustgrave, poker10: Make it easier to add a child menu item
parent
d6f9f1d42f
commit
246b571cbd
|
@ -89,7 +89,8 @@ class MenuLinkContentForm extends ContentEntityForm {
|
|||
public function form(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::form($form, $form_state);
|
||||
|
||||
$default = $this->entity->getMenuName() . ':' . $this->entity->getParentId();
|
||||
$parent_id = $this->entity->getParentId() ?: $this->getRequest()->query->get('parent');
|
||||
$default = $this->entity->getMenuName() . ':' . $parent_id;
|
||||
$id = $this->entity->isNew() ? '' : $this->entity->getPluginId();
|
||||
if ($this->entity->isNew()) {
|
||||
$menu_id = $this->entity->getMenuName();
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Drupal\menu_ui;
|
||||
|
||||
use Drupal\Component\Utility\NestedArray;
|
||||
use Drupal\Component\Utility\SortArray;
|
||||
use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\Core\Entity\EntityForm;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
@ -434,6 +435,21 @@ class MenuForm extends EntityForm {
|
|||
'#default_value' => $link->getParent(),
|
||||
];
|
||||
$operations = $link->getOperations();
|
||||
if ($element->depth < $this->menuTree->maxDepth()) {
|
||||
$add_link_url = Url::fromRoute(
|
||||
'entity.menu.add_link_form',
|
||||
['menu' => $this->entity->id()],
|
||||
['query' => ['parent' => $link->getPluginId()]]
|
||||
);
|
||||
$operations += [
|
||||
'add-child' => [
|
||||
'title' => $this->t('Add child'),
|
||||
'weight' => 20,
|
||||
'url' => $add_link_url,
|
||||
],
|
||||
];
|
||||
uasort($operations, [SortArray::class, 'sortByWeightElement']);
|
||||
}
|
||||
foreach ($operations as $key => $operation) {
|
||||
if (!isset($operations[$key]['query'])) {
|
||||
// Bring the user back to the menu overview.
|
||||
|
|
|
@ -504,6 +504,10 @@ class MenuUiTest extends BrowserTestBase {
|
|||
$this->toggleMenuLink($item1);
|
||||
$this->toggleMenuLink($item2);
|
||||
|
||||
// Test the "Add child" link for two siblings.
|
||||
$this->verifyAddChildLink($item5);
|
||||
$this->verifyAddChildLink($item6);
|
||||
|
||||
// Move link and verify that descendants are updated.
|
||||
$this->moveMenuLink($item2, $item5->getPluginId(), $menu_name);
|
||||
// Hierarchy
|
||||
|
@ -787,7 +791,7 @@ class MenuUiTest extends BrowserTestBase {
|
|||
*/
|
||||
public function checkInvalidParentMenuLinks() {
|
||||
$last_link = NULL;
|
||||
$created_links = [];
|
||||
$plugin_ids = [];
|
||||
|
||||
// Get the max depth of the tree.
|
||||
$menu_link_tree = \Drupal::service('menu.link_tree');
|
||||
|
@ -810,18 +814,28 @@ class MenuUiTest extends BrowserTestBase {
|
|||
$this->submitForm($edit, 'Save');
|
||||
$menu_links = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['title' => $title]);
|
||||
$last_link = reset($menu_links);
|
||||
$created_links[] = 'tools:' . $last_link->getPluginId();
|
||||
$plugin_ids[] = $last_link->getPluginId();
|
||||
}
|
||||
|
||||
$last_plugin_id = array_pop($plugin_ids);
|
||||
|
||||
// The last link cannot be a parent in the new menu link form.
|
||||
$this->drupalGet('admin/structure/menu/manage/tools/add');
|
||||
$value = 'tools:' . $last_link->getPluginId();
|
||||
$value = 'tools:' . $last_plugin_id;
|
||||
$this->assertSession()->optionNotExists('edit-menu-parent', $value);
|
||||
|
||||
// All but the last link can be parents in the new menu link form.
|
||||
array_pop($created_links);
|
||||
foreach ($created_links as $key => $link) {
|
||||
$this->assertSession()->optionExists('edit-menu-parent', $link);
|
||||
foreach ($plugin_ids as $plugin_id) {
|
||||
$this->assertSession()->optionExists('edit-menu-parent', 'tools:' . $plugin_id);
|
||||
}
|
||||
|
||||
// The last link does not have an "Add child" operation.
|
||||
$this->drupalGet('admin/structure/menu/manage/tools');
|
||||
$this->assertSession()->linkByHrefNotExists('parent=' . urlencode($last_plugin_id));
|
||||
|
||||
// All but the last link do have an "Add child" operation.
|
||||
foreach ($plugin_ids as $plugin_id) {
|
||||
$this->assertSession()->linkByHrefExists('parent=' . urlencode($plugin_id));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -909,6 +923,25 @@ class MenuUiTest extends BrowserTestBase {
|
|||
$this->assertSession()->pageTextContains($title);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the "Add child" link selects the correct parent item.
|
||||
*
|
||||
* @param \Drupal\menu_link_content\Entity\MenuLinkContent $item
|
||||
* Menu link entity.
|
||||
*/
|
||||
public function verifyAddChildLink(MenuLinkContent $item): void {
|
||||
$menu_name = $item->getMenuName();
|
||||
|
||||
$this->drupalGet('admin/structure/menu/manage/' . $menu_name);
|
||||
$links = $this->xpath('//a[normalize-space()=:item_label]/following::a[normalize-space()=:link_label]', [
|
||||
':item_label' => $item->getTitle(),
|
||||
':link_label' => 'Add child',
|
||||
]);
|
||||
$links[0]->click();
|
||||
$option = $this->assertSession()->optionExists('edit-menu-parent', $menu_name . ':' . $item->getPluginId());
|
||||
$this->assertTrue($option->isSelected());
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets a standard menu link using the UI.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue