diff --git a/core/modules/views/src/Plugin/views/display/Page.php b/core/modules/views/src/Plugin/views/display/Page.php index 6db80c9ba87f2a223e11d8751b7ea229cf4298e4..2b41335313215acad3b2c5957541667be89814a7 100644 --- a/core/modules/views/src/Plugin/views/display/Page.php +++ b/core/modules/views/src/Plugin/views/display/Page.php @@ -51,6 +51,7 @@ protected function defineOptions() { 'description' => array('default' => '', 'translatable' => FALSE), 'weight' => array('default' => 0), 'menu_name' => array('default' => 'navigation'), + 'parent' => array('default' => ''), 'context' => array('default' => ''), ), ); @@ -217,13 +218,14 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { ), ); - // Only display the menu selector if Menu UI module is enabled. + // Only display the parent selector if Menu UI module is enabled. + $menu_parent = $menu['menu_name'] . ':' . $menu['parent']; if (\Drupal::moduleHandler()->moduleExists('menu_ui')) { - $form['menu']['menu_name'] = array( - '#title' => t('Menu'), - '#type' => 'select', - '#options' => menu_ui_get_menus(), - '#default_value' => $menu['menu_name'], + $form['menu']['parent'] = \Drupal::service('menu.parent_form_selector')->parentSelectElement($menu_parent); + $form['menu']['parent'] += array( + '#title' => $this->t('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.'), + '#attributes' => array('class' => array('menu-title-select')), '#states' => array( 'visible' => array( array( @@ -237,9 +239,9 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { ); } else { - $form['menu']['menu_name'] = array( + $form['menu']['parent'] = array( '#type' => 'value', - '#value' => $menu['menu_name'], + '#value' => $menu_parent, ); $form['menu']['markup'] = array( '#markup' => t('Menu selection requires the activation of Menu UI module.'), @@ -410,7 +412,9 @@ public function submitOptionsForm(&$form, FormStateInterface $form_state) { switch ($form_state['section']) { case 'menu': - $this->setOption('menu', $form_state->getValue('menu')); + $menu = $form_state->getValue('menu'); + list($menu['menu_name'], $menu['parent']) = explode(':', $menu['parent'], 2); + $this->setOption('menu', $menu); // send ajax form to options page if we use it. if ($form_state->getValue(array('menu', 'type')) == 'default tab') { $form_state['view']->addFormToStack('display', $this->display['id'], 'tab_options'); diff --git a/core/modules/views/src/Plugin/views/display/PathPluginBase.php b/core/modules/views/src/Plugin/views/display/PathPluginBase.php index f4f4700e39b93cb2322efecba588001911d15862..07a2816139dc07254bcb97ea8f615284c05ab9d0 100644 --- a/core/modules/views/src/Plugin/views/display/PathPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/PathPluginBase.php @@ -308,6 +308,7 @@ public function getMenuLinks() { ); $links[$menu_link_id]['title'] = $menu['title']; $links[$menu_link_id]['description'] = $menu['description']; + $links[$menu_link_id]['parent'] = $menu['parent']; if (isset($menu['weight'])) { $links[$menu_link_id]['weight'] = intval($menu['weight']); diff --git a/core/modules/views/src/Tests/Plugin/DisplayPageTest.php b/core/modules/views/src/Tests/Plugin/DisplayPageTest.php index 97e5cb96a39e558a2267894011b1d644d1452121..22896ce9d614169df16e83f18fbd05608880ab87 100644 --- a/core/modules/views/src/Tests/Plugin/DisplayPageTest.php +++ b/core/modules/views/src/Tests/Plugin/DisplayPageTest.php @@ -7,9 +7,11 @@ namespace Drupal\views\Tests\Plugin; +use Drupal\Core\Menu\MenuTreeParameters; use Drupal\Core\Session\AnonymousUserSession; use Drupal\views\Views; use Drupal\views\Tests\ViewUnitTestBase; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\HttpKernelInterface; @@ -26,7 +28,7 @@ class DisplayPageTest extends ViewUnitTestBase { * * @var array */ - public static $testViews = array('test_page_display', 'test_page_display_route'); + public static $testViews = array('test_page_display', 'test_page_display_route', 'test_page_display_menu'); /** * Modules to enable. @@ -127,4 +129,15 @@ public function testPageRouterItems() { $this->assertFalse($route->hasDefault('arg_1'), 'No default value is set for the required argument id_2.'); } + /** + * Tests the generated menu links of views. + */ + public function testMenuLinks() { + \Drupal::service('plugin.manager.menu.link')->rebuild(); + $tree = \Drupal::menuTree()->load('admin', new MenuTreeParameters()); + $this->assertTrue(isset($tree['system.admin']->subtree['views_view:views.test_page_display_menu.page_4'])); + $menu_link = $tree['system.admin']->subtree['views_view:views.test_page_display_menu.page_4']->link; + $this->assertEqual($menu_link->getTitle(), 'Test child'); + } + } diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_page_display_menu.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_page_display_menu.yml index a159b36365f55b1e15ab1dbcd5b33fa79d314696..cd812f236ee58a80a82f204302f3118399950abc 100644 --- a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_page_display_menu.yml +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_page_display_menu.yml @@ -79,6 +79,24 @@ display: display_title: Page id: page_3 position: 0 + page_4: + display_options: + path: test_page_display_menu/child + title: 'Test page as child' + menu: + type: normal + title: 'Test child' + parent: 'system.admin' + description: '' + name: tools + weight: '0' + context: '0' + defaults: + title: '0' + display_plugin: page + display_title: Page + id: page_4 + position: 0 label: 'Test page menu' id: test_page_display_menu tag: ''