function search_autocomplete_page_build in Search Autocomplete 7.4
Implements hook_page_build().
Add autocomplete.js on every page.
File
- ./
search_autocomplete.module, line 21
Code
function search_autocomplete_page_build() {
// Checkout if user have authorization use admin tools.
if (user_access('administer Search Autocomplete') && variable_get('sa_admin_helper', TRUE)) {
// Add the admin tools.
drupal_add_js(drupal_get_path('module', 'search_autocomplete') . '/js/search_autocomplete.admin.js');
drupal_add_css(drupal_get_path('module', 'search_autocomplete') . '/css/search_autocomplete.admin.css');
}
// Checkout if user have authorization to access the autocompleted form.
if (user_access('use Search Autocomplete')) {
// Checkout if the db exists (it should).
if (db_table_exists('search_autocomplete_forms')) {
// Get every form to autocomplete.
$result = db_select('search_autocomplete_forms', 'f')
->fields('f')
->condition('enabled', 1, '=')
->execute()
->fetchAllAssoc('fid');
// Build the setting array to transfer to JS.
foreach ($result as $match) {
$form_id = 'form' . $match->fid;
$input_data = explode("\n", $match->data_static);
$data_static = array();
for ($i = 0; $i < count($input_data); $i++) {
$cut = strripos($input_data[$i], '=>');
$object = array();
if ($cut > 0) {
$object['label'] = trim(substr($input_data[$i], 0, $cut));
$object['value'] = trim(substr($input_data[$i], 0, $cut));
$object['link'] = trim(substr($input_data[$i], $cut + 2, strlen($input_data[$i])));
}
else {
$object['label'] = trim($input_data[$i]);
$object['value'] = trim($input_data[$i]);
}
$data_static[] = $object;
}
$theme_id = preg_replace("/\\.[^.\\s]{3,4}\$/", "", $match->theme);
$data_source = $match->data_source;
$data_callback = $match->data_callback;
if ($match->data_source != 'static') {
if (url_is_external($match->data_callback)) {
$data_source = 'external';
}
else {
$data_source = 'internal';
$data_callback = urldecode(url($match->data_callback, array(
'absolute' => TRUE,
)));
}
}
// Translate no_results label.
$no_results = json_decode($match->no_results);
$no_results->label = t($no_results->label);
// Translate all_results label.
$all_results = json_decode($match->all_results);
$all_results->label = t($all_results->label);
drupal_add_js(array(
'search_autocomplete' => array(
$form_id => array(
'selector' => $match->selector,
'minChars' => $match->min_char,
'max_sug' => $match->max_sug,
'type' => $data_source,
'datas' => $match->data_source == 'static' ? $data_static : $data_callback,
'fid' => $match->fid,
// Get the css filename with '-' instead of ' ', lower case and no '.css'.
'theme' => str_replace(' ', '-', strtolower($theme_id)),
'auto_submit' => $match->auto_submit,
'auto_redirect' => $match->auto_redirect,
),
),
), 'setting');
drupal_add_css(drupal_get_path('module', 'search_autocomplete') . '/css/themes/' . $match->theme);
}
// If there is some results: need to include the css and js....
if ($result) {
drupal_add_library('system', 'ui');
drupal_add_library('system', 'ui.widget');
drupal_add_library('system', 'ui.position');
drupal_add_library('system', 'ui.autocomplete');
drupal_add_js(drupal_get_path('module', 'search_autocomplete') . '/js/jquery.autocomplete.js');
}
}
}
if (isset($_GET['sort_order'])) {
$_GET['sort_order'] = drupal_strtoupper($_GET['sort_order']);
}
}