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.


1 Answer 1


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' => t('The other entity'),
    'description' => t('Renders the related entity'),
    'weight' => 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(&$f, &$fs) {
  $field_name = 'the_other_entity';
  $f[$field_name] = array(
    '#markup' => $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->field_the_other_node_er->value();
$nid = $other_node->nid; 
// or simply: 
$nid = $this_node->field_the_other_node_er->nid->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

  • Thanks for sharing. I thought it was simply a few lines of code that would go into the DS code field though. I'll take a look at your first suggestion of implementing Panels and see how that goes. I'll surely come back to this topic because it's a lot of information for me right now.
    – BassPlaya
    Oct 5, 2014 at 5:28
  • Oh! I think i missed it... it is a few line of codes which goes into ds! Use the last snippet and print the markup in ds code; i haven't worked with ds custom field but it should work.
    – hkoosha
    Oct 5, 2014 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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