CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • HOWTO: Customise the quick search box
Pages: [1]

Author Topic: HOWTO: Customise the quick search box  (Read 5976 times)

abeverley

  • I’m new here
  • *
  • Posts: 12
  • Karma: 3
  • CiviCRM version: 4.1.2
  • CMS version: Drupal 7
  • MySQL version: 5.1.49 (Debian)
  • PHP version: 5.3.3 (Debian)
HOWTO: Customise the quick search box
May 06, 2012, 01:28:05 pm
I thought I'd write this quick tutorial on customising the quick search box, as it seems that it's something that quite a few people want to do, but despite there being various hints on the web, there are no complete instructions.

This is still fairly brief, but hopefully enough to get people up and running.

1. Create a new directory in sites/all/modules/ for custom modules. I called mine civi_custom

2. Create a file in the above directory with the extension .info for the module's details. I called mine quicksearch.info. Add the following contents:

name = Customised Quicksearch
description = Module containing the CiviCRM hooks for customising the Quicksearch
dependencies[] = civicrm
package = CiviCRM
core = 7.x
version = 0.1


3. Create another file in the above directory with the extension .module for the module's code. I called mine quicksearch.module. Add the following contents. Note that the SQL query below is where you do your search customisation (both the search and what's returned). I'm not going to explain that here, but there is another example below and others on the web. Also note that the first part of the function should be the same as your module filename.

<?php
function quicksearch_civicrm_contactListQuery( &$query, $name, $context, $id ) {
  if ($context == 'navigation') {
    $query = "SELECT cc.id as id, CONCAT_WS( ' :: ', sort_name, phone ) as data
              FROM civicrm_contact cc
              LEFT JOIN civicrm_phone phe ON ( cc.id = phe.contact_id AND phe.is_primary = 1 )
              WHERE sort_name LIKE '%$name%' OR external_identifier = '$name'       
              AND cc.is_deleted = 0
              LIMIT 0,10";
  }
}
?>


4. Go to "modules" in the main Drupal administration pages. You should see your new module under the CiviCRM section.

5. Tick to enable and save settings.

That's it - your new custom search should start working when you do a new quick search.

Another custom search example:

$query = "SELECT cc.id as id, CONCAT_WS( ' :: ', sort_name, email, phone, street_address, city, ste.name, coy.name ) as data
        FROM civicrm_contact cc LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )
        LEFT JOIN civicrm_phone phe ON ( cc.id = phe.contact_id AND phe.is_primary = 1 )
        LEFT JOIN civicrm_address sts ON ( cc.id = sts.contact_id AND sts.is_primary = 1 )
        LEFT JOIN civicrm_state_province ste ON ( sts.state_province_id = ste.id )
        LEFT JOIN civicrm_country coy ON ( sts.country_id = coy.id )
        WHERE sort_name LIKE '%$name%' OR external_identifier = '$name'
        OR postal_code LIKE '%$name%' AND cc.is_deleted = 0
        LIMIT 0,10";


Andy Beverley / andy@andybev.com

Keith

  • I post occasionally
  • **
  • Posts: 42
  • Karma: 0
Re: HOWTO: Customise the quick search box
August 14, 2012, 01:16:45 pm
Thanks, Andy,

This was just what I needed to get the idea of modules in civi and drupal

Keith

abeverley

  • I’m new here
  • *
  • Posts: 12
  • Karma: 3
  • CiviCRM version: 4.1.2
  • CMS version: Drupal 7
  • MySQL version: 5.1.49 (Debian)
  • PHP version: 5.3.3 (Debian)
Re: HOWTO: Customise the quick search box
August 14, 2012, 01:20:16 pm
Glad you found it helpful. It took me ages just to work out how the function names should be called!

Tony Horrocks

  • I post occasionally
  • **
  • Posts: 110
  • Karma: 7
    • Fabriko Limited
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 7
Re: HOWTO: Customise the quick search box
August 15, 2012, 05:33:14 am
Nice one
Tony Horrocks
Author of the CiviCRM CookBook https://www.packtpub.com/web-development/civicrm-cookbook

