function facetapi_tokens in Facet API 7
Same name and namespace in other branches
- 7.2 facetapi.tokens.inc \facetapi_tokens()
Implements hook_tokens().
File
- ./
facetapi.tokens.inc, line 139 - Builds placeholder replacement tokens for searches.
Code
function facetapi_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ('facetapi_results' == $type && !empty($data['facetapi_results']['facetapi_adapter'])) {
$adapter = $data['facetapi_results']['facetapi_adapter'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'keys':
$replacements[$original] = check_plain($adapter
->getSearchKeys());
break;
case 'result-count':
$replacements[$original] = (int) $adapter
->getResultCount();
break;
case 'page-number':
$replacements[$original] = (int) $adapter
->getPageNumber();
break;
case 'page-limit':
$replacements[$original] = (int) $adapter
->getPageLimit();
break;
case 'page-total':
$replacements[$original] = (int) $adapter
->getPageTotal();
break;
case 'offset':
$offset = ($adapter
->getPageNumber() - 1) * $adapter
->getPageLimit();
$replacements[$original] = $offset;
break;
case 'start-count':
$offset = ($adapter
->getPageNumber() - 1) * $adapter
->getPageLimit() + 1;
$total_max = (int) $adapter
->getResultCount();
$replacements[$original] = min($offset, $total_max);
break;
case 'end-count':
$page_max = $adapter
->getPageNumber() * $adapter
->getPageLimit();
$total_max = (int) $adapter
->getResultCount();
$replacements[$original] = min($page_max, $total_max);
break;
case 'search-path':
$replacements[$original] = $adapter
->getSearchPath();
break;
}
}
}
elseif ('facetapi_active' == $type && !empty($data['facetapi_active_item'])) {
$item = $data['facetapi_active_item'];
$adapter = $item['adapter'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'active-value':
$markup = $adapter
->getMappedValue($item['facets'][0], $item['value']);
$text = empty($markup['#html']) ? check_plain($markup['#markup']) : $markup['#markup'];
$replacements[$original] = $text;
break;
case 'active-value-raw':
$replacements[$original] = check_plain($item['value']);
break;
case 'active-pos':
$replacements[$original] = $item['pos'];
break;
case 'facet-label':
if ($facet = facetapi_facet_load($item['facets'][0], $adapter
->getSearcher())) {
$replacements[$original] = check_plain($facet['label']);
}
break;
case 'facet-title':
if ($facet = facetapi_facet_load($item['facets'][0], $adapter
->getSearcher())) {
$facet_settings = $adapter
->getFacet($facet)
->getSettings('block');
$replacements[$original] = isset($facet_settings->settings['title_override']) && !empty($facet_settings->settings['title_override']) ? check_plain($facet_settings->settings['title']) : check_plain($facet['label']);
}
break;
case 'facet-name':
$replacements[$original] = check_plain($item['facets'][0]);
break;
}
}
}
elseif ('facetapi_facet' == $type && !empty($data['facetapi_facet'])) {
$facet = $data['facetapi_facet'];
$options += array(
'adapter' => NULL,
);
foreach ($tokens as $name => $original) {
switch ($name) {
case 'facet-label':
$replacements[$original] = check_plain($facet['label']);
break;
case 'facet-name':
$replacements[$original] = check_plain($facet['name']);
break;
case 'facet-title':
$replacements[$original] = check_plain($facet['label']);
if ($adapter = $options['adapter']) {
$facet_settings = $adapter
->getFacet($facet)
->getSettings('block');
$replacements[$original] = isset($facet_settings->settings['title_override']) && !empty($facet_settings->settings['title_override']) ? check_plain($facet_settings->settings['title']) : check_plain($facet['label']);
}
break;
}
}
}
return $replacements;
}