0

I can do something like this with a text field, because I can compare it to an empty string, but I can't figure out how to do it with an image field.

Visibility rules supports content:exists, but this only applies to the content the rule applies to, not another field.

Is there a way I can display one image field if another one does not exist?

3 Answers 3

1

I would suggest using views to create a block based on the filter criteria where the first image field is empty.

5
  • Hmmm. maybe that could work. I need to show image field B only if image field A doesn't exist. It sounds like your solution is to use the view for the criteria and always show the view in the Node Template?
    – Don Jewett
    Commented Apr 24, 2014 at 21:12
  • First you need to ensure that Image field B is hidden in the Display Mode, and even if the view block is always shown in the template it will be empty if the criteria is not full filled and hence will not matter.
    – pritywiz
    Commented Apr 24, 2014 at 21:18
  • Still having trouble. How do you set the criteria for a missing image? I have tried image:fid is not empty.
    – Don Jewett
    Commented Apr 24, 2014 at 21:46
  • +1 for suggesting a view. See the solution that eventually worked for me.
    – Don Jewett
    Commented Apr 24, 2014 at 22:03
  • I do not understand what I am missing in your issue, but I tested and it seemed to work.
    – pritywiz
    Commented Apr 24, 2014 at 22:14
0

Use a View with the "Views Is Empty" module

It seems there is no way to filter for an empty image or file. One way to filter for the existence of an image/file in a view is to add a relationship and also require the relationship. This only works if the image/file exists, but there is no converse (a test for it NOT existing).

The "Views Is Empty" module enables a view to display one image/file if it exists and another if it doesn't.

To use this, you:

  1. Add both image/files to the view.
  2. Exclude these from the view
  3. Add the "View Is Empty" field
  4. Select the two image/files in the settings for this new field.

In this solution, the view is added to the Node Template rather than either of the fields.

0

Let's say the two fields are called 'field_use_if_present' and 'field_fallback' and your content type is 'article'. Then you can use this code in custom module named 'yourmodule':

<?php 
/**
 * Implements hook_node_view
 */
function yourmodule_node_view($node) {
  if ($node->type === 'article') {
    if (empty($node->content['field_use_if_present']) {
      $node->content['field_use_if_present'] = $node->content['field_fallback'];
    }
    // Don't use this next line if you're managing 
    // show/hide/display of field_fallback with some other functionality.
    $node->content['field_fallback'] = array();
  }

}

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.