getMenuName example

/** * Tests for menu_name parameter for default menu links. */
  protected function doTestMenuName() {
    $admin_user = $this->drupalCreateUser(['administer site configuration']);
    $this->drupalLogin($admin_user);
    /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    $menu_links = $menu_link_manager->loadLinksByRoute('menu_test.menu_name_test');
    $menu_link = reset($menu_links);
    $this->assertEquals('original', $menu_link->getMenuName(), 'Menu name is "original".');

    // Change the menu_name parameter in menu_test.module, then force a menu     // rebuild.     menu_test_menu_name('changed');
    $menu_link_manager->rebuild();

    $menu_links = $menu_link_manager->loadLinksByRoute('menu_test.menu_name_test');
    $menu_link = reset($menu_links);
    $this->assertEquals('changed', $menu_link->getMenuName(), 'Menu name was successfully changed after rebuild.');
  }

  
    $menu = 'tools';
    $edit = [
      'title[0][value]' => 'Root',
      'link[0][uri]' => '/node',
    ];
    $this->drupalGet("admin/structure/menu/manage/{$menu}/add");
    $this->submitForm($edit, 'Save');
    $menu_links = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['title' => 'Root']);
    $link = reset($menu_links);

    $edit = [
      'menu[menu_parent]' => $link->getMenuName() . ':' . $link->getPluginId(),
    ];
    $this->drupalGet('node/' . $parent->id() . '/edit');
    $this->submitForm($edit, 'Save');

    // Add a taxonomy term/tag to last node, and add a link for that term to the     // Tools menu.     $tags = [
      'Drupal' => [],
      'Breadcrumbs' => [],
    ];
    $edit = [
      
    // item's weight doesn't get changed because of the old hardcoded delta=50.     $items = [];
    for ($i = -50; $i <= 51; $i++) {
      $items[$i] = $this->addMenuLink('', '/node/' . $node1->id()$menu_name, TRUE, strval($i));
    }
    $this->assertMenuLink(['weight' => '51']$items[51]->getPluginId());

    // Disable a link and then re-enable the link via the overview form.     $this->disableMenuLink($item1);
    $edit = [];
    $edit['links[menu_plugin_id:' . $item1->getPluginId() . '][enabled]'] = TRUE;
    $this->drupalGet('admin/structure/menu/manage/' . $item1->getMenuName());
    $this->submitForm($edit, 'Save');

    // Mark item2, item4 and item5 as expanded.     // This is done in order to show them on the frontpage.     $item2->expanded->value = 1;
    $item2->save();
    $item4->expanded->value = 1;
    $item4->save();
    $item5->expanded->value = 1;
    $item5->save();

    
/** * {@inheritdoc} */
  public function deleteLinksInMenu($menu_name) {
    foreach ($this->treeStorage->loadByProperties(['menu_name' => $menu_name]) as $plugin_id => $definition) {
      $instance = $this->createInstance($plugin_id);
      if ($instance->isDeletable()) {
        $this->deleteInstance($instance, TRUE);
      }
      elseif ($instance->isResettable()) {
        $new_instance = $this->resetInstance($instance);
        $affected_menus[$new_instance->getMenuName()] = $new_instance->getMenuName();
      }
    }
  }

  /** * Deletes a specific instance. * * @param \Drupal\Core\Menu\MenuLinkInterface $instance * The plugin instance to be deleted. * @param bool $persist * If TRUE, calls MenuLinkInterface::deleteLink() on the instance. * * @throws \Drupal\Component\Plugin\Exception\PluginException * If the plugin instance does not support deletion. */
// Move the menu link back to the Tools menu.     $item->menu_name->value = 'tools';
    $item->save();
    // Create a second node.     $child_node = $this->drupalCreateNode(['type' => 'article']);
    // Assign a menu link to the second node, being a child of the first one.     $child_item = MenuLinkContent::create([
      'link' => [['uri' => 'entity:node/' . $child_node->id()]],
      'title' => $this->randomMachineName(16),
      'parent' => $item->getPluginId(),
      'menu_name' => $item->getMenuName(),
    ]);
    $child_item->save();
    // Edit the first node.     $this->drupalGet('node/' . $node->id() . '/edit');
    // Assert that it is not possible to set the parent of the first node to itself or the second node.     $this->assertSession()->optionNotExists('edit-menu-menu-parent', 'tools:' . $item->getPluginId());
    $this->assertSession()->optionNotExists('edit-menu-menu-parent', 'tools:' . $child_item->getPluginId());
    // Assert that unallowed Administration menu is not available in options.     $this->assertSession()->optionNotExists('edit-menu-menu-parent', 'admin:');
  }

  

  protected function assertEntity($id$langcode$title$menu$description$enabled$expanded, array $attributes$uri$weight) {
    /** @var \Drupal\menu_link_content\MenuLinkContentInterface $menu_link */
    $menu_link = MenuLinkContent::load($id);
    $menu_link = $menu_link->getTranslation($langcode);
    $this->assertInstanceOf(MenuLinkContentInterface::class$menu_link);
    $this->assertSame($title$menu_link->getTitle());
    $this->assertSame($langcode$menu_link->language()->getId());
    $this->assertSame($menu$menu_link->getMenuName());
    $this->assertSame($description$menu_link->getDescription());
    $this->assertSame($enabled$menu_link->isEnabled());
    $this->assertSame($expanded$menu_link->isExpanded());
    $this->assertSame($attributes$menu_link->link->options);
    $this->assertSame($uri$menu_link->link->uri);
    $this->assertSame($weight$menu_link->getWeight());
    return $menu_link;
  }

}

  public function getQuestion() {
    return $this->t('Are you sure you want to reset the link %item to its default values?', ['%item' => $this->link->getTitle()]);
  }

  /** * {@inheritdoc} */
  public function getCancelUrl() {
    return new Url('entity.menu.edit_form', [
      'menu' => $this->link->getMenuName(),
    ]);
  }

  /** * {@inheritdoc} */
  public function getDescription() {
    return $this->t('Any customizations will be lost. This action cannot be undone.');
  }

  /** * {@inheritdoc} */
    // that may change the values of the access results and links.     $tree_cacheability = $tree_access_cacheability->merge($tree_link_cacheability);
    $tree_cacheability->applyTo($build);

    if ($items) {
      // Make sure Drupal\Core\Render\Element::children() does not re-order the       // links.       $build['#sorted'] = TRUE;
      // Get the menu name from the last link.       $item = end($items);
      $link = $item['original_link'];
      $menu_name = $link->getMenuName();
      // Add the theme wrapper for outer markup.       // Allow menu-specific theme overrides.       $build['#theme'] = 'menu__' . strtr($menu_name, '-', '_');
      $build['#menu_name'] = $menu_name;
      $build['#items'] = $items;
      // Set cache tag.       $build['#cache']['tags'][] = 'config:system.menu.' . $menu_name;
    }

    return $build;
  }

  


  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $link = $form['#plugin_form']->submitConfigurationForm($form$form_state);

    $this->messenger()->addStatus($this->t('The menu link has been saved.'));
    $form_state->setRedirect(
      'entity.menu.edit_form',
      ['menu' => $link->getMenuName()]
    );
  }

}


  /** * Tests migration of menu links. */
  public function testMenuLinks() {
    /** @var \Drupal\menu_link_content\Entity\MenuLinkContent $menu_link */
    $menu_link = MenuLinkContent::load(139)->getTranslation('fr');
    $this->assertInstanceOf(MenuLinkContent::class$menu_link);
    $this->assertSame('fr - Test 2', $menu_link->getTitle());
    $this->assertSame('fr - Test menu link 2', $menu_link->getDescription());
    $this->assertSame('secondary-links', $menu_link->getMenuName());
    $this->assertTrue($menu_link->isEnabled());
    $this->assertTrue($menu_link->isExpanded());
    $this->assertSame(['query' => ['foo' => 'bar'], 'attributes' => ['title' => 'Test menu link 2']]$menu_link->link->options);
    $this->assertSame('internal:/admin', $menu_link->link->uri);
    $this->assertSame(-49, $menu_link->getWeight());

    $menu_link = MenuLinkContent::load(139)->getTranslation('zu');
    $this->assertInstanceOf(MenuLinkContent::class$menu_link);
    $this->assertSame('Test 2', $menu_link->getTitle());
    $this->assertSame('zu - Test menu link 2', $menu_link->getDescription());
    $this->assertSame('secondary-links', $menu_link->getMenuName());
    

  public function build(array $tree$level = 0) {
    if ($level == 0) {
      if (!$tree) {
        return [];
      }
      $build = parent::build($tree);

      /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
      $first_link = reset($tree)->link;
      // Get the menu name of the first link.       $menu_name = $first_link->getMenuName();
      // Add a more specific theme suggestion to differentiate this rendered       // menu from others.       $build['#menu_name'] = $menu_name;
      $build['#theme'] = 'menu__toolbar__' . strtr($menu_name, '-', '_');
      return $build;
    }
    else {
      return parent::build($tree);
    }
  }

}
$count = function Darray $tree) {
      $sum = function D$carry, MenuLinkTreeElement $item) {
        return $carry + $item->count();
      };
      return array_reduce($tree$sum);
    };

    $this->assertEquals(8, $count($tree));
    $parameters = new MenuTreeParameters();
    $parameters->setRoot('test.example2');
    $tree = $this->linkTree->load($instance->getMenuName()$parameters);
    $top_link = reset($tree);
    $this->assertCount(1, $top_link->subtree);
    $child = reset($top_link->subtree);
    $this->assertEquals($links[3]->getPluginId()$child->link->getPluginId());
    $height = $this->linkTree->getSubtreeHeight('test.example2');
    $this->assertEquals(3, $height);
  }

}

