You are here

function ajax_facets_add_ajax_js in Ajax facets 7.2

Same name and namespace in other branches
  1. 7.3 ajax_facets.module \ajax_facets_add_ajax_js()
  2. 7 ajax_facets.module \ajax_facets_add_ajax_js()

Add required JS and handle single inclusion.

2 calls to ajax_facets_add_ajax_js()
FacetapiAjaxWidgetCheckboxes::init in plugins/facetapi/ajax_widget_checkboxes.inc
Overrides FacetapiWidgetCheckboxLinks::init().
FacetapiAjaxWidgetSelect::init in plugins/facetapi/ajax_widget_select.inc
Overrides FacetapiWidgetCheckboxLinks::init().

File

./ajax_facets.module, line 51
Ajax facets implementation.

Code

function ajax_facets_add_ajax_js($facet) {
  static $included = FALSE;
  if (!$included) {
    $included = TRUE;
    $module_path = drupal_get_path('module', 'ajax_facets');
    drupal_add_js($module_path . '/misc/ajax_facets.js');
    drupal_add_css($module_path . '/misc/ajax_facets.css');
    $search_path = $facet
      ->getAdapter()
      ->getSearchPath();
    $filter_key = $facet
      ->getAdapter()
      ->getUrlProcessor()
      ->getFilterKey();

    // Note that we add in query only filter params and exclude pages and etc...
    $query = isset($_GET[$filter_key]) ? array(
      $filter_key => $_GET[$filter_key],
    ) : array();
    $views = search_api_current_search();
    $view_name = '';
    $display_name = '';
    if (!empty($views)) {

      // Get display from settings.
      $facet_settings = $facet
        ->getSettings();
      if (!empty($facet_settings->settings['facet_search_ids'])) {
        $first_display_from_settings = reset($facet_settings->settings['facet_search_ids']);
        list(, $view_name, $display_name) = explode(':', $first_display_from_settings);
      }

      // Default search.
      if (empty($view_name) || empty($display_name)) {
        $keys = array_keys($views);
        foreach ($keys as $key) {
          if (substr_count($key, 'search_api_views')) {
            list(, $view_name, $display_name) = explode(':', $key);
          }
        }
      }
    }
    $facet = $facet
      ->getFacet();
    $setting['facetapi'] = array(
      'defaultQuery' => isset($_GET[$filter_key]) ? $_GET[$filter_key] : '',
      'searchPath' => $search_path,
      'index_id' => $facet['map options']['index id'],
      'view_name' => $view_name,
      'display_name' => $display_name,
      'facet_field' => $facet['map options']['field']['key'],
      'searchKeys' => isset($_GET['search_api_views_fulltext']) ? $_GET['search_api_views_fulltext'] : '',
      'applyPath' => url($search_path, array(
        'query' => $query,
      )),
    );
    drupal_add_js($setting, 'setting');
  }
}