0

Problem/Motivation

I am using Search API with SOLR Search API integration. Currently i have one node index showing content type Listing and one faceted block attached to it (category). My idea is to create another Faceted Block that will filter by starting letter of Listing title.

I think that i have tried everything (then again maybe not...) except creating new indexed field, trimming title to first letter and then using that to filter titles.

rinasek
  • 31
  • 4
  • Filter by content type is configurable but if you want to filter by 1st letter of title then you need to do custom implementation. – Sumoanand Jul 11 '13 at 13:33
  • Seems that your last approach would complete your purpose with ease. The module doesn't have hooks that I know of that can help you, so you would end up having to modify some SearchApiFacet classes. I've done it and it's not pretty and not good practice. – Miguel Lomelí Jul 11 '13 at 18:36
  • Can you give an example ? would help to understand the problem – Jayendra Jul 12 '13 at 04:28

1 Answers1

3

This solved my problems. Its adding another field to index and you can create facet out of it.

function wtc_glossary_search_api_alter_callback_info() {
  $callbacks['wtc_glossary_alter_add_first_letter_title'] = array(
    'name' => t('First letter of listing title'),
    'description' => t("This module provides first letter of title for glossary view."),
    'class' => 'WtcAlterAddFirstLetter',
  );

  return $callbacks;
}

/**
 * Search API data alteration callback that adds the first letter of title  for glossary mode
 */
class WtcAlterAddFirstLetter extends SearchApiAbstractAlterCallback {

  public function alterItems(array &$items) {
    foreach ($items as $id => &$item) {
      if (!isset($item->FIELD_YOU_NEED)) {
        $item->search_api_title_first_letter = NULL;
        continue;
      }
      $item->search_api_title_first_letter = substr($item->FIELD_YOU_NEED,0,1);
    }
  }

  public function propertyInfo() {
    return array(
      'search_api_title_first_letter' => array(
        'label' => t('First Letter of FIELD_YOU_NEED'),
        'description' => t('For for listings in glossary mode.'),
        'type' => 'text',
      ),
    );
  }

}
rinasek
  • 31
  • 4
  • Thank you so much for taking the time to post your solution to your own question. The only example I could find on the web :) – Alex Kirsten Sep 08 '14 at 14:24