I want to print an entity referenced fields (node) title in node--customtype.tpl.php.

<?php print render($content['field_my_entity_reference']); ?> prints the field with some CSS class as below:

<div class="field-items">
<div class="field-item even">
<a href="/node/11">Entity Title</a>

What I want to do is to print this field without any pre-defined CSS classes etc..

[field_my_entity_reference] => Array
            [und] => Array
                    [0] => Array
                            [target_id] => 11
                            [entity] => stdClass Object
                                    [vid] => 41
                                    [uid] => 1
                                    [title] => Entity Title
                                    [log] => 
                                    [status] => 1
                                    [comment] => 1
                                    [promote] => 0
                                    [sticky] => 0
                                    [nid] => 11
                                    [type] => project
                                    [language] => es
                                    [created] => 1466009429
                                    [changed] => 1466502663
                                    [tnid] => 0
                                    [translate] => 0
                                    [revision_timestamp] => 1466502663
                                    [revision_uid] => 1
                                    [body] => Array
                                            [und] => Array
                                                    [0] => Array
                                                            [value] => 

I tried <?php print $node->field_my_entity_reference['und'][0]['entity']['title']; ?> but it gives error:

Fatal error: Cannot use object of type stdClass as array in

How can I print the title value of entity referenced field in node.tpl.php?

  • You can actually print the reference entity using print render($content['field_machine_name']);. – Gulok Jun 26 '16 at 9:36
  • My bad..see my answer. – Gulok Jun 26 '16 at 10:26

Alternative using the API is to use field_get_items and field_view_value.

Consider this example:

$field = field_get_items('node', $node, 'field_machine_name');
$title = field_view_value('node', $node, 'field_machine_name', $field[0]);
print render($title);

$title will produce...

    [#markup] => test lang
    [#access] => 1

which is a renderable array.

The advantage of this against print $node->field_my_entity_reference['und'][0]['entity']->title; is the API will take care of the translation.

If you want to still use print $node->field_my_entity_reference['und'][0]['entity']->title I advice to avoid using und instead I will useLANGUAGE_NONE or much better $node->language.

print $node->field_my_entity_reference[$node->language][0]['entity']->title;
|improve this answer|||||
  • 1
    It is the most elegant way. – Jimmy Ko Jul 20 '16 at 23:30

OK, without knowing anything about stdClass Object these kind of questions are normal :)

So, after reading a question about stdClass Object in SO I did what I want as below:

<?php print $node->field_my_entity_reference['und'][0]['entity']->title; ?>

This prints the title as plain text.

Note: Is there another way or better way you're always welcome.

|improve this answer|||||

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.