4

Using entity reference, i have been able to link a node of type blog to another node of my custom content type, "challenge", by creating a field under blog of type entity reference. Great. Now, when the blog is published, some fields (not all) from the linked custom content type should also be displayed there. For ex, in my custom content type, there are some fields as title,body,image1,image2,sponsor, some flags using the flag module and other fields. I want that only some fields which i mentioned should be displayed on the blog (no flags etc to be displayed). SO what i do is under Manage Display for blog i select Rendered Entity for display. What this does is gets all the fields from the referenced/linked node. However i want some fields not all. Now what i do is write a custom node--blog.tpl.php. I then hide everything from content as:

<?php
   hide($content['title']);
   hide($content['field_blog_pic']);
   hide($content['field_challenge_name']);
   hide($content['field_challenge_pic']);
   hide($content['field_sponsor']);
   hide($content['field_sponsor_logo']);
   hide($content['field_sponsor_description']);
 ?>

Then i tried rendering the fields one by one. It is ok for the field of type blog but the moment i do a print_render($content['field_challenge_name']) then all fields from the referenced node (of my custom content type, challenge) get displayed. How can i just select a few fields for display of the referenced node.

If it helps, here's my node--blog.tpl.php:

    <div class ="in-blog-pic">
     <?php
       print render($content['field_blog_pic']);
     ?>
    </div>

    <div class ="in-blog-title">
     <?php
        print $title;
     ?>
    </div>

    <div class="in-blog-comment-count">
     <?php
        print $comment;
     ?>
    </div>

    <div class="in-blog-name-user">
     <?php
        print $name;
     ?>
    </div>

    <?php
    /** $user = user_load($user->uid);
      print theme_image_style(
                    array(

                        'path' => $user->picture->uri,
                        'attributes' => array(
                            'class' => 'in-blog-user'
                        )            
                    )
                );        
    */
    ?>
    <div class="in-blog-user-pic">
 <?php
   $user = user_load($uid);
   print theme('user_picture', array('account' =>$user));
 ?>
</div>

<div class="in-blog-title-below">
 <?php
  print $title;
 ?>
</div>

<?php
   hide($content['title']);
   hide($content['field_blog_pic']);
   hide($content['field_challenge_name']);
   hide($content['field_challenge_pic']);
   hide($content['field_sponsor']);
   hide($content['field_sponsor_logo']);
   hide($content['field_sponsor_description']);
   hide($content['field_categories']);
?>
<div class="in-blog-body">
 <?php
  print render($content);
 ?>
</div>

<?php /*
<div class="in-blog-challenge-name">
  <?php
     print render($content['field_challenge_name']);
  ?>
</div>
*/?>

All fields get displayed of the referenced node once i do the last few lines

<div class="in-blog-challenge-name">
      <?php
         print render($content['field_challenge_name']);
      ?>
    </div>
    ?>

So after all that blabbering, all i want to ask is how to print some selected fields of a referenced node (here my custom type, "challenge") onto the referencing node (here, blog)...

Regards

  • I would like to do it through .tpl.php itself only rather that using any module..if not then ofcourse i will look into the module – why Mar 12 '13 at 12:58
  • ok ...i did it using help of display suite as suggested by @Alfred Armstrong...but i would still like to know how to do so by tpl.php and therefore i am not just yet accepting the answer..will look do that in a couple of days..but thanks man – why Mar 13 '13 at 6:26
4

I would use Display Suite for this problem. Using it, you can define an extra view mode for the referenced node type, with just the fields you want, then configure the display of the entity reference field to use that view mode. We are doing this in the current project I am working on, and it's just the ticket.

  • Thoroughly endorse this recommendation. It's a well maintained, well documented and easy to use module. For the task in hand, it makes it quite literally a five minute job. – Obliquely Mar 12 '13 at 23:10
  • hi..can you elaborate a bit on this – why Mar 13 '13 at 4:38
  • i am not sure about creating a new view mode...will look into it...if you can share some tips... – why Mar 13 '13 at 5:17
  • i went to display suite and created a new view mode...and selected the entity type as node...howevere what fields do i add...i am confused here – why Mar 13 '13 at 5:24
  • Glad to be of service. I guess you are out of my timezone as I didn't see your comments until now. Display Suite has a lot of features but it is quite well documented so hopefully you'll find what you need. – Alfred Armstrong Mar 13 '13 at 10:37
0

Try this module field extractor For more advanced features use views eva

  • ...hey thanks man...I would like to do it through .tpl.php itself only rather that using any module..if not then ofcourse i will look into the module – why Mar 12 '13 at 12:59
0

I found your question trying to solve a similar problem - I want to grab an image from a referenced node without having to render all the rest of the content. I wanted to do it without adding another module to my already heavy install, and I ended up doing it with a custom display mode instead of template files.

The Token module - now in Core in Drupal 7 - lets you choose custom formatting for a given field in a "Tokens" display mode. Say, if you wanted an image to just link to its full-sized version in its own content type, but you wanted to be able to reference it in another and set it to link through to its original content there. See this post: https://www.drupal.org/node/1299662

It also makes it easy to only render a single field from the referenced content:

On the original content type (the one you're referencing content FROM), go to "Manage Display" and create a new view mode, Tokens. It's the bottom of the list of options - just tick the checkbox:

enter image description here

From there, you just get a new place to manage display settings: hide everything you don't want and configure it as you will:

enter image description here

Then, go back to the content type you want to add a reference into and click "Manage Display". In the display settings for your Node Reference field, select Format > Rendered Content. Select "View Mode: Tokens" and you'll get a nice single field to style as you want!

0

Another way is to use module Entity view modes. It's a lightweight alternative to Display Suite and let's you create custom view modes.

How to create view modes programmatically can be found in this tutorial

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.