hi guys

i have a custom content type and am using filefield to pull the taxonomy term added to the node, im then using the taxonomy term to create a link to another node type with the same name as the term being pulled from from the previous node
i had problems with spaces in the url of nodes that had more than 1 word seperated with a space so i created this php code and placed it in my node.tpl file and it removes the spaces
the codes working fine but im not sure if putting it in the tpl file is the best option as i dont want too many queiries being pulled and slowing the site down, would it be best to implement it in the template.php file and jus call it from my tpl file?

the code i place in the top of my tpl file is:

$custom_url = $node->field_artist_tax[0]['view']; 
$custom_url2 = str_replace(' ', "-", $custom_url); 

and then i use this code to display the link:

<div class="taxonomy-name">
				<strong><?php print t('Artist'); ?> </strong>: 
		      <div class="terms"><a href="/content/artist-page/<?php print $custom_url2 ?>"><?php print $node->field_artist_tax[0]['view'] ?></a></div>
	    </div>

Comments

nevets’s picture

You can move it to hook_preprocess_node() in template.php, you would replace hook with your module name (node the template.php may implement the function already in which can you would add the code to the existing function). Note while it makes the node template file cleaner it does not save time really. Also, if this depends on the node type you would would a test in hook_preprocess_node() which checks for node type. It would look something like

function YOURTHEMENAME_preprocess_node(&$vars) {
  $node = $vars['node'];
  $term_name = $node->field_artist_tax[0]['view'];
  $term_path = str_replace(' ', "-", $term_name);
  $vars['artist_link'] = l($term_name, "/content/artist-page/$term_path");
}

and then in node.tpl.php (or the one specific to the content type) use something like

<div class="taxonomy-name">
<strong><?php print t('Artist'); ?> </strong>:
      <div class="terms"><?php print $artist_link ?></div>
    </div>
prezaeis’s picture

that is perfect.. works brilliantly, thank u so much