I've been using drupal for a few months now and think I've got most things under control (theming, content types and basic setup of views). However, I've not really worked on relating nodes.

I've just created two content types:
1) Person. Basically just some textfields. No references here.
2) ContentPage. A normal page but with a node-reference to the Person content type. Here I've godt "unlimited" number of values.

For different reasons I don't want the ContentPage template to render the Person-teasers. What I want is to create a view that provides a block for use on every page.
When viewing a page where Persons are referenced I want the block to automatically render the teasers of the selected persons.

In my mind this should be easy - but I've found out that it is not (for me). I can easily render all Persons (by filtering node.type). But something must be wrong about my way of thinking. It must be something like: All Person-id's matching the current node's 'referenced Persons' array. But for some reason the views interface doesn't make sense in this way to me - I've tried everything it seems...

Comments

WorldFallz’s picture

I've not tried it, but there should be a filter available for your node reference field (it would be in the 'content' category.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

mdez’s picture

Thanks for your quick response. Your suggestion, however, doesn't seem to do the trick.

If I select the reference field in filters all I can do is select an operator (is one of, all of, none of) and the contact persons already in the database. What I need should be something like: 'is one of' the current node's referenced persons. This, I think, is something different from your suggestion - if I understand it correctly?

WorldFallz’s picture

The nodereference / nodereferrer relationship always makes my brain swim, lol. Usually I get what I need by poking around and trying everything I can think of, but then I can never describe to anyone how to recreate what i've done, lol.

Perhaps what your looking for is the http://drupal.org/project/nodereferrer module. Unfortunately its not ready for d6 yet-- but there is an issue in the issues queue with a patch as well as a comment with instructions for how to recreate that functionality directly with views. Might be worth a look.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

mdez’s picture

Yes - I think I've tried everything in my imagination already. I'm afraid I have to do some sort of php-code argument handling which isn't really something I know about. It must be possible without using another module as the information IS somewhere in the database (the "foreign" node knows that it is related to the current node)

Any tips will be appreciated.

WorldFallz’s picture

php argument handling is located in the configuration options of the argument you add. That is, once you add an argument in views2, you'll be presented with a list of options for that argument. Choose 'provide default argument' then 'php code', then you'll be presented with a text box to which you can add your argument handling code.

Also, i found the comment in the nodereferrer issue that describes how to create the functionality with views2: http://drupal.org/node/259837#comment-1162961.

hope that helps some....

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

mdez’s picture

I appreciate your help. I find php argument handling very difficult. The solution reevo describes below gave me the results I was looking for. But thank you for your suggestions.

reevo’s picture

* Create a node view, row style: Fields
* Add Node: nid as your argument - this should be the nid of your ContentPage
* Add your Node reference as a field, Label: none, Format: teaser

I haven't tested this out as a block, but it returned the teasers for a node's referenced nodes when I tested the view.

mdez’s picture

Oh boy - this works! I think I've tried 1000 different things but it never came to my mind to add node-ref as a field. I really appreciate your help reevo! Thanks.

doublejosh’s picture

The solution described above DID "work" for me.

Seems pretty limited though, and I would have thought this was the most basic implementation of relationships.
Wouldn't we want to add a relationship with the node reference and then use that relationship with the node id argument?

Works enough.

WorldFallz’s picture

I'm not sure I understand your question-- node relationships are anything but limited.

glennnz’s picture

reevo

This works great.

My referenced node has an image with it, how can I display the image rather than the node title, body or teaser? If I make the node reference excluded from display, and add in the image field, they don't show.

Thanks

Glenn

Glenn
THECA Group

glennnz’s picture

Bump

Thanks

Glenn

Glenn
THECA Group

reevo’s picture

I can't see a way of doing this without some coding, but then my knowledge of Views is mostly limited to D5 / Views 1, so someone else may be able to suggest something more elegant.

For simplicity, let's say we have 2 content types: Parent & Child. Parent contains the node reference to Child, and Child has a title, body and image, but in this instance we just want to show the image.

My view - let's call it "myview" - is a Node view, Row style: Node, Build mode: Teaser. It lists Parent nodes.

node-parent.tpl.php:

<?php 

if ($teaser):
 
  // we're a teaser. let's see if we're being called from our view (myview):
  if ($view->name == 'myview'):

    // we're in myview, so let's load the referenced node ($child)
    if ($child = node_load($node->field_nr_child[0]['nid'])):
      
      // ... and just print the image
      print $child->field_image[0]['view'];

    endif;

  else:

    // print normal teaser output

  endif;

else:

  // print full node

endif;

?>
bomarmonk’s picture

I have spent far too much time trying to set up a node reference "relationship," but all that I needed to do was add the field? This worked and is much simpler than I would have suspected. Thanks reevo!

dakhota’s picture

i've been looking for it for hours, and i never noticed that option. many thanks

fehin’s picture

subscribing

Sinan Erdem’s picture

