-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogramme_search.module
More file actions
66 lines (53 loc) · 1.61 KB
/
programme_search.module
File metadata and controls
66 lines (53 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Implements hook_menu().
*/
function programme_search_menu() {
$items['programme_search/autocomplete'] = array(
'page callback' => 'programme_search_autocomplete',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Build a basic form.
*/
function programme_search_search_form($form, &$form_state) {
$form['search_programmes'] = array(
'#title' => t(''),
'#type' => 'textfield',
'#default_value' => t('Start typing...'),
'#autocomplete_path' => 'programme_search/autocomplete',
);
return $form;
}
/**
* Menu callback function.
*
* @param string $string
* The string that will be searched.
* @return object
* A JSON object.
*/
function programme_search_autocomplete($string) {
$matches = array();
$query = db_select('node', 'n');
$query->join('field_data_field_programme_search_terms', 'st', 'st.entity_id = n.nid');
$query->join('taxonomy_term_data', 'td', 'td.tid = st.field_programme_search_terms_tid');
$or = db_or();
$or->condition('n.title', '%' . db_like($string) . '%', 'LIKE');
$or->condition('td.name', '%' . db_like($string) . '%', 'LIKE');
$query->fields('n', array('nid', 'title'));
$query->condition('n.type', 'programme', '=');
$query->condition($or);
$query->range(0, 20);
$return = $query->execute();
foreach ($return as $row) {
$matches[drupal_get_path_alias('node/' . $row->nid)] = check_plain($row->title);
}
if (empty($matches)) {
$matches['0'] = t('No matches.');
}
drupal_json_output($matches);
}