You are here

class FacetApiPrettyPathsCoderTaxonomyPathauto in Facet API Pretty Paths 7

Taxonomy pathauto specific implementation of FacetApiPrettyPathsCoder.

Hierarchy

Expanded class hierarchy of FacetApiPrettyPathsCoderTaxonomyPathauto

1 string reference to 'FacetApiPrettyPathsCoderTaxonomyPathauto'
facetapi_pretty_paths_facetapi_pretty_paths_coders in ./facetapi_pretty_paths.module
Implements hook_facetapi_pretty_paths_coders().

File

plugins/coders/facetapi_pretty_paths_coder_taxonomy_pathauto.inc, line 17
A taxonomy specific coder based on path aliases of terms.

View source
class FacetApiPrettyPathsCoderTaxonomyPathauto extends FacetApiPrettyPathsCoderDefault {

  /**
   * Taxonomy pathauto special case: <facet alias>/<term-name alias>
   *
   * @see FacetApiPrettyPathsCoderDefault::encodePathSegment()
   */
  public function encodePathSegment(array $args) {
    $voc_alias = $this
      ->getVocabularyPathAlias($args['facet'], $args['adapter']);
    if ($voc_alias) {
      if ($term = taxonomy_term_load($args['segment']['value'])) {

        // Get the alias ([term:vocabulary]/[term:name]) for this term and
        // extract the term:name part.
        $alias = drupal_lookup_path('alias', 'taxonomy/term/' . $term->tid);
        if ($alias) {
          $parts = explode('/', $alias);
          if (count($parts) == 2) {
            $args['segment']['value'] = $parts[1];
          }
        }
      }
    }
    return parent::encodePathSegment($args);
  }

  /**
   * Taxonomy pathauto special case: <facet alias>/<term-name alias>
   *
   * @see FacetApiPrettyPathsCoderDefault::decodePathSegmentValue()
   */
  public function decodePathSegmentValue(array $args) {
    $voc_alias = $this
      ->getVocabularyPathAlias($args['facet'], $args['adapter']);
    if ($voc_alias && $args['value'] != '!') {

      // Rebuild the term alias, get the source (taxonomy/term/[term:tid]) and
      // extract the term id.
      $source = drupal_lookup_path('source', $voc_alias . '/' . $args['value']);
      if ($source) {
        $exploded = explode('/', $source);
        if (count($exploded) == 3) {
          $args['value'] = $exploded[2];
        }
      }
      else {

        // If term doesn't exist, just make the value 0. This can help avoid
        // errors down the line if searches are expecting integer values
        $args['value'] = 0;
      }
    }
    return parent::decodePathSegmentValue($args);
  }

  /**
   * Helper function that returns the path alias for a vocabulary.
   */
  private function getVocabularyPathAlias($facet_info, $adapter) {
    static $aliases = array();
    if (!isset($aliases[$facet_info['name']])) {
      $aliases[$facet_info['name']] = FALSE;
      $facet_settings = $adapter
        ->getFacetSettingsGlobal($facet_info);
      $voc = taxonomy_vocabulary_machine_name_load($facet_settings->settings['pretty_paths_taxonomy_pathauto_vocabulary']);
      if ($voc && module_exists('pathauto')) {

        // Needed, as of http://drupal.org/node/907578#comment-5564008
        require_once drupal_get_path('module', 'pathauto') . '/pathauto.inc';
        $aliases[$facet_info['name']] = pathauto_cleanstring($voc->name);
      }
    }
    return $aliases[$facet_info['name']];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FacetApiPrettyPathsCoderDefault::decodePathSegmentAlias public function Decode the alias part of a path segment in order to interpret it from a given pretty path.
FacetApiPrettyPathsCoderDefault::prettyPath protected function Convert a given text to a pretty path using pathauto, if available.
FacetApiPrettyPathsCoderTaxonomyPathauto::decodePathSegmentValue public function Taxonomy pathauto special case: <facet alias>/<term-name alias> Overrides FacetApiPrettyPathsCoderDefault::decodePathSegmentValue
FacetApiPrettyPathsCoderTaxonomyPathauto::encodePathSegment public function Taxonomy pathauto special case: <facet alias>/<term-name alias> Overrides FacetApiPrettyPathsCoderDefault::encodePathSegment
FacetApiPrettyPathsCoderTaxonomyPathauto::getVocabularyPathAlias private function Helper function that returns the path alias for a vocabulary.