0

I have an entityreference field. I want to change the 'Format' under 'Manage Display' from 'Rendered Entity' to 'Label' based on custom logic. Basically, what I'm doing is, if there isn't an image in a node (field_image), it will load a default image from the taxonomy term. If there is an image in the node, it will just display the label. Sorry if this has been answered, but I can't find anything on it. I've tried hook_entity_view_alter, but I don't think that's the right one.

  • Have you tried using the node_view_alter, which allows you to modify the node just before final output. More info is here: api.drupal.org/api/drupal/modules!node!node.api.php/function/… – longboardnode Jul 9 '13 at 8:56
  • I did try node_view_alter, however it still doesn't work. Maybe I'm using it wrong. I'm changing $build['field_entityref']['#formatter'] from 'entityreference_entity_view' to 'entityreference_label' but when I dpm($content) in the node.tpl, it still has the rendered entity under the field_entityref instead of the label by itself. I ended up just keeping it at 'Rendered Entity'. If I need the Term name, I just grab it from $content['field_entityref'][$id]['taxonomy_term'][$term_id]['#term']->name – Brett Porter Jul 9 '13 at 13:07
0

You can do that printing directly the entityreference field in the node.tpl.php. First you need to check if the image field exists and then display the entity reference, so you can do something like this:

if(!$content['field_image']){
   //Load default image
}else{
   print 'Your custom label.';
}

You have to remember to set the format to hidden in the display section.

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.