Please enable JS

CiviCRM Contact and Drupal Search API

Indexing a CiviCRM Contact in Search API was missing civicrm_user.
April 15/Brian Young/CiviCRMDrupal 7Software

Why is civicrm_user missing from index?

For some reason, the Drupal User association I added to the CiviCRM Contact Search API index was missing. When I started debugging the index process, the $item->civicrm_user was always null.

Solution, create a custom index filter

I decided to try manually populating the civicrm_user property on the civicrm_contact entity in a custom Search API index filter. That worked!

Tell Search API about our filter

Sample custom Search API index filter

In sample_module.module:

  1. /**
  2.  * Implements hook_search_api_alter_callback_info().
  3.  */
  4. function sample_module_search_api_alter_callback_info() {
  5. $callbacks = array();
  6.  
  7. $callbacks['my_filter_name'] = array(
  8. 'name' => t('Sample index filter process'),
  9. 'description' => t('My entry into the index process so I can customize the items being indexed.'),
  10. 'class' => 'SampleSearchApiIndexFilter',
  11. // We need this thing to run before others. Set it to a very low weight.
  12. 'weight' => -50,
  13. );
  14.  
  15. return $callbacks;
  16. }

Create our index filter class

Create our index filter class in a file called: SampleSearchApiIndexFilter.inc.

  1. class SampleSearchApiIndexFilter extends SearchApiAbstractAlterCallback {
  2.  
  3. /**
  4.   * {@inheritdoc}
  5.   */
  6. public function supportsIndex(SearchApiIndex $index) {
  7. return $this->isCiviContactIndex($index);
  8. }
  9.  
  10. /**
  11.   * {@inheritdoc}
  12.   */
  13. public function alterItems(array &$items) {
  14. // Make sure we're running on the supported type of indexes.
  15. if (!$this->supportsIndex($this->index)) {
  16. return;
  17. }
  18.  
  19. // $index_fields = $this->index->getFields(); /* Not needed in example. */
  20.  
  21. foreach ($items as $id => $item) {
  22. // Super important! We need to index data on teh $item->civicrm_user
  23. // object but for some reason, it never gets populated
  24. $item->civicrm_user = self::getUserAccount($item->contact_id);
  25. }
  26. }
  27.  
  28. /**
  29.   * {@inheritdoc}
  30.   */
  31. public function configurationForm() {
  32. return parent::configurationForm();
  33. }
  34.  
  35. /**
  36.   * {@inheritdoc}
  37.   */
  38. public function configurationFormSubmit(array $form, array &$values, array &$form_state) {
  39. return parent::configurationFormSubmit($form, $values, $form_state);
  40. }
  41.  
  42. /**
  43.   * Detects whether the $index is based on a CiviCRM Contact entity.
  44.   *
  45.   * @param \SearchApiIndex|NULL $index
  46.   * @return bool
  47.   */
  48. protected function isCiviContactIndex(SearchApiIndex $index = null) {
  49. $data_source = $index->datasource();
  50. return $data_source->getEntityType() == 'civicrm_contact';
  51. }
  52.  
  53.  
  54. static function getUserAccount($contact_id) {
  55. $uid = db_select('civicrm_uf_match', 'm')
  56. ->fields('m', array('uf_id'))
  57. ->condition('m.contact_id', $contact_id)
  58. ->execute()->fetchCol();
  59.  
  60. if(!empty($uid)) {
  61. return user_load(reset($uid));
  62. }
  63.  
  64. return null;
  65. }
  66. }
  67.  

Update module's info file

Add our class to sample_module.info so Drupal's autoloader knows about it.

  1. files[] = SampleSearchApiIndexFilter.inc

Clear caches!

  1. drush cc all

Add your filter to the index

I'm assuming you already have a index, you'll need to add your filter to it.

Go to your index, click on FILTERS, then look for your custom filter under the "DATA ALTERATIONS" group. Tick the box next to it, save, then re-index your content.



RELATED POSTS


Comments/ 0


LEAVE A COMMENT

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.
But why?

My random thoughts and documentation on the various hardware and software projects that occupy my day-to-day. My only hope is that other people can benefit from my notes.

Recent posts
Categories