0

I have two content types: Case and News.
In node--case.tpl.php, I'm using echo $content['field_case_category'][0]['#markup']; in order to print the taxonomy term for the page and that works fine. In node--news.tpl.php, I can't use echo $content['field_news_category'][0]['#markup'];; in fact, #markup is not even present in this array at all.

  1. Why doesn't this method work in the second case? What can I do to solve it?
  2. Is there a better/right way to print taxonomy terms in nodes?

The error I get is the following:

Notice: Undefined index: #markup in include()

  • Sounds as if there is currently no term set in the node you are checking. Is this possible? Then you'ld maybe have to check isset() and !empty() first. – leymannx Dec 15 '15 at 11:04
  • The two pages are built exactly the same way, so that shouldn't be it if I understand you correctly? – Lenny Dec 15 '15 at 11:08
  • A field could be empty; that doesn't depend from how the pages are built, but from the content a field has for a specific node. – kiamlaluno Dec 15 '15 at 11:22
  • They're set up the same way. If I can render the field without any markup now I'm set. – Lenny Dec 15 '15 at 11:26
1

Please don't print some result directly. You should print the content of field like below.

print drupal_render($content['field_case_category']);

Hope this solves your problem. Also please check whether your node contains the value for the field_case_category field.

  • Yeah, this will work, but it also renders additional divs, does it not? I need to get the taxonomy term in plain text – is that possible with that method? – Lenny Dec 15 '15 at 11:10
  • You can set taxonomy term display as plain text using the manage display section of the specific content type. Also, is that node have some data within that field? – Shubham Dec 15 '15 at 11:17
  • I've already set it to display as plain text instead of a link. But there's still additional divs rendered around the plain text content. – Lenny Dec 15 '15 at 11:20
  • 1
    Sometimes the #markup not have content in it. So, you should use drupal_render for rendering the field and strip out the tags using some php function like strip_tags($str); – Shubham Dec 15 '15 at 11:32
1

The correct way to render a field API field is using render() (print render($content['field_case_category']) or print render($content['field_news_category'])) that, through drupal_render(), is able to correctly handle all the cases, including the case #markup is not set because the field is empty.

Notice that you don't normally need to explicitly print node fields, if not in the case you hide them before calling render($content), and then you need to render them separately. The default node.tpl.php file used from the Node module, or any theme that comes with Drupal.

// node/node.tpl.php
<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>

  <?php print $user_picture; ?>

  <?php print render($title_prefix); ?>
  <?php if (!$page): ?>
    <h2<?php print $title_attributes; ?>><a href="<?php print $node_url; ?>"><?php print $title; ?></a></h2>
  <?php endif; ?>
  <?php print render($title_suffix); ?>

  <?php if ($display_submitted): ?>
    <div class="submitted">
      <?php print $submitted; ?>
    </div>
  <?php endif; ?>

  <div class="content"<?php print $content_attributes; ?>>
    <?php
      // We hide the comments and links now so that we can render them later.
      hide($content['comments']);
      hide($content['links']);
      print render($content);
    ?>
  </div>

  <?php print render($content['links']); ?>

  <?php print render($content['comments']); ?>

</div>
//bartik/templates/node.tpl.php
<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>

  <?php print render($title_prefix); ?>
  <?php if (!$page): ?>
    <h2<?php print $title_attributes; ?>>
      <a href="<?php print $node_url; ?>"><?php print $title; ?></a>
    </h2>
  <?php endif; ?>
  <?php print render($title_suffix); ?>

  <?php if ($display_submitted): ?>
    <div class="meta submitted">
      <?php print $user_picture; ?>
      <?php print $submitted; ?>
    </div>
  <?php endif; ?>

  <div class="content clearfix"<?php print $content_attributes; ?>>
    <?php
      // We hide the comments and links now so that we can render them later.
      hide($content['comments']);
      hide($content['links']);
      print render($content);
    ?>
  </div>

  <?php
    // Remove the "Add new comment" link on the teaser page or if the comment
    // form is being displayed on the same page.
    if ($teaser || !empty($content['comments']['comment_form'])) {
      unset($content['links']['comment']['#links']['comment-add']);
    }
    // Only display the wrapper div if there are links.
    $links = render($content['links']);
    if ($links):
  ?>
    <div class="link-wrapper">
      <?php print $links; ?>
    </div>
  <?php endif; ?>

  <?php print render($content['comments']); ?>

</div>

If your template is already using render($content), and you are not seeing the taxonomy terms, then you need to check the field has not been hidden in the display setting, or a module is hiding it.

  • Thanks. Render does work, but is there a way to print only the string itself? As in no additional markup, <a> or <div> etc? Setting the format to plain text isn't enough to remove the surrounding divs it seems. – Lenny Dec 15 '15 at 11:27

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.