Here is my method (I am using Panels to show a node but I think this will also work for normal node pages):

I have two content types, say: Person, ContentPage. I want to show list of referenced Persons on a ContentPage node.

1. Created a View.
2. Added a filter: Content type: Person
3. Added any fields that I wanted to show for Person.
4. Added an argument: Content: Person (which is the name of the CCK reference field of the ContentPage content). Selected Provide Default Argument: Node ID from URL
5. Added a Block display to this view.
6. Adjusted other settings as desired...
7. Saved the view.

Then I added this block to the node pages. In my case I have done this within a panel page content. You can also add the block to any region on the normal blocks page (admin/build/blocks).

Now this block shows the list of referenced "Person"s on each "ContentPage" node.

Hope this helps...

dvasquez’s picture

Thanks, it was that i needed too!

letsbuild’s picture

brilliant! thanks alot for that

_sparrow_’s picture

I needed to make a block that would list only nodes that are referenced from the current one. In my case it was a list of related services at a blog post page.

Here's what I did:
- add relationship > pick Content: node reference > tick "Require relationship"
- add argument > pick Node: Nid, do NOT use a relationship; select "Provide default argument", then "Node ID from URL"
- add field > Node: Title, use a relationship
- add filter > pick Node: Type = {referenced type}, use a relationship

Voila! Check if it works correctly by trying referencing node's ID as argument in the preview box.

Below is my view, exported:

$view = new view;
$view->name = 'services_blog_related';
$view->description = '';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('relationships', array(
  'field_blog_service_ref_nid' => array(
    'label' => 'Related service',
    'required' => 1,
    'delta' => '-1',
    'id' => 'field_blog_service_ref_nid',
    'table' => 'node_data_field_blog_service_ref',
    'field' => 'field_blog_service_ref_nid',
    'override' => array(
      'button' => 'Override',
    ),
    'relationship' => 'none',
  ),
));
$handler->override_option('fields', array(
  'title' => array(
    'label' => '',
    'alter' => array(
      'alter_text' => 0,
      'text' => '',
      'make_link' => 0,
      'path' => '',
      'link_class' => '',
      'alt' => '',
      'prefix' => '',
      'suffix' => '',
      'target' => '',
      'help' => '',
      'trim' => 0,
      'max_length' => '',
      'word_boundary' => 1,
      'ellipsis' => 1,
      'html' => 0,
      'strip_tags' => 0,
    ),
    'empty' => '',
    'hide_empty' => 0,
    'empty_zero' => 0,
    'link_to_node' => 1,
    'exclude' => 0,
    'id' => 'title',
    'table' => 'node',
    'field' => 'title',
    'override' => array(
      'button' => 'Override',
    ),
    'relationship' => 'field_blog_service_ref_nid',
  ),
));
$handler->override_option('arguments', array(
  'nid' => array(
    'default_action' => 'default',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'All',
    'title' => '',
    'breadcrumb' => '',
    'default_argument_type' => 'node',
    'default_argument' => '',
    'validate_type' => 'none',
    'validate_fail' => 'not found',
    'break_phrase' => 0,
    'not' => 0,
    'id' => 'nid',
    'table' => 'node',
    'field' => 'nid',
    'validate_user_argument_type' => 'uid',
    'validate_user_roles' => array(
      '2' => 0,
    ),
    'override' => array(
      'button' => 'Override',
    ),
    'relationship' => 'none',
    'default_options_div_prefix' => '',
    'default_argument_fixed' => '',
    'default_argument_user' => 0,
    'default_argument_php' => '',
    'validate_argument_node_type' => array(
      'benefit' => 0,
      'blog' => 0,
      'faq' => 0,
      'guarantee' => 0,
      'keyword' => 0,
      'page' => 0,
      'picture' => 0,
      'product' => 0,
      'service' => 0,
      'testimonial' => 0,
    ),
    'validate_argument_node_access' => 0,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(
      '2' => 0,
    ),
    'validate_argument_type' => 'tid',
    'validate_argument_transform' => 0,
    'validate_user_restrict_roles' => 0,
    'validate_argument_php' => '',
  ),
));
$handler->override_option('filters', array(
  'status' => array(
    'operator' => '=',
    'value' => '1',
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'status',
    'table' => 'node',
    'field' => 'status',
    'relationship' => 'none',
  ),
  'type' => array(
    'operator' => 'in',
    'value' => array(
      'service' => 'service',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'relationship' => 'field_blog_service_ref_nid',
    'override' => array(
      'button' => 'Override',
    ),
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler->override_option('title', 'Related services');
$handler->override_option('items_per_page', 0);
$handler->override_option('distinct', 1);
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', 'Blog-related services');
$handler->override_option('block_caching', -1);

zeta1600’s picture

worked for me.

cleverhoods’s picture

thank you

kazah’s picture

Thank you very much. Great man, _sparrow_ =)

_sparrow_’s picture

Indeed I'm great.