help: node reference / getting context from block

Events happening in the community are now at Drupal community events on www.drupal.org.
brant's picture

Hello. I'm relatively new to Drupal and seeking some advice on working with a node reference field.

I have an Article content type that has a required node reference field, which points back to an Issue. I was happy to discover that using Panels module's Node template and context, I can apply a "template" to all my Issue items that passes the Issue node ID as an argument to a view/block that retrieves a list of the Issue's "child" Articles. Cool stuff.

However, we'd like to have a page that displays data from the current (determined by most recent publication date) Issue and a list of its "child" Articles, and I'm struggling with how to accomplish this (and hoping that I'm overlooking something simple). I have the view/block that can go fetch & display fields from the current Issue sitting on a page, but I'm not seeing a way to get hold of that Issue's ID such that I could pass it as an argument to another block that would fetch/list it's "child" Articles.

Is this possible, or is there another way to go about this? I know I could create a content type that would allow the admin to select the issue to display (and give me the node context I need), but I don't want to create that maintenance point. I'm comfy hacking PHP if need be, but need pointers on where to start.

Thanks in advance for any advice!

Comments

There are two ways to accomplish this....

bluespark's picture

(1) when comfortable with PHP & the Views API, you can do a views_embed_view() inside of a views_pre_render that attaches a "sub-view" to the results inside of the "parent" view. Using this way, you could pass the parent's node id to the child view as an argument, and then get the appropriate results that way... However this is not our favorite option

(2) The best way is to create a view that pulls the "child"/related nodes only, and takes a node ID as an argument... this will filter out the child results based on the argument node id... this also requires using a relationship in views to relate the argument Node id to the node referenced field in the child nodes.
Best way is a combination of a relationship that pulls the referenced field, then the argument of a node id is the node id in that field... so when you are on node/100 (parent node) the view displays all information however you have it formatted in that view based on any child node with node/100 as it's referenced node

Thanks for that. 1 gives me

brant's picture

Thanks for that.

(1) gives me something to explore -- I'm not familiar (yet!) with what I can accomplish using the Views API.

If I understand correctly, (2) is pretty much what I'm doing to render a given Issue page -- I'm handing the ID from the Issue node to a view/block as an argument that I use to look up the child Articles.

The problem I'm having is trying to do something similar outside the context of the Issue node. The idea is when users visit the "Issues" (think publication, not bug reports) section of the site, we present them with the current Issue (cover image, title, etc.) and a list of links to the child Articles. I have a view/block that returns the current issue on the page, but I'm not sure how to grab the node id it returns and subsequently pass it into the block that will fetch the articles.

For what it's worth, I'm

brant's picture

For what it's worth, I'm working with the "Views Attach" module (http://drupal.org/project/views_attach) and am able to achieve what I'm after for the most part. Using it, you can basically add what they call a "Node content" display to your view and "attach" it to a node type (for whatever build modes you choose). Importantly, I'm able to grab the id of the node this display is attached to as an argument. Still working with it, but seems to be a good solution in this case.

Please also see what merlinofchaos suggested when I cross-posed to the Panels group:

http://groups.drupal.org/node/48248

Hi Brant, Did you got a sort

summit's picture

Hi Brant, Did you build a sort of circle on which are the parent node and the child nodes, (so all relevant nodes are shown in a views-block?).
I am in the need of that for a sort of article situation as you described. In my situation a dress-node basiccolor is the parent, and other dress-node colors are referenced to this parent-color. I need a block of all colordresses referenced to the basiscolor, and the basiccolor itself when a childnode is shown.
Can you maybe explain in code, with your view and views attached how you solved it?
Thanks a lot in advance for your reply!
greetings, Martijn

Hi Martijn- It's been a while

brant's picture

Hi Martijn-

It's been a while since I put this together, but I'll try and run down what I did. At the end of the day, I was dealing with Articles that have a node reference to their parent Issue. For the Articles pages, I'm actually using Panels' Node Template (essentially applies a Panels template to all nodes matching a selection rule you define -- in my case, it's applied to all nodes of Article type) that includes a block which features "More from [Issue Name]" and lists the child Articles of the Issue the current Article belongs to.

The actual view/block is set up with a filter for the Article content type, and an argument of "Content: Issue". When I set up the Panels for the node template, I added my Issue field as a content item in the right sidebar I had set up, and, when configuring, chose a custom Field Formatter I put together. The field formatter basically picks up the node id of the Issue (provided by Panels), does a node_load() to load up the Issue (so I can grab useful things like the Title), and then executes my view/block (passing in the Issue's node id as an argument):

$view = views_get_view('child_articles');
$block = $view->execute_display('block', array($nid));

...where $block ends up being populated with the output from the block.

Kind of convoluted, but works for this particular case. I'm hopeful you'll find a more straightforward solution and tell us about it. :)

HTH,

-Brant

Hi Brant, Thanks for your

summit's picture

Hi Brant,

Thanks for your quick reply. I am on a deadline with this site for friday... :(. And a programmer should help me, I paid him through paypal, but he is not responding anymore to my emails....). I think I am scammed!

I am looking to do it sort of the same.
I have a nodetemplate using panels showing my product. (the main "article" is in my case the basic color nodetype).
I have other color nodetype with reference to the basic color nodetype. Nodes of other color nodetype are same nodes, only with different color images!

I think I will try using views to get the whole list of the specific parent node and the references.
Will you be willing to share your view and field formatter? Than I can look if it will fit my needs?

Greetings,
Martijn

formatter code

brant's picture

Here's the mysite_field_formatters.module that sets up the field formatter -- I hope that helps. Best of luck.

<?php

/
* Implementation of hook_field_formatter_info(),.
*/
function mysite_field_formatters_field_formatter_info() {
    'mysitenodereference' => array(
      'label' => t('mysite Node Reference'),
      'field types' => array('nodereference'),
      'multiple values' => CONTENT_HANDLE_MODULE,
    ),
  );
}

/

* Implementation of hook_theme().
*/
function mysite_field_formatters_theme() {
  return array(
    'mysite_field_formatters_formatter_mysitenodereference' => array(
      'arguments' => array('element' => NULL),
    ),
  );
}

function theme_mysite_field_formatters_formatter_mysitenodereference($element) {
  if( !empty($element) ){
    $nid = $element[0]['#item']['nid'];
    $type = $element['#node']->type;
    $rv = '';
    switch( $type ){
      case 'article':
        $node = node_load($nid);
        $title = $node->title;
        $view = views_get_view('child_articles');
        $block = $view->execute_display('block', array($nid));         
        $rv = "<h2 class=\"pane-title\">More From $title</h2>";
        $rv .= $block;
        $rv .= '<span class="more-link">' . l("More...", "node/$nid") . '</span>';       
        break;
      default:
        $title = db_result(db_query("SELECT title FROM {node} WHERE nid=%d", $nid));
        $rv = l($title, 'node/'. $nid);
        break;
    }
    return $rv;
  }
  else {
    return '';
  }
}

Here's what my mysite_field_formatters.info looked like:

name = mysite Field Formatters
description = Custom field formatters
core = 6.x
package = "mysite Custom Modules"
dependencies[] = content

triDUG

Group organizers

Group notifications

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