Issue #2222817 by dasjo, rv0: Rename alter hook for external coders.
[project/facetapi_pretty_paths.git] / plugins / coders / facetapi_pretty_paths_coder_taxonomy_pathauto.inc
1 <?php
2
3 /**
4 * @file
5 * A taxonomy specific coder based on path aliases of terms.
6 *
7 * This handler reuses the path aliases of terms and hence avoids term ids in
8 * the facet URLs. It requires the default pattern for taxonomy terms
9 * ([term:vocabulary]/[term:name]), which can be configured at
10 * admin/config/search/path/patterns. The alias of [term:name] will be reused
11 * and combined with the facet alias ([facet:alias]/[term:name]).
12 */
13
14 /**
15 * Taxonomy pathauto specific implementation of FacetApiPrettyPathsCoder.
16 */
17 class FacetApiPrettyPathsCoderTaxonomyPathauto extends FacetApiPrettyPathsCoderDefault {
18
19 /**
20 * Taxonomy pathauto special case: <facet alias>/<term-name alias>
21 *
22 * @see FacetApiPrettyPathsCoderDefault::encodePathSegment()
23 */
24 public function encodePathSegment(array $args) {
25 $voc_alias = $this->getVocabularyPathAlias($args['facet'], $args['adapter']);
26 if ($voc_alias) {
27 if ($term = taxonomy_term_load($args['segment']['value'])) {
28 // Get the alias ([term:vocabulary]/[term:name]) for this term and
29 // extract the term:name part.
30 $alias = drupal_lookup_path('alias', 'taxonomy/term/' . $term->tid);
31 if ($alias) {
32 $parts = explode('/', $alias);
33 if (count($parts) == 2) {
34 $args['segment']['value'] = $parts[1];
35 }
36 }
37 }
38 }
39 return parent::encodePathSegment($args);
40 }
41
42 /**
43 * Taxonomy pathauto special case: <facet alias>/<term-name alias>
44 *
45 * @see FacetApiPrettyPathsCoderDefault::decodePathSegmentValue()
46 */
47 public function decodePathSegmentValue(array $args) {
48 $voc_alias = $this->getVocabularyPathAlias($args['facet'], $args['adapter']);
49 if ($voc_alias) {
50 // Rebuild the term alias, get the source (taxonomy/term/[term:tid]) and
51 // extract the term id.
52 $source = drupal_lookup_path('source', $voc_alias . '/' . $args['value']);
53 if ($source) {
54 $exploded = explode('/', $source);
55 if (count($exploded) == 3) {
56 $args['value'] = $exploded[2];
57 }
58 }
59 else {
60 // If term doesn't exist, just make the value 0. This can help avoid
61 // errors down the line if searches are expecting integer values
62 $args['value'] = 0;
63 }
64 }
65 return parent::decodePathSegmentValue($args);
66 }
67
68 /**
69 * Helper function that returns the path alias for a vocabulary.
70 */
71 private function getVocabularyPathAlias($facet_info, $adapter) {
72 static $aliases = array();
73 if (!isset($aliases[$facet_info['name']])) {
74 $aliases[$facet_info['name']] = FALSE;
75 $facet_settings = $adapter->getFacetSettingsGlobal($facet_info);
76 $voc = taxonomy_vocabulary_machine_name_load($facet_settings->settings['pretty_paths_taxonomy_pathauto_vocabulary']);
77 if ($voc && module_exists('pathauto')) {
78 // Needed, as of http://drupal.org/node/907578#comment-5564008
79 require_once drupal_get_path('module', 'pathauto') . '/pathauto.inc';
80 $aliases[$facet_info['name']] = pathauto_cleanstring($voc->name);
81 }
82 }
83 return $aliases[$facet_info['name']];
84 }
85 }