I have a View that's similar to what you see on a lot of blog listings and the like: basically date, title and a display of the node body limited to a certain number of characters with an ellipsis. What I would like to do is add the option to write a tailor written blurb for the view, if the cribbed node body isn't ideal. So, the View would display the node body field iff the content blurb field was empty.

I can't seem to think of useful google search terms for this. I only get results relating to handling empty Views (like, the whole View is empty).

I'm happy to tweak View configuration or write up code in a tpl.php file, whichever makes more sense and is easiest. Thanks in advance!

Comments

just_fixed_it’s picture

A simple way of doing it would be to output both the node.body field and your custom node.blurb field, and then create a custom views----tpl.php file to display the fields (clear the cache!). In the tpl file conditionally print the blurb or the body.

matt v.’s picture

There is now a sandbox module called Views If Empty that provides a way to do what you're looking for.

ashish.verma85’s picture

Please follow the steps
1. Add both fields
2. make the first one invisible (exclude from display)
3. in the second field go to no result behavior tab.
4. write the token of the first field [field_name].
That should be fine.

donavanwilliams’s picture

Just came across this today. Got to give credit where credit's due... brilliant approach on this, Ashish. Thanks.

jpat14’s picture

ashish.verma85: Thank you, this helped tremendously.

efruin’s picture

Thanks Ashish for a brilliant way to accomplish this task. Helped me a lot!

cewernlund’s picture

Still works with Drupal 8 too, thank you for an easy solution!

scoutdickens’s picture

Thank you ashish. Needed to override a field fed from another department and this solution was the quick fix. 

soldrupal’s picture

Thanks, ashish, works for my  D7

auxiliaryjoel’s picture

Hi Ashish,

Is there a way I can do the following in my View:

If Field C is empty, then Hide Field B.

Example: Field A is Start Date. Field B is a dash. Field C is Finish Date.

If Finish Date (C) is empty, then hide Dash (B)?

justinchev’s picture

In this case you should be able to do away with Field B, and then for Field C you would Rewrite Results > Override the output of this field with custom text, and here you would include the dash followed by the Finish date token.

tonytheferg’s picture

What can be done for Drupal 8 when the field you want to rewrite is a form, like an add to cart form? Drupal 8 doesn't allow you to rewrite a form like an add to cart form. I am trying to hide the add to cart form in a view when a node is flagged.