From ce842f8809315ee7ed0318e83ec9220fc40d535e Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 13 Aug 2014 14:48:34 -0700
Subject: [PATCH] Issue #2202493 by Xano, dawehner, longwave, tim.plunkett:
 Fixed views_menu_link_defaults() does not set a parent for links.

---
 .../views/src/Plugin/views/display/Page.php   | 22 +++++++++++--------
 .../Plugin/views/display/PathPluginBase.php   |  1 +
 .../src/Tests/Plugin/DisplayPageTest.php      | 15 ++++++++++++-
 .../views.view.test_page_display_menu.yml     | 18 +++++++++++++++
 4 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/core/modules/views/src/Plugin/views/display/Page.php b/core/modules/views/src/Plugin/views/display/Page.php
index 6db80c9ba87f..2b4133531321 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 f4f4700e39b9..07a2816139dc 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 97e5cb96a39e..22896ce9d614 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 a159b36365f5..cd812f236ee5 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: ''
-- 
GitLab