You are here

mpac.module in Multi-path autocomplete 6

Same filename and directory in other branches
  1. 8 mpac.module
  2. 7 mpac.module

Find node paths on menu item creation via autocomplete.

File

mpac.module
View source
<?php

/**
 * @file
 * Find node paths on menu item creation via autocomplete.
 */

/**
 * Implement hook_menu().
 *
 * @return An array of menu items.
 */
function mpac_menu() {
  $items = array();
  $items['mpac/autocomplete'] = array(
    'title' => 'Menu path autocomplete',
    'description' => 'Autocomplete callback for menu path autocomplete',
    'page callback' => 'mpac_autocomplete',
    'access callback' => 'user_access',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Find nodes and URL aliases based on title.
 * @param $string
 *   Title of element to find.
 * @return
 *   List of elements with path as key.
 */
function mpac_autocomplete($string) {
  $matches = array();
  $title = filter_xss_admin($string);

  // Get a list of all nodes where the title matches the given string.
  $query = db_query("SELECT * FROM {node} n WHERE n.title LIKE '%%%s%%'", $title);
  while ($node = db_fetch_object($query)) {

    // Add node path and title to list.
    if (node_access('view', $node) && $node->status) {
      $matches['node/' . $node->nid] = $node->title;
    }
  }
  if (module_exists('path')) {

    // Get a list of all URL aliases where the destination matches the given
    // string.
    $query = db_query("SELECT * FROM {url_alias} WHERE dst LIKE '%%%s%%'", $title);
    while ($alias = db_fetch_object($query)) {

      // Add alias source and destination to list (and add a special marker to
      // the item).
      $matches[$alias->src] = $alias->dst . t('*');
    }
  }
  drupal_json($matches);
}

/**
 * Implement hook_form_FORM_ID_alter().
 *
 * Change path field to autocomplete field.
 */
function mpac_form_menu_edit_item_alter(&$form, &$form_state) {
  $form['menu']['link_path']['#autocomplete_path'] = 'mpac/autocomplete';
  $form['menu']['link_path']['#description'] .= '<br />' . t("You may enter the title of the node you'd like to link to to get a list of all possible matches.");
  if (module_exists('path')) {
    $form['menu']['link_path']['#description'] .= ' ' . t('Matches marked with %marker are URL aliases.', array(
      '%marker' => t('*'),
    ));
  }
}

Functions

Namesort descending Description
mpac_autocomplete Find nodes and URL aliases based on title.
mpac_form_menu_edit_item_alter Implement hook_form_FORM_ID_alter().
mpac_menu Implement hook_menu().