3

How can I make one field show up, unless it has no value, in which case another field is shown? I want to do this on the default node view, I already know how to accomplish it in views. I'm assuming it will mean coding something in my theme, but where best to do that is a little confusing to me.

1
  • In a node view? in a view? custom code? Please provide more data to get a good answer. – sanzante Feb 17 '16 at 9:44
3

I would checkout Field formatter conditions where you can set a whole variety of conditions of one field based on if another field is empty or not. You would set these conditions in whatever display mode you are in so in the case of a node, most likely the view mode will be full or default. (e.g. admin/structure/types/manage/[MY_content_type]/display

enter image description here

Alternatively, you could do some custom node template theming and set conditions there such as if one field is not empty do this, else, show the other field.

<?php if (!empty($content['field_foo'])): ?>
  <?php print render($content['field_foo']); ?>
<?php else: ?>
  <?php print render($content['field_bar']); ?>
<?php endif; ?>

This assumes there is a value with field_bar so you may need to add conditions for that field too if it's empty. With per field theming, you may need to not render the general content variable as well, it just depends on the order and where things fall in the template.

2
  • Thanks that module looks promising and the code based approach is what I was looking for, though now that I see it I kick myself for not figuring it out myself. – UltraBob Feb 17 '16 at 23:51
  • Great, glad this helped! – Danny Englander Feb 18 '16 at 3:57

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.