362
materio_search_api.module
Normal file
362
materio_search_api.module
Normal file
@@ -0,0 +1,362 @@
|
||||
<?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.
|
||||
*/
|
||||
// define(MATERIO_SEARCH_API_RESULTS_PATH, 'explore');
|
||||
|
||||
/**
|
||||
* Implements hook_permission().
|
||||
*/
|
||||
function materio_search_api_permission() {
|
||||
return array(
|
||||
'use materio search api' => array(
|
||||
'title' => t('Use materio search api'),
|
||||
'description' => t('Use materio search api.'),
|
||||
),
|
||||
'use materio search api autocomplete' => array(
|
||||
'title' => t('Use materio search api autocomplete'),
|
||||
'description' => t('Use materio search api autocomplete.'),
|
||||
),
|
||||
'use materio search api viewmode selection' => array(
|
||||
'title' => t('Use materio search api viewmode selection'),
|
||||
'description' => t('Use materio search api viewmode selection.'),
|
||||
),
|
||||
'administer materio_search_api' => array(
|
||||
'title' => t('administer Materio search api'),
|
||||
'description' => t('Administer materio search api.'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_menu().
|
||||
*/
|
||||
function materio_search_api_menu() {
|
||||
$items = array();
|
||||
|
||||
$base = array(
|
||||
'type' => MENU_CALLBACK,
|
||||
'file' => 'materio_search_api.pages.inc',
|
||||
);
|
||||
|
||||
$items['admin/config/search/search_api/materiosearchapi'] = array(
|
||||
'title' => 'Materio Search Api',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('materio_search_api_settings'),
|
||||
'access arguments' => array('administer materio_search_api'),
|
||||
'file' => 'materio_search_api.admin.inc',
|
||||
'type' => MENU_LOCAL_TASK,
|
||||
);
|
||||
|
||||
$items['materiosearchapi/autocomplete/dbselect'] = $base+array(
|
||||
'access arguments' => array('use materio search api autocomplete'),
|
||||
'page callback' => 'materio_search_api_autocomplete_dbselect',
|
||||
);
|
||||
|
||||
$items['materiosearchapi/autocomplete/searchapi'] = $base+array(
|
||||
'access arguments' => array('use materio search api autocomplete'),
|
||||
'page callback' => 'materio_search_api_autocomplete_searchapi',
|
||||
);
|
||||
|
||||
$items['explore'] = $base+array(
|
||||
'access arguments' => array('use materio search api'),
|
||||
'page callback' => 'materio_search_api_results_search',
|
||||
//'page argument' => array(1,2,3),
|
||||
);
|
||||
|
||||
$items['materiosearchapi/viewmode/change'] = $base+array(
|
||||
'access arguments' => array('use materio search api'),
|
||||
'page callback' => 'materio_search_api_viewmode_change',
|
||||
'page argument' => array(3),
|
||||
);
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_block_info().
|
||||
*/
|
||||
function materio_search_api_block_info() {
|
||||
// This example comes from node.module.
|
||||
$blocks['materio_search_api_search'] = array(
|
||||
'info' => t('Materio search api search'),
|
||||
'cache' => DRUPAL_NO_CACHE
|
||||
);
|
||||
|
||||
$blocks['materio_search_api_viewmode'] = array(
|
||||
'info' => t('Materio search api view mode selection'),
|
||||
'cache' => DRUPAL_NO_CACHE
|
||||
);
|
||||
|
||||
return $blocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_block_view().
|
||||
*/
|
||||
function materio_search_api_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 'materio_search_api_search':
|
||||
if (user_access('use materio search api')) {
|
||||
$block['subject'] = t('Search');
|
||||
$block['content'] = theme('materio_search_api_search_block', array());
|
||||
}
|
||||
break;
|
||||
case 'materio_search_api_viewmode':
|
||||
if (user_access('use materio search api viewmode selection')) {
|
||||
$block['subject'] = t('View mode');
|
||||
$block['content'] = theme('materio_search_api_select_viewmode_block', array());
|
||||
}
|
||||
break;
|
||||
}
|
||||
return $block;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_info_alter().
|
||||
*/
|
||||
function materio_search_api_entity_info_alter(&$entity_info) {
|
||||
$entity_info['node']['view modes']['cardsmall'] = array(
|
||||
'label' => t('Small cards'),
|
||||
'custom settings' => TRUE,
|
||||
);
|
||||
|
||||
$entity_info['node']['view modes']['cardmedium'] = array(
|
||||
'label' => t('Medium cards'),
|
||||
'custom settings' => TRUE,
|
||||
);
|
||||
|
||||
$entity_info['node']['view modes']['cardbig'] = array(
|
||||
'label' => t('Big cards'),
|
||||
'custom settings' => TRUE,
|
||||
);
|
||||
|
||||
$entity_info['node']['view modes']['cardfull'] = array(
|
||||
'label' => t('Full cards'),
|
||||
'custom settings' => TRUE,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_node().
|
||||
*/
|
||||
function materio_search_api_preprocess_node(&$vars) {
|
||||
// $vars['theme_hook_suggestions'][] = 'node__'.$vars['view_mode'];
|
||||
// $vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__' . $vars['view_mode'];
|
||||
// dsm($vars, '$vars');
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_preprocess_field().
|
||||
*/
|
||||
function materio_search_api_preprocess_field(&$vars) {
|
||||
//$vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#view_mode'];
|
||||
//$vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#field_type'] . '__' . $vars['element']['#view_mode'];
|
||||
//$vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#field_name'] . '__' . $vars['element']['#view_mode'];
|
||||
// dsm($vars, '$vars');
|
||||
}
|
||||
|
||||
/**
|
||||
* materiobase_search_form()
|
||||
*/
|
||||
function materio_search_api_search_form($form, &$form_state){
|
||||
$form = array();
|
||||
|
||||
$args = arg();
|
||||
$path = array_shift($args);
|
||||
$keys = implode('/', $args);
|
||||
|
||||
$form['searchfield'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#default_value' => $path == 'explore' ? $keys : "", // TODO: set the search page path global or a variable in settings
|
||||
// '#value' => $keys,
|
||||
'#autocomplete_path' => 'materiosearchapi/autocomplete/searchapi',
|
||||
//'#autocomplete_path' => 'materiosearchapi/autocomplete/dbselect',
|
||||
'#size' => 30,
|
||||
'#maxlength' => 1024,
|
||||
);
|
||||
|
||||
$form['create'] = array(
|
||||
'#type' => 'image_button',
|
||||
'#src' => drupal_get_path('module', 'materio_search_api') . '/images/search.png',
|
||||
'#value' => t('Search'),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
function materio_search_api_search_form_validate($form, &$form_state){
|
||||
// dsm($form, '$form');
|
||||
// dsm($form_state, '$form_state');
|
||||
// dsm(strlen($form_state['values']['searchfield']));
|
||||
if (strlen(trim($form_state['values']['searchfield'])) <= 1) {
|
||||
form_set_error('searchfield', 'Please enter at least 2 characters keyword.');
|
||||
}
|
||||
}
|
||||
|
||||
function materio_search_api_search_form_submit($form, &$form_state){
|
||||
$keys = $form_state['values']['searchfield'];
|
||||
// drupal_go_to('base/explore/'.$keys);
|
||||
$form_state['redirect'] = 'explore/'.$keys;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_form_alter().
|
||||
*
|
||||
* Adds language fields to user forms.
|
||||
*/
|
||||
// TODO: user can choose preferred view mode
|
||||
// function materio_search_api_form_alter(&$form, &$form_state, $form_id) {
|
||||
// if (user_access('use materio search api viewmode selection')) {
|
||||
// if ($form_id == 'user_register_form' || ($form_id == 'user_profile_form' && $form['#user_category'] == 'account')) {
|
||||
// materio_search_api_viewmode_selector_form($form, $form_state, $form['#user']);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// function materio_search_api_viewmode_selector_form(&$form, &$form_state, $user) {
|
||||
|
||||
// }
|
||||
|
||||
/**
|
||||
* - - - - - - - - - - - - THEME - - - - - - - - - - - -
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
function materio_search_api_theme($existing, $type, $theme, $path) {
|
||||
return array(
|
||||
'materio_search_api_search_block' => array(
|
||||
'arguments' => array(),
|
||||
'template' => 'materio-search-api-search-block',
|
||||
'path' => drupal_get_path('module', 'materio_search_api').'/templates',
|
||||
),
|
||||
'materio_search_api_select_viewmode_block' => array(
|
||||
'arguments' => array(),
|
||||
'template' => 'materio-search-api-select-viewmode-block',
|
||||
'path' => drupal_get_path('module', 'materio_search_api').'/templates',
|
||||
),
|
||||
'materio_search_api_results' => array(
|
||||
'arguments' => array(),
|
||||
'template' => 'materio-search-api-results',
|
||||
'path' => drupal_get_path('module', 'materio_search_api').'/templates',
|
||||
'variables' => array(
|
||||
'index' => NULL,
|
||||
'results' => array('result count' => 0),
|
||||
'items' => array(),
|
||||
'view_mode' => 'teaser',
|
||||
'keys' => '',
|
||||
// 'page_machine_name' => NULL,
|
||||
'spellcheck' => NULL,
|
||||
'pager' => NULL,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* template_preprocess_materiobase_search_block();
|
||||
*/
|
||||
function template_preprocess_materio_search_api_search_block(&$vars){
|
||||
// dsm($vars, '$vars');
|
||||
$vars['searchform'] = drupal_get_form("materio_search_api_search_form");
|
||||
}
|
||||
|
||||
function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
|
||||
global $user;
|
||||
|
||||
$active = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
|
||||
|
||||
$availableviewmodes = variable_get('availableviewmodes', -1);
|
||||
// dsm($availableviewmodes);
|
||||
|
||||
$entity_infos = entity_get_info();
|
||||
// dsm($entity_infos, 'entity_infos');
|
||||
|
||||
$content = '<div class="btn-group btn-group-vertical">';
|
||||
foreach ($entity_infos['node']['view modes'] as $viewmode => $value) {
|
||||
if(in_array($viewmode, $availableviewmodes)){
|
||||
$content .= '<div class="viewmode-link viewmode-'.$viewmode.($active == $viewmode ? " active" : '').'" rel="'.$viewmode.'"><span class="inner">'.$value['label'].'</span></div>';
|
||||
}
|
||||
}
|
||||
$content .= '</div>';
|
||||
$vars['content'] = $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for preprocessing the variables for the search_api_page_results
|
||||
* template.
|
||||
*
|
||||
* @param array $variables
|
||||
* An associative array containing:
|
||||
* - $index: The index this search was executed on.
|
||||
* - $results: An array of search results, as returned by
|
||||
* SearchApiQueryInterface::execute().
|
||||
* - $keys: The keywords of the executed search.
|
||||
*
|
||||
* @see materio-search-api-results.tpl.php
|
||||
*/
|
||||
function template_preprocess_materio_search_api_results(array &$variables) {
|
||||
// dsm($variables, '$variables');
|
||||
$results = $variables['results'];
|
||||
$keys = $variables['keys'];
|
||||
|
||||
$variables['items'] = $variables['index']->loadItems(array_keys($variables['results']['results']));
|
||||
$variables['result_count'] = $results['result count'];
|
||||
$variables['sec'] = round($results['performance']['complete'], 3);
|
||||
$variables['search_performance'] = array(
|
||||
'#theme' => 'search_performance',
|
||||
'#markup' => format_plural(
|
||||
$results['result count'],
|
||||
'The search found 1 result in @sec seconds.',
|
||||
'The search found @count results in @sec seconds.',
|
||||
array('@sec' => $variables['sec'])
|
||||
),
|
||||
'#prefix' => '<p class="search-performance">',
|
||||
'#suffix' => '</p>',
|
||||
);
|
||||
|
||||
// $variables['search_results'] = array(
|
||||
// '#theme' => 'search_results_list',
|
||||
// 'results' => $variables['results']['results'],
|
||||
// 'items' => $variables['items'],
|
||||
// 'index' => $variables['index'],
|
||||
// 'type' => 'ul',
|
||||
// );
|
||||
|
||||
// dsm($variables, 'variables');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* - - - - - - - - - - - - SEARCH API PAGE - - - - - - - - - - - -
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_block_view_alter().
|
||||
*/
|
||||
function materio_search_api_block_view_alter(&$data, $block) {
|
||||
// this alter prepopulate the search form provide by search_api_page
|
||||
if ($block->module == 'search_api_page') {
|
||||
$page = search_api_page_load($block->delta);
|
||||
$item = menu_get_item();
|
||||
|
||||
if (isset($page->path) && $page->path == $item['path']) {
|
||||
$keys = arg(count(arg(NULL, $page->path)));
|
||||
if ($keys) {
|
||||
$data['content']['keys_' . $page->id]['#default_value'] = $keys;
|
||||
$data['content']['keys_' . $page->id]['#value'] = $keys;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user