Themeing Node Reference Fields

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
akanik's picture

Is there a way to format a node reference field in d6 so that every time it appears it displays not only the title, but a collection of fields from the referenced node? I have a node reference field called field_investigations_reference that is used to reference articles (investigations) so that they can be place on a homepage in a curated way.

Situation: a content type "Homepage" references a variety of articles so that the site homepage can be fully assembled and previewed before items are actually put on the homepage. When the homepage has been successfully created with all the correct articles referenced, the node is then scheduled for publishing. A view has also been set up to filter out the most recently published "homepage" node and display it on the frontpage.

Now for the node reference fields on these "homepage" nodes, I do not need them to show only the title of the articles, nor do I need for them to show the entire node or even the node teaser (which are the only options currently available for displaying node reference fields). I need for the node reference fields to always display node title, node author(s), node publisher(s), and node post date.

Is there a way to format the content-field.tpl.php so that it digs into the node being referenced and displays the above information?

Thanks so much.

Comments

node_load

DeeZone's picture

Without understanding fully the specs of what you're trying to do I'd consider adding:

1) Create a preprocess_node function in your theme template.php file for the node type you would like to display - "Homepage"
2) Call node_load (http://api.drupal.org/api/drupal/modules--node--node.module/function/nod...) using the node_reference node ID.
3) Collect the field details from the node_load results and send as variables to the node tpl.php file.
4) Format/display the values as you see fit in the tpl file.

Depending on your workflow, I

RobW's picture

Depending on your workflow, I believe you can do something with relationships and views as well. But if you're going for simplicity, I second node_load().

Hmmm, interesting...

DeeZone's picture

Relationships and views, interesting, thanks for pointing that out. Can you give us a summary of your solution using these items so that this thread can be a better reference for future readers.

Like I mentioned in another

RobW's picture

Like I mentioned in another post all of my projects are in either final frontend coding or pre-dev stages, so right now I'm out of the Drupal mindset; forgive me if this is a little vague.

Setting up a views relationship on the node reference field should allow you to use all of the referenced node's fields in the current view. There's a tutorial on creating a basic node reference relationship view here. After you set the relationship up, it's as simple as building a view with the new fields you have available.

This would be a good solution if you were displaying info from many node references on a single page, maybe title and price of 100 items in a table, or doing multiple nested node references (use two relationships instead of node_load, find second reference, node load again) -- I believe that views overhead is much less than doing a full node_load() on many nodes. If you're only doing one, the overhead is probably negligible, and I find a node_load() way simpler, just stick it in a template and go.

Cleaner code

yelvington's picture

I have a bunch of this sort of thing implemented directly in the node-TYPE.tpl.php files; would be much better organized as preprocess functions as DeeZone suggested. Especially when dealing with related nodes containing video emfields. What a hairball they turned out to be.

Clean code is next to...

DeeZone's picture

Indeed, clean code makes life much easier as your site grows and others might be trying to work with you. I'm sure you know it's never to late to pull that nasty PHP logic out of your tpl files in put them into a prepress function. In addition to this "RobW" approach of using Views to craft a solution is potentially much "slicker".

Corresponding Node Reference

visuaLatte's picture

I've used a module called Corresponding Node Reference, which can be set up so that if Node A has a reference to Node B, a "back reference" to Node A will be added in Node B. This means, then, that you can have node references pointing both directions anywhere you need them to do so, which makes it even easier to use Views to list referenced nodes.

This module can be used in conjunction with Views Attach, which essentially adds a field on a node that is a View of nodes that reference it.

In Drupal 7, the References will probably end up being superseded by the Relation module, which allows any entity to have a relationship with any other entity, which should be a lot more flexible than simple node- and user-references.

Possible solution found

akanik's picture

Thanks for all of your comments! I figured out a solution that was a combination of the above suggestions. My problem initially was that I was telling views to filter out only the most recently published homepage node. I did this by sorting it by post date and allowing only one node to be shown. This method, however, effected the node reference fields, allowing only one of those to be shown too! This became an issue when I can to a node reference field with multiple values. I didn't realize that this was what was happening for a while so I thought it was a glitch.

Once I realized what was going on, I allowed unlimited items to show. This fixed the node reference issue, but not the issue of how to have only one homepage node show. To solve this problem I filtered only published homepage nodes, and had only one homepage node at a time be published. I am attempting to accomplish this with the scheduler module. Hopefully the homepage will update daily (or semi-daily) early in the morning, about 6am. So the day before, I will compose the next days homepage node, schedule it to publish at 6am the next day, and then schedule the current homepage node to unpublish at the same time.

I am having some issues with cron so I haven't been able to test this out just yet. If anyone sees any issues with this plan.... feel free to speak up!

Thanks again for all of your input.

allie

Newspapers on Drupal

Group organizers

Group categories

Topics - Newspaper on Drupal

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: