Prepopulate CCK ImageField with existing images 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?
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(&amp;$vars) {
  if ($vars['node']-&gt;type == 'apartment') {
    $building_nid = $vars['node']-&gt;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;
    }
  }
}