phpdrupaldrupal-7drupal-nodes

take the fields or the entire contents of a "node reference"


I created a content type: "battle" in which a field is a "node reference" that refers to an existing content from another content type("facts").

Now, when I go to thematize the node: "node--battaglia.tpl.php", how do I get some of the fields of the node referenced("facts"), or to use all of its contents?

(I use Drupal 7)


Solution

  • Try something similar to:

    $nids = array();
    foreach($content['YOUR_FIELD_NAME']['#items'] as $key => $val)
    {
        $nids[] = $val['target_id'];
    }
    // the referenced nodes ids are now stored inside "$nids" array.
    // You can do whatever you need to do from there.
    // below I try to load the node object of each nid.
    $nodes = node_load_multiple($nids);
    

    Hope this helps... Muhammad.