1 system.module path_autocomplete($string = '')

Menu callback; Autocomplete callback for paths.

Parameters

string $string: The string to find matches for.

File

core/modules/system/system.module, line 4220
Configuration system that lets administrators modify the workings of the site.

Code

function path_autocomplete($string = '') {
  $matches = array();
  $match_count = 0;
  $range = 10;

  if ($string) {
    // Get node matches.
    $results = db_select('node')
      ->fields('node', array('nid', 'type', 'title', 'status'))
      ->condition('node.title', '%' . db_like($string) . '%', 'LIKE')
      ->condition('node.status', 1)
      ->addTag('node_access')
      ->orderBy('node.nid', 'DESC')
      ->range(0, $range)
      ->execute();
    foreach ($results as $result) {
      $path = backdrop_get_path_alias('node/' . $result->nid);
      $match = '<span class="autocomplete-suggestion">' . check_plain($result->title) . '</span>';
      $match_description = t('Content: %type', array('%type' => node_type_get_name($result->type)));
      $matches[$path] = $match . ' <span class="autocomplete-description">(' . $match_description . ')</span>';
      $match_count++;
    }

    // Get file matches.
    if (user_access('manage files') && $match_count < $range) {
      $results = db_select('file_managed')
        ->fields('file_managed', array('fid', 'filename', 'uri', 'filemime', 'status', 'type'))
        ->condition('filename', '%' . db_like($string) . '%', 'LIKE')
        ->condition('status', 1)
        ->condition('filemime', 'image%', 'NOT LIKE')
        ->orderBy('fid', 'DESC')
        ->range(0, $range - $match_count)
        ->execute();
      foreach ($results as $result) {
        // If possible, try to make a local path for better portability.
        $absolute_path = parse_url($GLOBALS['base_url'], PHP_URL_PATH) . '/';
        $url = file_create_url($result->uri);
        $path = str_replace($GLOBALS['base_url'] . '/', $absolute_path, $url);
        $match = '<span class="autocomplete-suggestion">' . check_plain($result->filename) . '</span>';
        $match_description = t('File: %type', array('%type' => file_type_get_name($result->type)));
        $matches[$path] = $match . ' <span class="autocomplete-description">(' . $match_description . ')</span>';
        $match_count++;
      }
    }

    // Get user matches.
    if (user_access('access user profiles') && $match_count < $range) {
      $results = db_select('users')
        ->fields('users', array('uid', 'name'))
        ->condition('name', '%' . db_like($string) . '%', 'LIKE')
        ->range(0, $range - $match_count)
        ->orderBy('uid', 'DESC')
        ->execute();
      // We need to load the User object to use user_format_name().
      $uids = array();
      foreach ($results as $result) {
        $uids[] = $result->uid;
      }
      if (!empty($uids)) {
        $users = user_load_multiple($uids);
        foreach ($users as $user) {
          $path = backdrop_get_path_alias('user/' . $user->uid);
          $match = '<span class="autocomplete-suggestion">' . check_plain(user_format_name($user)) . '</span>';
          $match_description = t('User account');
          $matches[$path] = $match . ' <span class="autocomplete-description">(' . $match_description . ')</span>';
          $match_count++;
        }
      }
    }

    // Get taxonomy term matches.
    if (module_exists('taxonomy') && $match_count < $range) {
      $results = db_select('taxonomy_term_data')
        ->fields('taxonomy_term_data', array('tid', 'vocabulary', 'name'))
        ->condition('taxonomy_term_data.name', '%' . db_like($string) . '%', 'LIKE')
        ->addTag('taxonomy_term_access')
        ->range(0, $range - $match_count)
        ->orderBy('taxonomy_term_data.tid', 'DESC')
        ->execute();
      foreach ($results as $result) {
        $path = backdrop_get_path_alias('taxonomy/term/' . $result->tid);
        $match = '<span class="autocomplete-suggestion">' . check_plain($result->name) . '</span>';
        $match_description = t('Taxonomy term: %vocabulary', array('%vocabulary' => taxonomy_vocabulary_load($result->vocabulary)->name));
        $matches[$path] = $match . ' <span class="autocomplete-description">(' . $match_description . ')</span>';
        $match_count++;
      }
    }

    // Get Views page matches.
    if ($match_count < $range) {
      $views = views_get_enabled_views();
      foreach ($views as $view) {
        foreach ($view->display as $display_id => $display) {
          if ($view->access($display_id) && $display->display_plugin == 'page' && !empty($display->display_options['path'])) {
            $path = backdrop_get_path_alias($display->display_options['path']);
            if (!path_is_admin($path)) {
              $view_title = '';
              $found_match = FALSE;
              // Display title.
              if (!empty($display->display_options['title']) && stripos($display->display_options['title'], $string) !== FALSE) {
                $found_match = TRUE;
                $view_title = $display->display_options['title'];
              }
              // Default title.
              elseif (!empty($view->display['default']->display_options['title']) && stripos($view->display['default']->display_options['title'], $string) !== FALSE) {
                $found_match = TRUE;
                $view_title = $view->display['default']->display_options['title'];
              }
              // View name.
              elseif (stripos($view->human_name, $string) !== FALSE) {
                $found_match = TRUE;
                $view_title = $view->human_name;
              }
              if ($found_match) {
                $match = '<span class="autocomplete-suggestion">' . check_plain($view_title) . '</span>';
                $match_description = t('View: %human - %display', array('%human' => $view->human_name, '%display' => $display->display_title));
                $matches[$path] = $match . ' <span class="autocomplete-description">(' . $match_description . ')</span>';
                $match_count++;
              }
            }
          }
          if ($match_count == $range) {
            break 2;
          }
        }
      }
    }

    // Get layout path matches.
    if ($match_count < $range) {
      $menu_items = layout_get_all_configs('menu_item');
      foreach ($menu_items as $menu_item) {
        if ($match_count < $range) {
          $layout_title = '';
          $found_match = FALSE;
          if (!empty($menu_item['menu']['title']) && stripos($menu_item['menu']['title'], $string) !== FALSE) {
            $found_match = TRUE;
            $layout_title = $menu_item['menu']['title'];
          }
          elseif (stripos($menu_item['name'], $string) !== FALSE) {
            $found_match = TRUE;
            $layout_title = $menu_item['name'];
          }
          if ($found_match) {
            $match = '<span class="autocomplete-suggestion">' . check_plain($layout_title) . '</span>';
            $match_description = t('Layout: %display', array('%display' => $menu_item['name']));
            $matches[$menu_item['path']] = $match . ' <span class="autocomplete-description">(' . $match_description . ')</span>';
            $match_count++;
          }
        }
      }
    }

    // Get menu item matches.
    if ($match_count < $range) {
      $results = db_select('menu_links')
        ->fields('menu_links', array('link_title', 'link_path'))
        ->condition('link_title', '%' . db_like($string) . '%', 'LIKE')
        ->condition('link_path', '%' . db_like('%') . '%', 'NOT LIKE')
        ->range(0, $range)
        ->execute();
      foreach ($results as $result) {
        $path = backdrop_get_path_alias($result->link_path);
        if (!isset($matches[$path]) && !path_is_admin($path)) {
          $match = '<span class="autocomplete-suggestion">' . check_plain($result->link_title) . '</span>';
          $match_description = t('Menu item: %path', array('%path' => $path));
          $matches[$path] = $match . ' <span class="autocomplete-description">(' . $match_description . ')</span>';
          $match_count++;
        }
      }
    }
  }

  // Allow modules to alter the matches.
  backdrop_alter('path_autocomplete', $matches, $string);
  backdrop_json_output($matches);
}