I have two types of node:

  • Building, with a CCK imagefield
  • Apartment, which has a node reference field to a Building node, where one building has many apartments

My goal is to to grab the images from the Building node being referenced. Is there a way to reuse the images uploaded on the Building node and use that on Apartments node?

2 Answers 2


There are several ways to do this.

I'm supposing that you just want to display the images on the node page, not necessarily filling imagefields on the Apartment node (right ?).

The easiest way is to change the way your reference field is rendered. By default, it should render a title. If you go to Content Management > Content Types > Apartment > Display Fields, you can change the display for the node reference field from a "title" to "teaser" or even "full node". That way you can display the images in your apartment node.

Another way, if you really just want the images and not the rest, is to do some coding. For example, in your theme template.php file, you could add the following code (Drupal 6 - because you mention CCK, I guess you're using D6, right ?):

function YOURTHEME_preprocess_node(&$vars) {
  if ($vars['node']->type == 'apartment') {
    $building_nid = $vars['node']->field_YOUR_REF_FIELD_NAME[0]['nid'];

    $vars['images'] = array();

    // If your imagefield can have multiple values OR only one value BUT the same field is present on
    // another content type
    $result = db_query("SELECT f.* FROM {files}
                          LEFT JOIN {content_field_YOUR_REF_FIELD_NAME} cf ON cf.field_YOUR_REF_FIELD_NAME_fid = f.fid
                            LEFT JOIN {node} n ON n.vid = cf.vid
                        WHERE n.nid = %d", $building_nid);

    // If your imagefield can have only ONE value and is ONLY present on this content type
    $result = db_query("SELECT f.* FROM {files}
                          LEFT JOIN {content_type_building} cb ON cb.field_YOUR_REF_FIELD_NAME_fid = f.fid
                            LEFT JOIN {node} n ON n.vid = cb.vid
                        WHERE n.nid = %d", $building_nid);

    while ($image = db_fetch_array($result)) {
      $vars['images'][] = $image;
  • Thank Wouter, Yes I'm still on D6. I would like to show the images on the image upload field of the node forms.
    – r2b2
    Dec 30, 2011 at 8:07
  • The "apartment" node form ? That's tricky. It will require some heavy custom code (I've done something similar before...headaches guaranteed). Do the imagefields need to be populated directly, as soon as the user fills in the node reference field ? In that case, you should add a javascript event listener, and fetch a populated imagefield via Drupal's Ajax/HTML system (AHAH) when a node is referenced. That means that you must also create a small module that will receive the AHAH request, fetch the building image, and return the imagefield widget.
    – wadmiraal
    Dec 30, 2011 at 8:34
  • But is it worth it ? I mean, if you change the image in the building node, it won't update the image in the apartment node (because imagefields are all unique, they don't share images). Or, your custom module will have to take that into account as well. Why not just use the node reference field to get the latest and shiny images from the building node ?
    – wadmiraal
    Dec 30, 2011 at 8:36
  • Actually the node reference field is being prepopulated with the nid in the URL (using the prepopulate module) I was able to load the referenced node with the images but im not quite sure how to show those values in the CCK image fields
    – r2b2
    Dec 30, 2011 at 8:36
  • Ok, so the nid is available in the URL (don't know the prepopulate module). In that case, you could write a small module that implements hook_nodeapi() (the 'prepare' operation). Use the building nid from the URL to fetch the images (just as in my example above) and 'prepopulate' the $node object with the images ($node->field_MY_FIELD = array(...)). This is just a theory, but I think it should work.
    – wadmiraal
    Dec 30, 2011 at 8:47

If you don't want to get your hands dirty in code...

Since you have a node reference from Apartment to Building which Drupal knows about already, another approach is to use Views, Panels and CTools to override the rendering of the Apartment nodes to pull in the related Building node, and thereby allowing you to drop in the image field or a view that contains it.

Also, if you want quick and easy references between Apartments and Buildings to help you when editing nodes or building views, add a Node Reference to the Building content type that points at (many) Apartments, then use the Corresponding Node References to automatically keep these reference fields mirroring one another.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.