From 6059fb36cd11333190d9450f60d778588e5741ca Mon Sep 17 00:00:00 2001
From: Stefan Borchert <stefan.borchert@undpaul.de>
Date: Tue, 7 Jun 2011 22:05:24 +0200
Subject: [PATCH] Issue #1123662: Add menu path using autocomplete.

---
 misc/autocomplete.js        |    1 +
 modules/menu/menu.admin.inc |   29 ++++++++++++++++++++++++++++-
 modules/menu/menu.module    |    7 +++++++
 3 files changed, 36 insertions(+), 1 deletions(-)

diff --git a/misc/autocomplete.js b/misc/autocomplete.js
index 5e85be4..837ce83 100644
--- a/misc/autocomplete.js
+++ b/misc/autocomplete.js
@@ -265,6 +265,7 @@ Drupal.ACDB = function (uri) {
  * Performs a cached and delayed search.
  */
 Drupal.ACDB.prototype.search = function (searchString) {
+  console.log(searchString);
   var db = this;
   this.searchString = searchString;
 
diff --git a/modules/menu/menu.admin.inc b/modules/menu/menu.admin.inc
index ec073e5..d7a5ace 100644
--- a/modules/menu/menu.admin.inc
+++ b/modules/menu/menu.admin.inc
@@ -287,7 +287,8 @@ function menu_edit_item($form, &$form_state, $type, $item, $menu) {
       '#type' => 'textfield',
       '#title' => t('Path'),
       '#default_value' => $path,
-      '#description' => t('The path for this menu link. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', array('%front' => '<front>', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')),
+      '#description' => t('The path for this menu link. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page. You may enter the title of the node you would like to link to to get a list of possible matches.', array('%front' => '<front>', '%add-node' => 'node/add', '%drupal' => 'http://drupal.org')),
+      '#autocomplete_path' => 'menu/autocomplete',
       '#required' => TRUE,
     );
     $form['actions']['delete'] = array(
@@ -683,3 +684,29 @@ function menu_configure() {
 
   return system_settings_form($form);
 }
+
+/**
+ * Helper function for autocompletion.
+ */
+function menu_autocomplete($title_typed = '') {
+  $title_matches = array();
+
+  if ($title_typed != '') {
+    $query = db_select('node', 'n');
+    $query->addTag('translatable');
+    $query->addTag('node_access');
+    
+    // Select rows that match by node title.
+    $titles_return = $query
+      ->fields('n', array('nid', 'title'))
+      ->condition('n.title', '%' . db_like($title_typed) . '%', 'LIKE')
+      ->range(0, 10)
+      ->execute()
+      ->fetchAllKeyed();
+
+    foreach ($titles_return as $nid => $title) {
+      $title_matches['node/' . $nid] = check_plain($title);
+    }
+  }
+  drupal_json_output($title_matches);
+}
diff --git a/modules/menu/menu.module b/modules/menu/menu.module
index fc8f68a..0fdac98 100644
--- a/modules/menu/menu.module
+++ b/modules/menu/menu.module
@@ -153,6 +153,13 @@ function menu_menu() {
     'access arguments' => array('administer menu'),
     'file' => 'menu.admin.inc',
   );
+  $items['menu/autocomplete'] = array(
+    'title' => 'Autocomplete menu items',
+    'page callback' => 'menu_autocomplete',
+    'access arguments' => array('administer menu'),
+    'type' => MENU_CALLBACK,
+    'file' => 'menu.admin.inc',
+  );
   return $items;
 }
 
-- 
1.7.4

