0

On my views-view-field template, I would like to print out 'field_images' if 'field_cover_image' is empty.

I tried this solution here but it throws me this error message.

Notice: Undefined variable: node in include() (line 45 of /sites/all/themes/mytheme/templates/views-view-fields--incubator.tpl.php).

EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7539 of /includes/common.inc).

Is there any other ways to check if a field is empty or not?

Thanks!

  • 1
    please paste your code, what code have you tried? – Ahmad Jul 17 '12 at 12:49
  • This is the code that I tried: $field = field_get_items('node', $node, 'field_cover_image'); if($field){ print 'yes there is'; }else{ print 'no there isn't'; } – kyooriouskoala Jul 19 '12 at 7:10
1

You get that error because $node is not one of the variables passed to views-view-fields.tpl.php, which receives:

  • $view: The view in use.
  • $fields: an array of $field objects.
  • $row: The raw result object from the query, with all data it fetched.

Basing on the description, $row could be used to retried the node object. I cannot tell you the content of that variable, but using print_r() from your template file can help to understand its content.

  • Hi, I've tried all three variables ($view, $fields, $row) that you mentioned and there's no error. However, it doesn't seem to be working as one of the entry that has a Cover Image still prints out "no". Here's the code: $field = field_get_items('row', $row, 'field_cover_image'); if($field){ print 'yes'; }else{ print 'no'; } – kyooriouskoala Jul 19 '12 at 7:22
1

Use views preprocess API and check if(isset(field['name'])) and then do things as per your needs. Put this in a module to achieve your needs. This will be a long term solution.

  • I have 2 entries, 1 without cover image and another one with a cover image. So I tried this code below but on the entry without cover image it still prints out "yes". if(isset($fields["field_cover_image"]->content)) { print 'yes'; } else { print 'no'; } PS: I'm no programmer. I would be happy if I could use GUI to set things up to achieve this; which is working for some other content type but not for this particular content type. – kyooriouskoala Jul 19 '12 at 7:13
  • 1
    I am not sure about all the variables in your code. But i would say there will be a way to differentiate the variable with cover and with out cover image. Put print_r in the module to get the list of variables and then use isset condition. I think you are almost half way done, keep trying. – Gladiator Jul 19 '12 at 7:16
  • I think Gladiator is correct. There must be a way to differentiate the things. Try some other fields instead of the one mentioned above in ur coment. – user4081 Jul 19 '12 at 7:18
0

Been awhile but thanks again for sharing your solutions. I ended up using Views if Empty module and that saves me from creating a views .tpl

0

The easiest solution for this is to add both fields but add field_images first and mark is as 'exclude from display'. Then add the field_cover_image and in the 'NO RESULTS BEHAVIOR' section add the token for field_images (can be found in replacement patterns). Done!

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.