Drupal: Custom search with filters

×

Error message

  • Deprecated function: Return type of DatabaseStatementBase::execute($args = [], $options = []) should either be compatible with PDOStatement::execute(?array $params = null): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2244 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).
  • Deprecated function: Return type of DatabaseStatementEmpty::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in require_once() (line 2346 of /var/www/html/drupaldeveloper.in/includes/database/database.inc).

This CodeLet has a search box with three checkboxes as filters. Depending on the fitler selected, result will be fetched. By default, this CodeLet will dispaly 10 most recent results from groups and users

Drupal: Custom search with filters | Drupal Developer
CodeLet: 

<?php

/**
* @file
*
* Provides a page to search for groups, users and service providers
*
* @author DrupalD
*/

/**
* Implemenation of hoook_menu
*
* @author DrupalD
*/
function invest_search_menu($ss__keyword = '') {
$items['directory'] = array(
'title' => t('Directory'),
'menu_name' => 'main-menu',
'page callback' => 'invest_search_page',
'page arguments' => array(1),
'access callback' => TRUE,
'weight' => -5,
);

return $items;
}

/**
* invest_search_page
*/
function invest_search_page($ss__keyword= '') {
if (!empty($ss__keyword)) {
list($key, $type) = explode('type:', $ss__keyword);
}
$build['invest_search_form'] = drupal_get_form('invest_search_form', $ss__keyword);
$ss__keyword = ($type == '') ? $key .'type: group users': $key . 'type:'. $type;
$build['search_result'] = array('#markup' => _get_invest_search_result($ss__keyword));
return drupal_render($build);
}

/**
* invest_search_form
*
* Search form for searching the content
*
* @author DrupalD
*/
function invest_search_form($form, &$form_sate, $ss__keyword = '') {
$form = array();
if (!empty($ss__keyword)) {
list($key, $type) = explode('type:', $ss__keyword);
}

$form['#action'] = 'directory';
$form['name'] = array(
'#type' => 'hidden',
'#value' => 'invest_search_page',
);

$form['search_type'] = array(
'#type' => 'checkboxes',
'#title' => t('Search in'),
'#multicolumn' => array('width' => 3),
'#default_value' => !empty($type) ? explode(' ', $type) : array('group', 'users'),
'#options' => array(
'group' => t('Groups'),
'users' => t('Users'),
'provider' => t('Service provider'),
),
'#weight' => -9,
);

$form['search_keywrod'] = array(
'#type' => 'textfield',
'#title' => t('Search'),
'#default_value' => $key,
'#weight' => -10,
);

$form['search_submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#weight' => -8,
);

$form['#submit'] = array('invest_search_submit');
return $form;
}

/**
* invest_search_submit
*
* @author DrupalD
*/
function invest_search_submit($form, &$form_state) {
$ss__keyword = $form_state['values']['search_keywrod'];
$ss__type = implode(' ', $form_state['values']['search_type']);
$keys = $ss__keyword .'type:'. $ss__type;
if ($ss__type == '0 0 0') {
form_set_error('search_type', t('Please select a type to search in.'));
}

$form_state['redirect'] = $form_state['complete form']['#action']
. '/' . $keys;
}

/**
* _get_invest_search_result
*
* @param unknown_type $ss__keyword
* @author DrupalD
*/
function _get_invest_search_result($ss__keyword) {
$type = '';
$header = array(t('Name'), t('Body'), t('Is service provider?'));

if (!empty($ss__keyword)) {
unset($type);
list($key, $type) = explode('type:', $ss__keyword);
$key = trim($key);
}

if (strstr($type, 'group') != '' || strstr($type, 'provider') != '') {
unset($row);
$group_query = db_select('node', 'n');
$group_query->leftjoin('field_data_body', 'fdr', 'n.nid = fdr.entity_id');
$group_query->leftjoin('field_data_field_service_provider', 'fsp', 'n.nid = fsp.entity_id');
if (strstr($type, 'provider') != '') {
$group_query->condition('field_service_provider_value', '1', '=');
}

$group_query->fields('n', array('nid', 'title'))
->fields('fdr', array('body_value'))
->fields('fsp', array('service_provider' => 'field_service_provider_value'))
->condition('type', 'group', '=')
->condition('fsp.bundle', 'group', '=')
->condition('n.status', '1', '=');
if ($key != '') {
$group_query->where("((title LIKE '%". $key ."%') OR (body_value LIKE '%". $key ."%'))");
}

$group_query->orderBy('title', 'ASC');
$group_result = $group_query->extend('PagerDefault')->limit(5);
$group_result = $group_query->execute();
while($group_record = $group_result->fetchAssoc()) {
$row[] = array(
l($group_record['title'], 'node/'. $group_record['nid']),
$group_record['body_value'],
$group_record['field_service_provider_value'] == 1 ? t('Yes') : t('No'),
);
}
}

if (strstr($type, 'users') != '' || strstr($type, 'provider') != '') {
$user__query = db_select('users', 'u');
$user__query->leftjoin('field_data_field_notes', 'fdn', 'u.uid = fdn.entity_id');
$user__query->leftjoin('field_data_field_service_provider', 'fsp', 'u.uid = fsp.entity_id');
if (strstr($type, 'provider') != '') {
$user__query->condition('field_service_provider_value', '1', '=');
}
$user__query->fields('u', array('name', 'uid'))
->fields('fdn', array('field_notes_value'))
->fields('fsp', array('field_service_provider_value'))
->condition('fsp.bundle', 'user', '=');
if ($key != '') {
$user__query->where("((name LIKE '%". $key ."%') OR (field_notes_value LIKE '%". $key ."%'))");
}
$user__query->orderBy('name', 'ASC');
$user__query = $user__query->extend('PagerDefault')->limit(5);
$user__query = $user__query->execute();
while($user__record = $user__query->fetchAssoc()) {
$row[] = array(
l($user__record['name'], 'user/'. $user__record['uid']),
$user__record['field_notes_value'],
$user__record['field_service_provider_value'] == '1' ? t('Yes') : t('No'),
);
}
}

$output .= theme('table', array('header' => $header, 'rows' => $row, 'empty' => t('No records found'), 'caption' => ''. t('Result') .''));
$output .= theme('pager', array('tags' => array()));
return $output;
}
?>

Compatibility: 
Is CodeLet useful?: 
Drupal: Custom search with filters | Drupal Developer

Error

The website encountered an unexpected error. Please try again later.