I have one field, field_foo, that I want to be rendered one way if field_bar does not have a value. Should field_bar exist, I would like to render the values of both field_foo and field_bar together inside the same markup that all my other fields have.

I could do this in the template file for the content type, but that does not seem like the Drupal way (logic in the tpl file).

I am already using theme_field to adjust the way some of my fields are being rendered, and I have tried to put the logic in there, but I am not sure how to access other fields from within a field-specific theme_field function. Moreover, this does not really strike me as the proper place for this logic either.

What would be the best way of implementing the different rendering of one field based on whether or not another field has content?

  • with views module, nodes must be linked by entity ref. module os so., you can do magic with views conditional module.
    – pinueve
    Jan 31, 2018 at 6:07
  • From my experience on drupal 7 views have huge impact on performance, and I don't see why you can't put your logic inside template file, it's right way in both, drupal 7 tpl.php and drupal 8 twig files. Also for debugging templates you can use devel module to print fields and values.
    – StevanRS
    Jan 31, 2018 at 6:17

2 Answers 2


Some additions to the answer from Taggart Jensen ... I agree with using hook_node_view.

You have a few ways of going about this.

  1. Using the Hook in the template.php file of your custom theme.
  2. Making a small custom module and using the hook in there.

I would recommend the second option so that this is not theme-dependent.

Let's say you want to name your module foo_display.

  1. Create the folder sites/all/modules/foo_display
  2. Create sites/all/modules/foo_display/foo_display.info file (Note: Read here for more details on making an info file.) For the sake of this example, feel free to use this:

name = Foo Display
description = Changes how field_foo and field_bar are displayed based on their values.
core = 7.x

  1. Create sites/all/modules/foo_display/foo_display.module file.
  2. Enable the foo_display module you created.

Inside of the foo_display.module file, we are going to use hook_node_view().

Start with the following code:


foo_display_node_view($node, $view_mode) {
    if ($node->type == 'MACHINE_NAME_OF_CONTENT_TYPE') {

            // Logic for Fields Here

        return $node;

It was right of you to see that putting logic in the theme is not the right place. Doing something like this is a bit more Drupal-y.

I also saw that you were having trouble getting field-specific data.

I recommend using Entity metadata wrappers.

Here is an example of how you could use it in your scenario:


foo_display_node_view($node, $view_mode) {
    if ($node->type == 'MACHINE_NAME_OF_CONTENT_TYPE') {

            // Create Entity Metadata Wrapper for the Node being viewed
            $node_wrapper = entity_metadata_wrapper('node', $node);

            // Get the Value of field_foo
            $field_foo_value = $node_wrapper->field_foo->value();

            // Get the Value of field_bar
            $field_bar_value = $node_wrapper->field_bar->value();

            // Check if field_bar has a value or not
            if ($field_bar_value == NULL) {

                // Render both values together


        return $node;

I hope that this helps to get you started!

  • 1
    Thanks for the really phenomenal step-by-step instructions. You just cleared up another chunk of Drupal for me.
    – kosher
    Feb 1, 2018 at 19:40

look in to https://api.drupal.org/api/drupal/modules%21node%21node.api.php/function/hook_node_view/7.x

In Drupal 7 yes you can do this in the template file for the content type, -> unless you are changing themes lots perhaps the "custom logic" in the theme file is the quickest win.

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