5

I'm trying to change the path of a term before it's displayed on a node page. I'm using this preprocess function in template.php:

function bartik_preprocess_node(&$variables) {
    $variables['field_photo_topic']['0']['taxonomy_term']->uri['path'] = 'photo/test';
}

The path won't change, what am I missing? This is the dpm output:

enter image description here

  • I have posted a solution on your original question. What you are trying to do here, as suggested by reptilex, is not going to work. The value you are manipulating exists only for convenience, and will have no effect on the actual system path. – Letharion Jan 8 '12 at 12:00
  • 1
    @Letharion: There is another item in the render array that can be modified to change the path on the rendered object that will affect the final page output. This makes sense if you want to change the link to point to a view instead of the taxonomy term itself. See my answer below. – greg_1_anderson Jan 10 '12 at 22:49
9
+50

This question makes perfect sense if photo/% and video/% are views. It might be even better to map taxonomy/term/% to topic/%, and then make a single view with a path topic/%/%, where one argument is the content type and the other is the taxonomy term. However, you may also use the paths you suggest by building two views (or two pages in one view), each with its own path.

On to your main question, though. Changing items in the render array is easy, but it can sometimes be hard to figure out what needs to change and when. It is a general problem with render arrays that sometimes there are shadow copies of info that are available for your reference. Changing these copies does not affect the rendering of the page, because they are only copies of the item that is used to build the actual page. The problem with the code in your question is that it was altering a copy of the target link; the master information that you need to change to affect page rendering appears somewhere else in the data structure. You need to adjust the copy that appears inside the 'content' item.

Here is some code that works for a similar content type:

function MYTHEME_preprocess_node(&$vars) {
  if (array_key_exists('content', $vars)) {
    $content = &$vars['content'];
      if (array_key_exists('field_photo_topic', $content)) {
        foreach ($content['field_photo_topic']['#items'] as $index => $info) {
          $tid = $info['tid'];
          $content['field_photo_topic'][$index]['#href'] = 'photos/' . $tid;
        }
      }
  }
}

From here, you can use pathauto to map photos/% to a pleasing URL to your photos + taxonomy term view.

  • awesome, that was exactly what I was looking for. I changed it slightly to user $term instead of $tid. Thank you so much!!! – uwe Jan 11 '12 at 2:19
9

Just found this solution based on hook_entity_info_alter(): http://erikwebb.net/blog/changing-url-all-taxonomy-terms. Basically:

function YOUR_MODULE_entity_info_alter(&$entity_info) {
    $entity_info['taxonomy_term']['uri callback'] = 'YOUR_MODULE_taxonomy_term_uri';
}

and

function YOUR_MODULE_taxonomy_term_uri($term) {
    return array('path' => 'your/custom/path/' . $term->tid);
}
  • Generally speaking, this is the preferred way to override term paths, although it will do it more generally, across the whole site, which might not suit the OP in this particular case. In Drupal 6 the equivalent of this was hook_term_path(). – rooby Feb 1 '17 at 6:05
2

Why not create a View to create a block for the terms of a node? You can use a contextual filter to get the terms associated with the node and display those terms. Then using Views you can call the term ID field and the term name field and rewrite the terms to go to your custom URL.

<a href="custom/path/[tid]">[term_name]</a>
  • Oh, and I should mention, display the view as a block on your node (replacing the core functionality of terms displaying at the bottom of the node) – Aaron Ortega Jan 9 '12 at 11:58
  • that's a great idea. But since I'm showing comments below the node I don't think I can show the view between the node display and the comments. – uwe Jan 9 '12 at 17:12
  • Move them to a block. midwesternmac.com/blogs/jeff-geerling/… – Aaron Ortega Jan 10 '12 at 0:27
1

Have you considered Pathauto pattern for taxonomy? Once the module is installed and configured, it also allows you to override individual URLs per term. This would be much more sane system-wise.

Having read your other post, I would suggest that for your content types (photo, video) you enter in Pathauto pattern section:

photo/[term:url:path]

video/[term:url:path]

then rebuild the patterns for all entities and see if that works. If that does not work, then please specify what exactly - 'cause I'm afraid I might not understand your issue well enough.

-1

First, I don't understand why you would do this, the taxonomy is still going to have this path, your theme is just going to show a wrong path for it if it worked. Have you tried using autopath module or changing the path in the taxonomy form? There you can define an alias for the taxonomoy.

If what you are trying is to change the taxonomy of a node, then change the node. What you are doing now is probably working but being overwritten by some other process. But I think you might want to provide more details, since your question is not very clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.