I have been working with Drupal for some months now but I have not coded anything in it so I need help.

Situation: On my homepage there are 3 rows that contain blog teasers. On the first row the first video (youtube/vimeo) and the first image of the blog is shown. They are all styled responsively and it works fine. I use a view to do this.

What needs to change: When there is a video in the blog, I want to exclude the image. So Video = No image, No video = Image, No video and No image = just the teaser text (which is always present).

What I have tried: Tried the Views Conditional module. Doesn't work since the replacement variable (example [photo]) does not contain the image itself.

Tried Views If Empty module. The logic works but messes up my CSS in a lot of ways and impossible to make it look like it should.

Solution?: I think that what I need is a PHP part in my View. I have no experience at all with making this. What I have tried is visible below. (messes up the homepage).

if (empty($fields[$row->field_alg_video_1]->last_render)) {
          $this->last_render = $fields[$row->field_alb_fotos]->last_render;
        else {
          $this->last_render = '';

I just threw some stuff together from the net. Can anyone help me out? :)

1 Answer 1


Trust on Views empty field settings :-)

Hide the image and the test field. Configure the video field empty setting telling that should show the image field: use the right token, something like this:


Then configure the image field empty setting with the text field.

So, if there's no vídeo Views will try to show the image field. When rednering the image field Vies will show the image if it's present, or the text field if image is also empty.

  • When I try to do this in stead of an image it shows: [field_alb_fotos]. So just that text. You are talking about rewrite if empty right? Apr 25, 2014 at 10:53
  • Yes, rewrite if empty.
    – sanzante
    Apr 25, 2014 at 11:01
  • It keeps on showing literal text between brackets --> []. Rewriting is just html, I cannot use php there. Apr 25, 2014 at 11:07
  • You don't need PHP here, just the tokens that Views make available. Note about fields order: only fields that are before current field will be available as tokens. Rearrange your fields if you don't have the needed tokens available.
    – sanzante
    Apr 25, 2014 at 11:15
  • Ah, and don't forget to unselect the 'Hide rewriting if empty' checkbox!
    – sanzante
    Apr 25, 2014 at 11:18

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.