/** * Test automatic reparenting of menu links. */ function testMenuLinkReparenting($module = 'menu_test') { // Check the initial hierarchy. $links = $this->createLinkHierarchy($module); $expected_hierarchy = array('parent' => '', 'child-1' => 'parent', 'child-1-1' => 'child-1', 'child-1-2' => 'child-1', 'child-2' => 'parent'); $this->assertMenuLinkParents($links, $expected_hierarchy); // Start over, and move child-1 under child-2, and check that all the // childs of child-1 have been moved too. $links = $this->createLinkHierarchy($module); /* @var \Drupal\Core\Menu\MenuLinkInterface $menu_link_plugin */ $this->menuLinkManager->updateDefinition($links['child-1'], array('parent' => $links['child-2'])); // Verify that the entity was updated too. $menu_link_plugin = $this->menuLinkManager->createInstance($links['child-1']); $entity = \Drupal::entityManager()->loadEntityByUuid('menu_link_content', $menu_link_plugin->getDerivativeId()); $this->assertEqual($entity->getParentId(), $links['child-2']); $expected_hierarchy = array('parent' => '', 'child-1' => 'child-2', 'child-1-1' => 'child-1', 'child-1-2' => 'child-1', 'child-2' => 'parent'); $this->assertMenuLinkParents($links, $expected_hierarchy); // Start over, and delete child-1, and check that the children of child-1 // have been reassigned to the parent. $links = $this->createLinkHierarchy($module); $this->menuLinkManager->removeDefinition($links['child-1']); $expected_hierarchy = array('parent' => FALSE, 'child-1-1' => 'parent', 'child-1-2' => 'parent', 'child-2' => 'parent'); $this->assertMenuLinkParents($links, $expected_hierarchy); // @todo Figure out what makes sense to test in terms of automatic // re-parenting. https://www.drupal.org/node/2309531 }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Locked menus may not be deleted. if ($this->entity->isLocked()) { return; } // Delete all links to the overview page for this menu. // @todo Add a more generic helper function to the menu link plugin // manager to remove links to a entity or other ID used as a route // parameter that is being removed. Also, consider moving this to // menu_ui.module as part of a generic response to entity deletion. // https://www.drupal.org/node/2310329 $menu_links = $this->menuLinkManager->loadLinksByRoute('entity.menu.edit_form', array('menu' => $this->entity->id()), TRUE); foreach ($menu_links as $id => $link) { $this->menuLinkManager->removeDefinition($id); } parent::submitForm($form, $form_state); }
/** * Remove menu entries associate with the vocabulary of this term. * * @param \Drupal\taxonomy\TermInterface $term */ public function removeTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) { // Load relevant taxonomy menus. $tax_menus = $this->getTermMenusByVocabulary($term->getVocabularyId()); foreach ($tax_menus as $menu) { foreach (array_keys($menu->getLinks([], TRUE)) as $plugin_id) { if (!$rebuild_all) { $plugin_id_parts = explode('.', $plugin_id); $term_id = array_pop($plugin_id_parts); if ($term->id() != $term_id) { continue; } } $this->manager->removeDefinition($plugin_id, FALSE); } } }
/** * {@inheritdoc} */ public function submit(array $form, FormStateInterface $form_state) { $form_state->setRedirect('menu_ui.overview_page'); // Locked menus may not be deleted. if ($this->entity->isLocked()) { return; } // Delete all links to the overview page for this menu. // @todo Add a more generic helper function to the menu link plugin // manager to remove links to a entity or other ID used as a route // parameter that is being removed. Also, consider moving this to // menu_ui.module as part of a generic response to entity deletion. // https://www.drupal.org/node/2310329 $menu_links = $this->menuLinkManager->loadLinksByRoute('menu_ui.menu_edit', array('menu' => $this->entity->id()), TRUE); foreach ($menu_links as $id => $link) { $this->menuLinkManager->removeDefinition($id); } // Delete the custom menu and all its menu links. $this->entity->delete(); $t_args = array('%title' => $this->entity->label()); drupal_set_message(t('The custom menu %title has been deleted.', $t_args)); $this->logger('menu')->notice('Deleted custom menu %title and all its menu links.', $t_args); }