How can I print a field from a referenced entity in my theme? I'm trying to print a field from an entity reference field - but I don't have a clue how to do it! 

A bit of background: I've got two content types - master and variation. Variation includes an entity reference field, pulling in fields from the referenced master. But they share an image field. When the variation's image field is empty, I need the master's to display in its place. On the other hand, if the variation's image-field is in use, the masters needs to be hidden. My first thought was to use a display mode, but as you can see I need a little more flexibility than that.

So the details.

A dpm($node) on a variation node gives me:



... (Object) stdClass

field_master_reference (Array, 1 element)

--und (Array, 1 element)

----0 (Array, 3 elements)

------target_id (String, 2 characters ) 11

------entity (Object) stdClass

--------(list of lots of strings which I'll leave out for concision)

--------field_country (Array, 1 element)

--------field_publisher (Array, 1 element)

--------field_catalog (Array, 1 element)

--------field_main_image (Array, 1 element)

----------und (Array, 1 element)

------------0 (Array, 13 elements)

--------------fid (String, 2 characters ) 10

--------------alt (String, 0 characters )

--------------title (String, 0 characters )

--------------width (String, 3 characters ) 518

--------------height (String, 3 characters ) 360

--------------uid (String, 1 characters ) 1

--------------filename (String, 21 characters ) testimage99_r1_c1.png

--------------uri (String, 38 characters ) public://masters/testimage99_r1_c1.png

--------------filemime (String, 9 characters ) image/png

--------------filesize (String, 4 characters ) 4627

--------------status (String, 1 characters ) 1

--------------timestamp (String, 10 characters ) 1366482521

--------------uuid (String, 36 characters ) 67gf4dbb-0a37-4ee9-893e-6g51e3et33c1

------access (Boolean) TRUE

(miscellaneous other fields which I leave out)



I've bolded the relevant structure. From it, how do I print field_main_image (as a fully rendered image)? Sincere thanks to anyone who can help me with something that has perplexed me since Wednesday.
You could do this using either of these:


hook_node_view() in your own custom module
hook_preprocess_node() in your theme template.php


Here's an example of both of these methods. The idea is the same as what rooby suggested earlier but you need to modify the content of the node, not the fields.

Example 1: hook_node_view(), can be used in a custom module.

    // Implements hook_preprocess_node();
    function mycustommodule_node_view($node, $view_mode, $langcode) {

      $field_name = 'field_main_image';

      // Check that the field exists in our node and that it has no content.
      // Alternatively we could check $node-&gt;type.
      if (
        isset($node-&gt;$field_name) &amp;&amp;
        empty($node-&gt;$field_name)
      ) {

        // Use entity metadata wrapper to gain easy access to referenced entities.
        // This method is more reliable than digging the field array.
        $node_wrapper = entity_metadata_wrapper('node', $node);
        if ($node_wrapper-&gt;field_master_reference-&gt;raw()) {

          // Get the referenced node object.
          $master = $node_wrapper-&gt;field_master_reference-&gt;value();

          // Use field_view_field to get the renderable content of the field.
          // Save this data in $node-&gt;content so that node template can output it. 
          $node-&gt;content[$field_name] = field_view_field('node', $master, $field_name);
        }
      }
    }


Example 2: hook_preprocess_node(), can be used in theme template.php.

    // Implements hook_preprocess_node();
    function mycustomtheme_preprocess_node(&amp;$vars) {

      $node = $vars['node'];
      $field_name = 'field_main_image';
      if (
        isset($node-&gt;$field_name) &amp;&amp;
        empty($node-&gt;$field_name)
      ) {
        $node_wrapper = entity_metadata_wrapper('node', $node);
        if ($node_wrapper-&gt;field_master_reference-&gt;raw()) {
          $project = $node_wrapper-&gt;field_master_reference-&gt;value();
          $vars['content'][$field_name] = field_view_field('node', $project, $field_name, array(
          'type' =&gt; 'image',
          'settings' =&gt; array(
            'image_style' =&gt; 'thumbnail',
            'image_link' =&gt; 'content',
          ),
        ));
        }
      }
    }