class MenuLinkContentDeleteForm extends ContentEntityDeleteForm {

  /** * {@inheritdoc} */
  public function getCancelUrl() {
    if ($this->moduleHandler->moduleExists('menu_ui')) {
      return new Url('entity.menu.edit_form', ['menu' => $this->entity->getMenuName()]);
    }
    return $this->entity->toUrl();
  }

  /** * {@inheritdoc} */
  protected function getRedirectUrl() {
    return $this->getCancelUrl();
  }

  
$container->get('entity_type.bundle.info'),
      $container->get('datetime.time')
    );
  }

  /** * {@inheritdoc} */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form$form_state);

    $default = $this->entity->getMenuName() . ':' . $this->entity->getParentId();
    $id = $this->entity->isNew() ? '' : $this->entity->getPluginId();
    $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default$id);
    $form['menu_parent']['#weight'] = 10;
    $form['menu_parent']['#title'] = $this->t('Parent link');
    $form['menu_parent']['#description'] = $this->t('The maximum depth for a link and all its children is fixed. Some menu links may not be available as parents if selecting them would exceed this limit.');
    $form['menu_parent']['#attributes']['class'][] = 'menu-title-select';

    return $form;
  }

  /** * {@inheritdoc} */
$menu_link = MenuLinkContent::load(1);
    $this->drupalGet($menu_link->toUrl('delete-form'));
    $this->assertSession()->pageTextContains("Are you sure you want to delete the custom menu link {$menu_link->label()}?");
    $this->assertSession()->linkExists('Cancel');
    // Make sure cancel link points to link edit     $this->assertSession()->linkByHrefExists($menu_link->toUrl('edit-form')->toString());

    \Drupal::service('module_installer')->install(['menu_ui']);

    // Make sure cancel URL points to menu_ui route now.     $this->drupalGet($menu_link->toUrl('delete-form'));
    $menu = Menu::load($menu_link->getMenuName());
    $this->assertSession()->linkByHrefExists($menu->toUrl('edit-form')->toString());
    $this->submitForm([], 'Delete');
    $this->assertSession()->pageTextContains("The menu link {$menu_link->label()} has been deleted.");
  }

}
Home | Imprint | This part of the site doesn't use cookies.