How do I code a function for sending mail containing a rendered entity that is referenced from a field?

I have content type A that contains static information, and content type B that users create and reference to the corresponding content type A node instead of writing everything manually. In a custom module I have some code for sending mail notifications containing info in a content type B node, but now the client wants some info from the referenced content type A node.

The line of code that I have a problem is this:

$avtal_id = $node->field_avtalsnummer['und'][0]['value'];

field_avtalsnummer is the reference field in content type B that points to a node of type A and $avtal_id is used in the mail (plain-text). All other fields in the custom module works fine, but for some reason that I don't fully understand I can't get this to work. If I change value to target_id i get the node ID but I can't get the value. Title does not work either. Do I need some code to load the entity first? The optimal solution for me would be to get the info from the referenced field and to display a custom view mode so that I can customise the mail notification accordingly to my clients demands, but I can't even get the value of of the referenced field to work so I'm running out of ideas. Help is very appreciated!

1 Answer 1


You need to use target_id to get the node id, then load the node and get the fields values you want.

$avtal_id = $node->field_avtalsnummer[LANGUAGE_NONE][0]['target_id'];
$avtal_node = node_load($avtal_id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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