first import :: search autocomplete form

Signed-off-by: bachy <git@g-u-i.net>
This commit is contained in:
bachy 2012-07-19 18:31:59 +02:00
commit ad7809415c
4 changed files with 202 additions and 0 deletions

27
materiobasemod.info Normal file
View File

@ -0,0 +1,27 @@
name = materiobasemod
description = "Materio base module"
; Core version (required)
core = 7.x
; Package name (see http://drupal.org/node/542202 for a list of names)
package = Materio
; PHP version requirement (optional)
; php = 5.2
; Loadable code files
;files[] = materiobasemod.theme.inc
;files[] = materiobasemod.forms.inc
files[] = materiobasemod.pages.inc
files[] = materiobasemod.module
; Module dependencies
dependencies[] = taxonomy
; Configuration page
; configure = admin/config/materiobasemod
; For further information about configuration options, see
; - http://drupal.org/node/542202

118
materiobasemod.module Normal file
View File

@ -0,0 +1,118 @@
<?php
/**
* @file
* This is the file description for Materiobasemod module.
*
* In this more verbose, multi-line description, you can specify what this
* file does exactly. Make sure to wrap your documentation in column 78 so
* that the file can be displayed nicely in default-sized consoles.
*/
/**
* Implements hook_permission().
*/
function materiobasemod_permission() {
return array(
'use materiobase search' => array(
'title' => t('Materio base search'),
'description' => t('Use materiobasemod search.'),
),
);
}
/**
* Implements hook_menu().
*/
function materiobasemod_menu() {
$items = array();
$items['materiobase/search/autocomplete'] = array(
'title' => 'Autocomplete materiobase search',
'page callback' => 'materiobase_search_autocomplete',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'file' => 'materiobasemod.pages.inc',
);
return $items;
}
/**
* Implements hook_block_info().
*/
function materiobasemod_block_info() {
// This example comes from node.module.
$blocks['base_search'] = array(
'info' => t('Materio base search'),
'cache' => DRUPAL_NO_CACHE
);
return $blocks;
}
/**
* Implements hook_theme().
*/
function materiobasemod_theme($existing, $type, $theme, $path) {
return array(
'materiobase_search_block' => array(
'arguments' => array(),
'template' => 'materiobase-search-block',
'path' => drupal_get_path('module', 'materiobasemod').'/templates',
),
);
}
/**
* Implements hook_block_view().
*/
function materiobasemod_block_view($delta = '') {
// This example comes from node.module. Note that you can also return a
// renderable array rather than rendered HTML for 'content'.
$block = array();
switch ($delta) {
case 'base_search':
if (user_access('use materiobase search')) {
$block['subject'] = t('Search');
$block['content'] = theme('materiobase_search_block', array());
}
break;
}
return $block;
}
/**
* materiobase_search_form()
*/
function materiobase_search_form(){
$form = array();
$form['searchfield'] = array(
'#type' => 'textfield',
'#default_value' => '',
'#autocomplete_path' => 'materiobase/search/autocomplete',
'#size' => 30,
'#maxlength' => 1024,
// '#element_validate' => array('taxonomy_autocomplete_validate'),
);
$form['create'] = array(
'#type' => 'submit',
'#value' => t('Search'),
);
return $form;
}
/**
* template_preprocess_materiobase_search_block();
*/
function template_preprocess_materiobase_search_block(&$vars){
$vars['searchform'] = drupal_get_form("materiobase_search_form");
}

54
materiobasemod.pages.inc Normal file
View File

@ -0,0 +1,54 @@
<?php
/**
* materiobase_search_autocomplete()
*
* inspired by taxonomy_autocomplete()
*
*/
function materiobase_search_autocomplete($typed = ''){
// If the request has a '/' in the search text, then the menu system will have
// split it into multiple arguments, recover the intended $tags_typed.
$args = func_get_args();
$typed = implode('/', $args);
/*
TODO riche serach engine + \\ etc gmail like
*/
if ($typed != '') {
// Part of the criteria for the query come from the field's own settings.
$vids = array();
$vocabularies = taxonomy_vocabulary_get_names();
foreach ($vocabularies as $voc) {
$vids[] = $voc->vid;
}
$query = db_select('taxonomy_term_data', 't');
$query->addTag('translatable');
$query->addTag('term_access');
// Select rows that match by term name.
$tags_return = $query
->fields('t', array('tid', 'name'))
->condition('t.vid', $vids)
->condition('t.name', '%' . db_like($typed) . '%', 'LIKE')
->range(0, 10)
->execute()
->fetchAllKeyed();
$term_matches = array();
foreach ($tags_return as $tid => $name) {
$n = $name;
// Term names containing commas or quotes must be wrapped in quotes.
// if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
// $n = '"' . str_replace('"', '""', $name) . '"';
// }
$term_matches[$n] = check_plain($name);
}
}
drupal_json_output($term_matches);
}

View File

@ -0,0 +1,3 @@
<?php
print "hello world !";
print render($searchform);