What is the php code to display a field of a referenced node in the node form of the node that references the node? So I have a node (content type A) with some text fields and a reference field referencing another node (content type B). 

When a user wants to update one of the text fields an existing node (content type A) , they need to see the text field of the referenced node (content type B). 

Question: what is the php code that I can use to output that textfield from the referenced node (content type B) on the node form of the referencing node (content type A). 

I'd like to add this to a code field in Display Suite and I also like to learn how to retrieve the variables from a loaded node and how to display them.

Cheers!
One way is to use panels, I highly suggest this method. It might be complicated at first but no coding is required.


Enable the built in panels page for the path node/%node/edit.
Add a selection rule: node (from node being edited) is of bunle 'type A'
in context configuration page of enabled panel variant, create a relation using the entity reference field you have other.
in Panels content add each field of node being edited (and submit buttons too!).


or perhaps add the general form instead of each field individually.

again in add content, node section, choose rendered entity.


or create a view with a display of type content pane (ctools views content panes has to be enabled). Add a contextual filter of type nid, choose argument input: nid from context in view's configuration and use it to render the entity in panel. while adding this view to panel, choose the node you added with relationship.



Any way, the original question:
First, implement hook_field_extra_fields. take node module's node_field_extra_field as an example. or follow this question: how should I use hook_field_extra_fields()?

/**
 * Implements hook_field_extra_fields().
 */
function MODULE_field_extra_fields() {
   $etype = 'node';
   $bundle = 'content_type_a';
   $field_name = 'the_other_entity';
   $extra[$etype][$bundle]['form'][$field_name] = array(
     'label' =&gt; t('The other entity'),
    'description' =&gt; t('Renders the related entity'),
    'weight' =&gt; 0,
  );
  return $extra;
}


Next, taking node.module as an example, implement a form alter for your content type:

function MODULE_node_form_content_type_a_form_alter(&amp;$f, &amp;$fs) {
  $field_name = 'the_other_entity';
  $f[$field_name] = array(
    '#markup' =&gt; $the_mighty_markup,
  );
}


Here is another example how to do it: http://johnheaven.eu/en/extending-drupals-user-contact-form it attachs fields to contact form though.

And about the $the_mighty_markup, here you have to generate output of your entity. One suggested way is to create a view, add a contextual filter on nid to it, display the fields you want. Then in your code execute the view to generate the output:

$this_node = entity_metadata_wrapper('node', $this_node);
$other_node = $this_node-&gt;field_the_other_node_er-&gt;value();
$nid = $other_node-&gt;nid; 
// or simply: 
$nid = $this_node-&gt;field_the_other_node_er-&gt;nid-&gt;value();  
$the_mighty_markup = views_embed_view('the_created_view_of_yours', 'master', $nid);


edit:
This is how to output a view: http://codekarate.com/blog/outputting-drupal-7-view-programmatically-contextual-filters-arguments