mjpforsberg

  • I’m new here
  • *
  • Posts: 10
  • Karma: 0
  • CiviCRM version: 4.5.x
  • CMS version: Drupal 6.x
  • MySQL version: 5.5.40
Re: HOWTO: Customise the quick search box
November 29, 2012, 01:08:11 am
Thanks for that!

I needed to put the WHERE clause in parentheses in order for it to work correctly.

Aahar

  • I post occasionally
  • **
  • Posts: 79
  • Karma: 3
  • CiviCRM version: 3.4 and 4.0
  • CMS version: Drupal 6.17, 6.x and Drupal 7.x
  • PHP version: 5.2
Re: HOWTO: Customise the quick search box
December 13, 2012, 10:52:30 pm
I think hook_civicrm_contactListQuery can be applied to a custom auto-complete field for contacts.
But in that case, how will I be able to know the value of 'context'?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: HOWTO: Customise the quick search box
December 14, 2012, 01:30:36 am
$context is a param of the hook

the easiest is add a die($context) and see the value when using it in the context you want (might need to use firebug to see what's returned by the ajax call)

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

flug

  • I post frequently
  • ***
  • Posts: 126
  • Karma: 12
Re: HOWTO: Customise the quick search box
December 20, 2012, 11:18:05 am
Quote from: mjpforsberg on November 29, 2012, 01:08:11 am
Thanks for that!

I needed to put the WHERE clause in parentheses in order for it to work correctly.

Yes, like this:

Code: [Select]
$query = "SELECT cc.id as id, CONCAT_WS( ' :: ', sort_name, email, phone, street_address, city, ste.name, coy.name ) as data
        FROM civicrm_contact cc LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )
        LEFT JOIN civicrm_phone phe ON ( cc.id = phe.contact_id AND phe.is_primary = 1 )
        LEFT JOIN civicrm_address sts ON ( cc.id = sts.contact_id AND sts.is_primary = 1 )
        LEFT JOIN civicrm_state_province ste ON ( sts.state_province_id = ste.id )
        LEFT JOIN civicrm_country coy ON ( sts.country_id = coy.id )
        WHERE  ( sort_name LIKE '%$name%' OR external_identifier = '$name'
        OR postal_code LIKE '%$name%' ) AND cc.is_deleted = 0
        LIMIT 0,10";

Otherwise the deleted contacts are pulled up, which is probably not what you want.

flug

  • I post frequently
  • ***
  • Posts: 126
  • Karma: 12
Re: HOWTO: Customise the quick search box
December 20, 2012, 11:20:34 am
Here is another example--in our case almost all contacts are from the same country and state so that info wasn't very helpful, but being able to search by name, email, contact ID, and street address is quite helpful.

Code: [Select]
    $query = "SELECT cc.id as id, CONCAT_WS( ' :: ',  cc.id, sort_name, email,  street_address, city, postal_code ) as data
        FROM civicrm_contact cc LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )
        LEFT JOIN civicrm_address sts ON ( cc.id = sts.contact_id AND sts.is_primary = 1 )
        LEFT JOIN civicrm_state_province ste ON ( sts.state_province_id = ste.id )
        WHERE ( sort_name LIKE '%$name%'
        OR email LIKE '%$name%'
        OR display_name LIKE '%$name%'
        OR  cc.id LIKE '$name'
        OR sts.street_address LIKE '%$name%' )
        AND cc.is_deleted = 0
        LIMIT 0,10";

Aahar

  • I post occasionally
  • **
  • Posts: 79
  • Karma: 3
  • CiviCRM version: 3.4 and 4.0
  • CMS version: Drupal 6.17, 6.x and Drupal 7.x
  • PHP version: 5.2
Re: HOWTO: Customise the quick search box
August 03, 2013, 06:34:00 am
This is how to make this Custom QuickSearch of Contact List in Drupal Block:-

1). Make a CiviCRM profile and use this custom field. (Administer -. Customize Data & Screens -> Profiles)
2) Now take the HTML Snippet of the profile  (In the profile page click on more and select HTML Snippet)
3) Copy this code and paste it in a new Drupal Block. Use Full HTML.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • HOWTO: Customise the quick search box

This forum was archived on 2017-11-26.