0

I'm trying to show all taxonomy terms in a custom template (page--project.tpl.php) that overrides page.tpl.php, but I can't pull it off.

I'd like to use the default rendering that I get when I see with render($page['content']):

drupal default render of taxonomy term list

It seems like I should be able to use render($node->field_project_tags), but I get a bunch of drupal errors on the rendered template page:

Warning: Invalid argument supplied for foreach() in element_children() (line 6590 of /Users/username/sites/d7/includes/common.inc).
Warning: Illegal string offset '#children' in drupal_render() (line 6047 of /Users/username/sites/d7/includes/common.inc).
Warning: Illegal string offset '#children' in drupal_render() (line 6057 of /Users/username/sites/d7/includes/common.inc).
...

What's the cleanest, simplest way to render these taxonomy terms (as links) in a page template?

  • Create view block for display all the taxonomy terms and call the block inside the page.tpl.php by using module_invoke(). – logeshvaran May 3 '17 at 7:14
  • 1
    Are you sure is page.tpl.php? more variables are avilable in node.tpl.php – pinueve May 5 '17 at 23:16
  • @pinueve Yeah, I know. But I want to display the taxonomy in the right sidebar along with other blocks. It seems like node.tpl.php only alters the content that is dropped into the content region (as defined by page.tpl.php). – doub1ejack May 6 '17 at 15:30
  • 1
    You are doing it wrong and this question reflects this :) While it is possible to do what you describe, it's not a good idea to do what you describe. The page template should be used to setup the layout of the page. All/most content should come from blocks or the actual page content. Either you should render the terms from the node while rendering the node, or you should make a block which pulls and display this data which you can place elsewhere on the page if needed. This could be made as a view. Probably something like Panels could help you layout the page as well. – googletorp May 9 '17 at 9:37
  • 1
    From my experience, using standard ways of solving problems usually makes changing ownership a lot easier, rather than having to understand "custom code" in various places, with little to none documentation. – googletorp May 11 '17 at 12:12
1
+50

You can display taxonomy terms in sidebar in multiple methods.

  1. Create a view block with required taxonomy terms and assign that block to any region in required pages.

  2. Create a normal block, load and display taxonomy terms using following code and assign that block to any region in required pages.

    $vocabulary = taxonomy_vocabulary_machine_name_load('vocobulary_name');
    if ($terms = taxonomy_get_tree($vocabulary->vid)) {
      foreach ($terms as $term) {
        $term_name = $term->name;
        // Do required actions.
      }
    }
    
  3. Use the above code in your page template to display taxonomy terms.

Hope this will helps you.

  • It would be most intuitive if these terms were rendered in the template along with all other content fields. Considering @Aladjinn's answer though, is there a significant performance hit from doing so? A block would be confusing to later developers & add minor limitations for page layout. – doub1ejack May 10 '17 at 20:11
  • Also, as I mentioned in the OP, I would prefer to render the term with the default behavior instead of building my own links up. Can I skip the foreach loop and just render($terms) somehow? – doub1ejack May 10 '17 at 20:14
1

Use following code to display manually taxonomy terms:

$vocabulary = taxonomy_vocabulary_machine_name_load('vocobulary_name');
if ($terms = taxonomy_get_tree($vocabulary->vid)) {
  $tags ='';
  foreach ($terms as $term) {
      $taxonomy_path = taxonomy_term_uri($term);
      $tags .= l($term->name, $taxonomy_path['path']);
  }
}
0

Jeetendra Kumar gave the correct answer but You should never ever ever do it in a template, especially in Drupal7 since there is a serious performance hit.

As googletorp mentioned the right way would be to create a custom block that will display Your taxonomies. You should never do such within a template!

Create a custom block with hook_block_info and hook_block_view, and paste the Kumar's code there.

Than You can place that block where it fits Your needs via block management or programmatically.

  • I don't like the idea of rendering some content in a template, but other pieces in a block. I'm thinking of future developers who are (most likely) new to drupal and I think that would be unintuitive & confusing. – doub1ejack May 10 '17 at 20:24
  • 1
    Having a separate block is a good idea if the block is independent but if the terms are coming from a node field then I think it might be better to do that in template itself. – Tejas Vaidya May 11 '17 at 9:30

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.