Probably every web developer faced with the need to implement a search on the site. Quite a common solution - Apache Solr. In the world of Drupal development, this is no exception. For the integration of Solr with Drupal and the implementation of faceted search, there are modules search_api , search_api_solr and facetapi . But in most cases, we would like the search results and the facet filters to be updated without reloading the page, that is, ajax. And, as usual in the Drupal world, on d.org there is some time and user-tested module (or maybe not proven, as lucky) that does what we need. In this case, ajax_facets . /** * Implements hook_requirements(). */ function ajax_facets_requirements($phase) { $requirements = array(); $t = get_t(); switch ($phase) { case 'runtime': $description = $t('For now browser ajax history feature works only in HTML5 browsers. If you want to get this feature on HTML4 browsers you need to install libraries module and download history.js library.'); $value = $t('Libraries module not installed.'); if (module_exists('libraries')) { if (!libraries_get_path('history.js')) { $description = $t('For now browser ajax history feature works only in HTML5 browsers. If you want to get this feature on HTML4 browsers you need to download history.js library.'); $value = $t('Library history.js not found.'); } else { $description = $t('For now browser ajax history feature works both in HTML4 and HTML5 browsers.'); $value = $t('Works with history.js library'); } } $requirements['ajax_facets_message'] = array( 'title' => $t('Ajax Facets'), 'description' => $description, 'value' => $value, 'severity' => REQUIREMENT_INFO, ); break; } return $requirements; } /** * Add required JS and handle single inclusion. */ function ajax_facets_add_ajax_js($facet) { static $included = FALSE; if (!$included) { ... // Add history.js file if exists. if (module_exists('libraries')) { $history_js_path = libraries_get_path('history.js'); if ($history_js_path) { $history_js_exists = TRUE; drupal_add_js($history_js_path . '/scripts/bundled/html4+html5/jquery.history.js', array('group' => JS_LIBRARY)); } } ... $facet = $facet->getFacet(); $setting['facetapi'] = array( .... 'isHistoryJsExists' => $history_js_exists, ); drupal_add_js($setting, 'setting'); drupal_add_library('system', 'drupal.ajax'); } } /** * Pushes new state to browser history. * * History.js library fires "statechange" event even on API push/replace calls. * So before pushing new state to history we should unbind from this event and after bind again. */ Drupal.ajax_facets.pushState = function (state, title, stateUrl) { // If history.js available - use it. if (Drupal.settings.facetapi.isHistoryJsExists) { var $window = $(window); $window.unbind('statechange', Drupal.ajax_facets.reactOnStateChange); History.pushState(state, title, stateUrl); $window.bind('statechange', Drupal.ajax_facets.reactOnStateChange); } else { // Fallback to HTML5 history object. if (typeof history.pushState != 'undefined') { history.pushState(state, title, stateUrl); } } }; Source: https://habr.com/ru/post/283282/
All Articles