2

I have two content types, Event and Gallery. Each Event has a node-reference field to a gallery. I am now trying to style the gallery node page from inside the template file. I need to have a way of creating a link which links back to its parent, the Event. How can I find this information?

2

it seems you need to know value of node which is referring the current node. I am assuming machine name for the refernce field you have on event content type as "field_event_gallery"

In your custom module or template.php // function to get backrefernce value.

function mymodulename_get_back_refernce_value($current_nid) {
  $nids = array();
  $results = db_select('field_data_field_event_gallery','n')
            ->fields('n',array('entity_id'))
            ->condition('field_event_gallery_nid',$current_nid)
            ->execute()
            ->fetchAll();
  if(is_array($results) && count($results) > 0) {
    foreach($results as $result) {
      $nids[] = $result->entity_id
    }
  }
  return $nids;
}

Now in your theme template.php use template_preprocess_node(),something like below

function MYTHEME_preprocess_node(&$variables) {
  $node = $variables['node'];
  if ($node->type == 'gallery') {
    //get referring nids
    $referring_nids = mymodulename_get_back_refernce_value($variables['node']->nid);
   if(count($referring_nids) > 0) {
     foreach($referring_nids as $referring_nid) {
       $referring_node = node_load($referring_nid);
       $variables['parent_event_link'][$referring_nid] = l(t('View parent event: @event_title', array('@event_title' => $referring_node->title)), 'node/' . $referring_nid);
   }
  }
}

finally in your template loop through $variables['parent_event_link'] and print it.

My codes are not tested, you have to debug to make them workign for you

  • @user1015214 did you get check this code? – arpitr Oct 11 '13 at 19:07
  • Sorry, I was having computer issues last week. Yes, the code works! Thanks! But you had a typo, it should say $results = db_select... – user1015214 Oct 14 '13 at 13:46
  • oh, also, I needed to change the 'n' in fields('n',array('entity_id')) to 'eg', to match the line above – user1015214 Oct 14 '13 at 21:00
  • updated just an table alias :) – arpitr Oct 14 '13 at 21:08
0

The best way to prepare this for template is through hook_preprocess_node() implementation.

This code should be in your theme's template.php file:

function MYTHEME_preprocess_node(&$variables) {
  $node = $variables['node'];
  if ($node->type == 'gallery') {
    // You can add new attributes to the $variables variable and they will be
    // available automatically in the node template file as direct variables.
    $variables['parent_event_link'] = l(t('View parent event: @event_title', array('@event_title' => $variables['field_gallery_event'][0]['node']->title)), 'node/' . $variables['field_gallery_event'][0]['nid']);
  }
}

And in your node.tpl.php or node--gallery.tpl.php file you can use:

<?php print $parent_event_link; ?>
  • I guess he is asking for backreference. – arpitr Oct 11 '13 at 17:13
  • This wont work, there is no field_gallery_event inside of the gallery node, thats my issue. – user1015214 Oct 11 '13 at 18:12
  • Install devel module and check the output of dpm($variables) in hook_preprocess_node(). Are you trying to do this on single node page or somewhere else? – Aram Boyajyan Oct 11 '13 at 18:24

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.