You are here

function commerce_search_api_generate_facets in Commerce Search API 7

Generate and enable Facetapi facets with some defaults.

Parameters

$index_machine_name: The machine name of the index.

Return value

array An array of facets exports with some default configurations.

1 call to commerce_search_api_generate_facets()
commerce_search_api_facetapi_default_facet_settings in ./commerce_search_api.facetapi_defaults.inc
Implements hook_facetapi_default_facet_settings().
2 string references to 'commerce_search_api_generate_facets'
commerce_search_api_admin_settings_form in includes/commerce_search_api.admin.inc
Form constructor for the Commerce Search API form.
commerce_search_api_facetapi_default_facet_settings in ./commerce_search_api.facetapi_defaults.inc
Implements hook_facetapi_default_facet_settings().

File

includes/commerce_search_api.callbacks.inc, line 200
Commerce Search API callbacks.

Code

function commerce_search_api_generate_facets($index_machine_name) {
  $export = array();
  $searcher = 'search_api@' . $index_machine_name;
  $index = search_api_index_load($index_machine_name);
  $adapter = facetapi_adapter_load($searcher);
  if (empty($adapter) || empty($index) || !empty($index) && !$index
    ->server()
    ->supportsFeature('search_api_facets')) {
    return $export;
  }
  $product_attributes = commerce_info_fields('taxonomy_term_reference', 'commerce_product');
  $realm = facetapi_realm_load('block');
  $search_api_ranges_exists = module_exists('search_api_ranges');
  foreach (facetapi_get_facet_info($searcher) as $field_name => $facet_info) {
    $export_facet = FALSE;
    if (isset($facet_info['field type']) && $facet_info['field type'] == 'taxonomy_term') {
      $widget = 'facetapi_checkbox_links';
      $export_facet = TRUE;
    }
    if ($search_api_ranges_exists && strpos($field_name, ':amount_decimal') !== FALSE) {
      $widget = 'search_api_ranges_ui_slider';
      $export_facet = TRUE;
    }
    if ($export_facet) {
      $facet = $adapter
        ->getFacetSettings($facet_info, $realm);
      if ($facet->export_type == EXPORT_IN_DATABASE) {
        continue;
      }
      $facet->settings['widget'] = $widget;
      $facet->settings['filters'] += array(
        'active_items' => array(
          'status' => 1,
          'weight' => -1,
        ),
      );

      // Adding extra filters to product attribute fields.
      if (!empty($product_attributes)) {
        if (strpos($field_name, ':') !== FALSE) {
          list($prefix, $suffix) = explode(':', $field_name, 2);
          if (isset($product_attributes[$suffix])) {
            $facet->settings['filters'] += array(
              'useless_searches' => array(
                'status' => 1,
                'weight' => 0,
              ),
              'hide_search_start' => array(
                'status' => 1,
                'weight' => 1,
              ),
            );
          }
        }
      }
      $facet->api_version = 1;
      $facet->disabled = FALSE;
      $facet->enabled = TRUE;
      $export[$facet->name] = $facet;
    }
  }
  return $export;
}