function MenuLinksTest::createLinkHierarchy

Same name in other branches
  1. 9 core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php \Drupal\Tests\menu_link_content\Kernel\MenuLinksTest::createLinkHierarchy()
  2. 8.9.x core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php \Drupal\Tests\menu_link_content\Kernel\MenuLinksTest::createLinkHierarchy()
  3. 10 core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php \Drupal\Tests\menu_link_content\Kernel\MenuLinksTest::createLinkHierarchy()

Create a simple hierarchy of links.

1 call to MenuLinksTest::createLinkHierarchy()
MenuLinksTest::testMenuLinkReparenting in core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php
Tests automatic reparenting of menu links.

File

core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php, line 64

Class

MenuLinksTest
Tests handling of menu links hierarchies.

Namespace

Drupal\Tests\menu_link_content\Kernel

Code

public function createLinkHierarchy($module = 'menu_test') {
    // First remove all the menu links in the menu.
    $this->menuLinkManager
        ->deleteLinksInMenu('menu-test');
    // Then create a simple link hierarchy:
    // - parent
    //   - child-1
    //     - child-1-1
    //     - child-1-2
    //   - child-2
    $base_options = [
        'title' => 'Menu link test',
        'provider' => $module,
        'menu_name' => 'menu-test',
    ];
    $parent = $base_options + [
        'link' => [
            'uri' => 'internal:/menu-test/hierarchy/parent',
        ],
    ];
    $link = MenuLinkContent::create($parent);
    $link->save();
    $links['parent'] = $link->getPluginId();
    $child_1 = $base_options + [
        'link' => [
            'uri' => 'internal:/menu-test/hierarchy/parent/child',
        ],
        'parent' => $links['parent'],
    ];
    $link = MenuLinkContent::create($child_1);
    $link->save();
    $links['child-1'] = $link->getPluginId();
    $child_1_1 = $base_options + [
        'link' => [
            'uri' => 'internal:/menu-test/hierarchy/parent/child2/child',
        ],
        'parent' => $links['child-1'],
    ];
    $link = MenuLinkContent::create($child_1_1);
    $link->save();
    $links['child-1-1'] = $link->getPluginId();
    $child_1_2 = $base_options + [
        'link' => [
            'uri' => 'internal:/menu-test/hierarchy/parent/child2/child',
        ],
        'parent' => $links['child-1'],
    ];
    $link = MenuLinkContent::create($child_1_2);
    $link->save();
    $links['child-1-2'] = $link->getPluginId();
    $child_2 = $base_options + [
        'link' => [
            'uri' => 'internal:/menu-test/hierarchy/parent/child',
        ],
        'parent' => $links['parent'],
    ];
    $link = MenuLinkContent::create($child_2);
    $link->save();
    $links['child-2'] = $link->getPluginId();
    return $links;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.