You are here

function facetapi_luceneapi_luceneapi_document_alter in Facet API 6

Implementation of hook_luceneapi_document_alter().

File

contrib/facetapi_luceneapi/facetapi_luceneapi.module, line 231
The Search Lucene API module's implementation of the the Facet API.

Code

function facetapi_luceneapi_luceneapi_document_alter(Zend_Search_Lucene_Document $doc, $node, $module, $type = NULL) {
  static $fields;
  if ('luceneapi_node' == $module) {

    // Maintains taxonomy ancestry for hierarchical browsing. Parent term counts
    // should also include child terms counts.
    $terms = array();
    if (isset($node->taxonomy) && is_array($node->taxonomy)) {
      foreach ($node->taxonomy as $term) {
        $ancestors = taxonomy_get_parents_all($term->tid);
        foreach ($ancestors as $ancestor) {
          $terms[$ancestor->vid][$ancestor->tid] = $ancestor->tid;
        }
      }
    }

    // Changes the field definition for categories, rebuilds the field's value
    // with all parent and child terms.
    foreach ($terms as $vid => $tids) {
      try {
        $field = $doc
          ->getField('category_' . $vid);
        $field->isStored = FALSE;
        $field->isIndexed = TRUE;
        $field->isTokenized = TRUE;
        $field->value = join(' ', $tids);
      } catch (Exception $e) {
        luceneapi_throw_error($e, WATCHDOG_ERROR, 'facetapi_luceneapi');
      }
    }

    // @todo This is nasty. We need a better way to do this.  Unfortunately, we
    // have to wait until this functionality is integrated into the core Search
    // Lucene API module.
    if (NULL === $fields) {
      $fields = array();
      foreach (facetapi_enabled_facets_get($module) as $facet) {
        $fields[] = $facet['field'];
      }
    }
    foreach ($fields as $field) {
      $variable = 'facetapi:termfreqs_cached:' . $module . ':::' . $field;
      variable_del($variable);
    }
  }
}