1

In learning, I've created a custom content type called story and a custom taxonomy called story_category. In story I have a term reference for story_category.

I can render a taxonomy term associated with a node however it automatically renders as a link to the URL alias instead of my custom taxonomy field.

How can I display a taxonomy term with no link?

My current result is the following.

screenshot

These are the fields for the Story content up.

  1. title
  2. body
  3. field_impactful_story
  4. field_related_fund
  5. field_term_reference
  6. field_summary

These are the fields for the Story Category vocabulary.

  1. name
  2. description
  3. field_story_category_link

This is my page--front.tpl.php template.

<main>
  <div class="container">
    <div class="row">
      <div class="col-sm-8">
        <?php // ------ impactful stories ------ // ?>
        <?php
          $impactfulStoriesQuery = new EntityFieldQuery();
          $impactfulStoriesQuery
            ->entityCondition( 'entity_type', 'node' )
            ->entityCondition( 'bundle', 'story' )
            ->propertyCondition( 'status', 1 )
            ->fieldCondition( 'field_impactful_story', 'value', 1, '=')
            ->propertyOrderBy( 'created', 'DESC' )
            ->range( 0, 2 );
          $impactfulStoriesQueryResults = $impactfulStoriesQuery->execute();
          if ( isset( $impactfulStoriesQueryResults[ 'node' ] ) ) {
            $impactfulStoriesNodeIDs = array_keys( $impactfulStoriesQueryResults[ 'node' ] );
            $impactfulStoriesNodes = node_load_multiple( $impactfulStoriesNodeIDs );
            $impactfulStories = node_view_multiple( $impactfulStoriesNodes, 'full' );
            print render( $impactfulStories );
          }
        ?>
        <?php // ------ end impactful stories ------ // ?>
      </div>
      <div class="col-sm-4">
        <p>Some other content</p>
      </div>
    </div>
  </div>
</main>

This is my node--story.tpl.php template.

<article class="story u-clearfix">
  <img class="story-image u-floatLeft" src="http://lorempixel.com/image_output/people-q-c-200-200-4.jpg" alt="">
  <div class="u-floatRight">
    <h3 class="story-title">
      <?php print render( $title ); ?>
    </h3>
    <p class="story-summary">
      <?php if ( $impactfulStorySummary ): ?>
        <?php print render( $impactfulStorySummary ); ?>
      <?php endif; ?>
    </p>
    <div class="story-meta u-clearfix">
      <?php if ( $impactfulStoryCategory ): ?>
        <?php if ( $impactfulStoryCategoryLink ): ?>
          <a class="u-floatLeft" href="<?php print render ( $impactfulStoryCategoryLink ); ?>">
            <?php print render ( $impactfulStoryCategory ); ?>
          </a>
        <?php endif; ?>
      <?php endif; ?>
      <?php if ( $impactfulStoryRelatedFund ): ?>
        <a class="u-floatRight" href="https://www.giveto.osu.edu/makeagift/?fund=<?php print render( $impactfulStoryRelatedFund ); ?>">
          Give
        </a>
      <?php endif; ?>
    </div>
  </div>
</article>

This is the template.php file for my theme.

<?php
  function osu_preprocess_node( &$variables ) {
    // ------ preprocess impactful story nodes ------ //
    if ( $variables[ 'node' ]->type == 'story' ) {

      // get summary for the story content type
      $summaryItem = field_get_items( 'node', $variables[ 'node' ], 'field_summary' );
      $summaryView = field_view_value( 'node', $variables[ 'node' ], 'field_summary', $summaryItem[ 0 ] );
      $variables[ 'impactfulStorySummary' ] = $summaryView;

      // get the taxonomy term associated with a particular story
      $categoryItem = field_get_items( 'node', $variables[ 'node' ], 'field_term_reference' );
      $categoryView = field_view_value( 'node', $variables[ 'node' ], 'field_term_reference', $categoryItem[ 0 ] );
      $variables[ 'impactfulStoryCategory' ] = $categoryView;

      // get the term id so we can get term.
      // once we have the term when can get a field of that term
      $categoryID = $categoryItem[ 0 ][ 'tid' ];
      $category = taxonomy_term_load( $categoryID );

      $categoryLinkItem = field_get_items( 'taxonomy_term', $category, 'field_story_category_link' );
      $categoryLinkView = field_view_value( 'taxonomy_term', $category, 'field_story_category_link', $categoryLinkItem[ 0 ] );
      $variables[ 'impactfulStoryCategoryLink' ] = $categoryLinkView;

      // get the related fund field associated with a story
      $item = field_get_items( 'node', $variables[ 'node' ], 'field_related_fund' );
      $view = field_view_value( 'node', $variables[ 'node' ], 'field_related_fund', $item[ 0 ] );
      $variables[ 'impactfulStoryRelatedFund' ] = $view;
    }
    // ------ end preprocess impactful story nodes ------ //
  }
?>
1
  • Update Ok, so digging around I've found taxonomy_term_title( $term ) which does get you just a plain text title as long as you provide it a $term. This seems pretty good to me. However $categoryItem = field_get_items( 'node', $variables[ 'node' ], 'field_term_reference' ); $categoryID = $categoryItem[ 0 ][ 'tid' ]; still feels like a dirty way to get a term from a node template.
    – ZCKVNS
    Commented May 28, 2016 at 1:26

2 Answers 2

0

It is considered best practice to keep your template files organized and simple by placing all complicated PHP logic inside of preprocess functions. This will make you happy when you revisit your code down the road. Try hook_preprocess_page() to pass your EntityFieldQuery results as a new variable to your page--front.tpl.php.

Try to preprocess the term via template_preprocess_taxonomy_term(); if that fails you could preprocess the field you are using to display your term via template_preprocess_field().

If you just want to render the term as text you can visit the manage display tab when managing your content type and change the display formatted from link to plain text.

1
  • Thanks for the advice doostinharrell. I'll definitely make sure to preprocess the EFQ and taxonomy term stuff. That's probably where I should be doing my taxonomy_term_title( $term ) call too.
    – ZCKVNS
    Commented May 28, 2016 at 2:14
0

Under manage display you can uncheck link label to the reference entity

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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