Where is Drupal 7 termpath?

2020-04-23 04:27发布

问题:

Using the pathauto and token module in Drupal 6 allowed you to create url aliases using a pattern like so: [termpath-raw]/[title-raw].

However, this is not the case in Drupal 7. I understand that D7 is still in alpha, but the beta looks to be here pretty soon and it is soooo much nicer that D6 IMO.

Is this functionality not available yet?

回答1:

In Drupal 7, the word path means something very specific, and apparently something different than what termpath would refer to, and it doesn't look like there's any been action taken to replace the [*path] tokens just yet (although it's a known issue): BIKESHED: Token for a term or menu item's entire tree/hierarchy.

It also looks like it's not going to make it into core, and will stay a part of contrib Token, and even #D7CX-pledged projects have until final release to complete their Drupal 7 ports, which could feasibly be close to the end of the year.



回答2:

Token module co-maintainer here. There's more at work here because taxonomy tokens are not quite simple. They're now fields, and we haven't written token support for D7 fields yet. It is something we have to get done though.



回答3:

I've been breaking my head about this issue for several months now and I finally found a solution that seems to work:

http://drupal.org/node/741914#comment-5025862

In short, I created a custom module that exposes a few extra tokens (which can be used in modules like page title or pathauto). In the code-behind, the tokens are replaced by the full hierarchical taxonomy path of either the node or the taxonomy term (there are tokens aimed for the url and others aimed for the page title).

The actual implementation can be found in the discussion at the linked page.

I hope this might help some people with their own implementations.



回答4:

You can use the taxonomy_entity_index module with patches from the issue queue. The only thing that is really bad is that you have to use Drush command to build the index on working site or somehow reimport the current site's contents.



回答5:

I don't remember in which sandbox project I have found this, but it is the perfect solution.

taxonomy_path_token.info

name = Taxonomy Path Token
description = Taxonomy path token creates a path of parent taxonomy terms of a node
package = Token 
core = 7.x

dependencies[] = token

taxonomy_path_token.module

<?php

/**
 * Implements hook_tokens().
 */
function taxonomy_path_token_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  if (!empty($tokens['taxonomy_path']) && !empty($data['node'])) {
    if(!empty($options['sanitize'])) {
       $sanitize = $options['sanitize'];
    } else {
      $sanitize = FALSE;
    }

    $node = $data['node'];
    $replacements[$tokens['taxonomy_path']] = $sanitize ? check_plain(taxonomy_path_token_get_parents($node)) : taxonomy_path_token_get_parents($node);
  }

  if ($type == 'array' && !empty($data['array'])) {
    $array = $data['array'];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'join-path-except-first':
          module_load_include('inc', 'pathauto');
          $values = array();
          foreach (element_children($array) as $key) {
            $value = is_array($array[$key]) ? render($array[$key]) : (string) $array[$key];
            $value = pathauto_cleanstring($value);
            $values[] = $value;
          }
                    array_shift($values);
          $replacements[$original] = implode('/', $values);
          break;
      }
    }
  }

  return $replacements;
}

/**
 * Implements hook_token_info().
 */
function taxonomy_path_token_token_info() {
  $info['tokens']['node']['taxonomy_path'] = array(
    'name' => t('taxonomy_path'),
    'description' => t('Custom taxonomy_path token.'),
  );

    $info['tokens']['array']['join-path-except-first'] = array(
    'name' => t('Joined path'),
    'description' => t('The array values each cleaned by Pathauto and then joined with the slash into a string that resembles an URL.'),
  );

  return $info;
}

function taxonomy_path_token_get_parents($node) {
  module_load_include('inc','pathauto','pathauto');

    if(!empty($node->field_tags)){
        $tid = current($node->field_tags);
        $tid = $tid[0]['tid'];
    }
    else{
     return '';
    }

  $parents = taxonomy_get_parents_all($tid);
  $paths = array();

  foreach ($parents as $parent) {
    $paths[] = pathauto_cleanstring($parent->name);
  }

    $paths = array_reverse($paths);
    array_shift($paths);
  $pathauto = implode('/', $paths);

  return $pathauto;
}

Then add this "[node:taxonomy_path]/[node:title]" to your pathauto patterns.