Last active
January 28, 2016 20:07
-
-
Save ezheidtmann/2ed8dbc9a467a17aab15 to your computer and use it in GitHub Desktop.
Custom FacetAPI url processor for project-specific and search-specific overrides
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function mycustomurlprocessor_module_implements_alter(&$implementations, $hook) { | |
if ($hook == 'facetapi_searcher_info_alter') { | |
# move our implementation to the end of the list so we can override the | |
# alterations made by facetapi_pretty_paths_facetapi_searcher_info_alter | |
$group = $implementations['mycustomurlprocessor']; | |
unset($implementations['mycustomurlprocessor']); | |
$implementations['mycustomurlprocessor'] = $group; | |
} | |
} | |
/** | |
* Implements hook_facetapi_url_processors() | |
*/ | |
function mycustomurlprocessor_facetapi_url_processors() { | |
return array( | |
'mycustomurlprocessor_urlprocessor' => array( | |
'handler' => array( | |
'label' => t('Project Custom URL Processor'), | |
'class' => 'FacetapiUrlProcessorMyCustom', | |
), | |
), | |
); | |
} | |
function mycustomurlprocessor_facetapi_searcher_info_alter(array &$searcher_info) { | |
foreach ($searcher_info as &$info) { | |
if ($info['name'] === 'search_api@default_node_index') { | |
$info['url processor'] = 'mycustomurlprocessor_urlprocessor'; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FacetapiUrlProcessorMyCustom extends FacetapiUrlProcessorPrettyPaths { | |
public function getFacetPath(array $facet, array $values, $active) { | |
# see FacetapiUrlProcessorPrettyPaths::getFacetPath. This implementation is | |
# exactly the same, except we call $this->alterFacetPathSegments before | |
# rendering the final path. | |
$segments = $this->pathSegments; | |
$active_items = $this->adapter->getActiveItems($facet); | |
$settings = $this->adapter->getFacetSettingsGlobal($facet); | |
// Adds to segments if inactive, removes if active. | |
foreach ($values as $value) { | |
$segment = $this->getPathSegment($facet, $value); | |
if ($active && isset($active_items[$value])) { | |
unset($segments[$segment['key']]); | |
} | |
elseif (!$active) { | |
$segments[$segment['key']] = $segment; | |
} | |
} | |
// If Facet API supports limiting active items, remove path segments. See http://drupal.org/node/1393928 | |
if (count($active_items) && isset($settings->settings['limit_active_items']) && $settings->settings['limit_active_items']) { | |
foreach ($active_items as $active_item) { | |
$segment = $this->getPathSegment($facet, $active_item['value']); | |
unset($segments[$segment['key']]); | |
} | |
} | |
$this->alterFacetPathSegments($facet, $values, $active, $segments); | |
$path = $this->constructPath($segments); | |
return $path; | |
} | |
public function alterFacetPathSegments(array $facet, array $values, $active, array &$segments) { | |
# This code is project-specific and is determined by client requirements. Here, we | |
# have facet dependencies and we want dependent facets to be removed from the search | |
# when the parent facet is removed. | |
$dependent_facets = array('field_topic'); | |
if ($facet['name'] === 'field_type') { | |
# exclude dependent facets | |
foreach ($segments as $key => $segment) { | |
if (in_array($segment['facet']['name'], $dependent_facets)) { | |
unset($segments[$key]); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment