You are here

function linkit_autocomplete in Linkit 7.3

Same name and namespace in other branches
  1. 6 linkit.module \linkit_autocomplete()
  2. 7 linkit.module \linkit_autocomplete()

Autocomplete callback function.

Parameters

object $profile: A LinkitProfile object.

1 string reference to 'linkit_autocomplete'
linkit_menu in ./linkit.module
Implements hook_menu().

File

./linkit.module, line 722
Main file for Linkit module.

Code

function linkit_autocomplete(LinkitProfile $profile) {

  // Set the active Linkit profile.
  linkit_set_active_profile($profile);

  // This is not sanitized until it is used within output.
  $search_string = $_GET[LINKIT_BAC_QUERY_KEY];
  $results = array();

  // Special for link to frontpage.
  if (strpos($search_string, 'front') !== FALSE) {
    $results[] = array(
      'title' => t('Frontpage'),
      'description' => 'The frontpage for this site.',
      'path' => url('<front>'),
      'group' => t('System'),
    );
  }

  // Let the Linkit search plugins do their job.
  $results = array_merge($results, linkit_autocomplete_search_plugins($search_string));

  // If there is results from the Linkit search plugins, don't care about
  // searching for absolute URL's results.
  if (empty($results)) {

    // Try to parse the string as an URL.
    // We will not use the drupal wrapper function drupal_pasre_url() as that
    // function should only be used for URL's that have been generated by the
    // system, and we can't be sure that this is the case here.
    // Check for an e-mail address then return an e-mail result and create a
    // mail-to link if appropriate.
    if (filter_var($search_string, FILTER_VALIDATE_EMAIL)) {
      $results = array(
        array(
          'title' => t('E-mail @email', array(
            '@email' => $search_string,
          )),
          'path' => 'mailto:' . check_plain($search_string),
          'description' => t('Open your mail client ready to e-mail @email', array(
            '@email' => $search_string,
          )),
          'addClass' => 'status-notice',
        ),
      );
    }
    else {
      $parts = parse_url(trim($search_string, '/'));

      // This seems to be an absolute URL.
      if (isset($parts['scheme']) || isset($parts['host'])) {
        $results = array_merge($results, linkit_autocomplete_absolute_url($search_string, $parts));
      }
    }
  }

  // If there is still no results, return a "no results" array.
  if (empty($results)) {
    $results = array(
      array(
        'title' => t('No results'),
        'addClass' => 'status-notice',
        'disabled' => TRUE,
      ),
    );
  }
  print drupal_json_output($results);
  drupal_exit();
}