/** * Test views internal menu link options. */ public function testMenuLinkOverrides() { // Link from views module. $views_link = $this->menuLinkManager->getDefinition('views_view:views.test_page_display_menu.page_3'); $this->assertTrue($views_link['enabled'], 'Menu link is enabled.'); $this->assertFalse($views_link['expanded'], 'Menu link is not expanded.'); $views_link['enabled'] = 0; $views_link['expanded'] = 1; $this->menuLinkManager->updateDefinition($views_link['id'], $views_link); $views_link = $this->menuLinkManager->getDefinition($views_link['id']); $this->assertFalse($views_link['enabled'], 'Menu link is disabled.'); $this->assertTrue($views_link['expanded'], 'Menu link is expanded.'); $this->menuLinkManager->rebuild(); $this->assertFalse($views_link['enabled'], 'Menu link is disabled.'); $this->assertTrue($views_link['expanded'], 'Menu link is expanded.'); // Link from user module. $user_link = $this->menuLinkManager->getDefinition('user.page'); $this->assertTrue($user_link['enabled'], 'Menu link is enabled.'); $user_link['enabled'] = 0; $views_link['expanded'] = 1; $this->menuLinkManager->updateDefinition($user_link['id'], $user_link); $this->assertFalse($user_link['enabled'], 'Menu link is disabled.'); $this->menuLinkManager->rebuild(); $this->assertFalse($user_link['enabled'], 'Menu link is disabled.'); $this->menuLinkOverrides->reload(); $views_link = $this->menuLinkManager->getDefinition('views_view:views.test_page_display_menu.page_3'); $this->assertFalse($views_link['enabled'], 'Menu link is disabled.'); $this->assertTrue($views_link['expanded'], 'Menu link is expanded.'); $user_link = $this->menuLinkManager->getDefinition('user.page'); $this->assertFalse($user_link['enabled'], 'Menu link is disabled.'); }
/** * 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 }
/** * Update menu entries associate with the vocabulary of this term. * * @param \Drupal\taxonomy\TermInterface $term */ public function updateTaxonomyMenuEntries(TermInterface $term, $rebuild_all = TRUE) { // Load relevant taxonomy menus. $tax_menus = $this->getTermMenusByVocabulary($term->getVocabularyId()); foreach ($tax_menus as $menu) { foreach ($menu->getLinks([], TRUE) as $plugin_id => $plugin_def) { if (!$rebuild_all) { $plugin_id_explode = explode('.', $plugin_id); $term_id = array_pop($plugin_id_explode); if ($term->id() != $term_id) { continue; } } $this->manager->updateDefinition($plugin_id, $plugin_def, FALSE); } } }
/** * Submit handler for the menu overview form. * * This function takes great care in saving parent items first, then items * underneath them. Saving items in the incorrect order can break the tree. */ protected function submitOverviewForm(array $complete_form, FormStateInterface $form_state) { // Form API supports constructing and validating self-contained sections // within forms, but does not allow to handle the form section's submission // equally separated yet. Therefore, we use a $form_state key to point to // the parents of the form section. $parents = $form_state['menu_overview_form_parents']; $input = NestedArray::getValue($form_state['input'], $parents); $form =& NestedArray::getValue($complete_form, $parents); // When dealing with saving menu items, the order in which these items are // saved is critical. If a changed child item is saved before its parent, // the child item could be saved with an invalid path past its immediate // parent. To prevent this, save items in the form in the same order they // are sent, ensuring parents are saved first, then their children. // See http://drupal.org/node/181126#comment-632270 $order = is_array($input) ? array_flip(array_keys($input)) : array(); // Update our original form with the new order. $form = array_intersect_key(array_merge($order, $form), $form); $fields = array('weight', 'parent', 'enabled'); foreach (Element::children($form) as $id) { if (isset($form[$id]['#item'])) { $element = $form[$id]; $updated_values = array(); // Update any fields that have changed in this menu item. foreach ($fields as $field) { if ($element[$field]['#value'] != $element[$field]['#default_value']) { // Hidden is a special case, the form value needs to be reversed. if ($field == 'enabled') { $updated_values['hidden'] = $element['enabled']['#value'] ? 0 : 1; } else { $updated_values[$field] = $element[$field]['#value']; } } } if ($updated_values) { // Use the ID from the actual plugin instance since the hidden value // in the form could be tampered with. $this->menuLinkManager->updateDefinition($element['#item']->link->getPLuginId(), $updated_values); } } } }
/** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { $new_definition = $this->extractFormValues($form, $form_state); return $this->menuLinkManager->updateDefinition($this->menuLink->getPluginId(), $new_definition); }