Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conditionally hide a field in a drupal view?

I have two fields that I want to render but I only want the second one to display when the first one is empty. Short of overriding the field in a template file, can I do this using functionality in the built in views functionality?

like image 822
Allain Lalonde Avatar asked Dec 05 '10 18:12

Allain Lalonde


People also ask

How do I hide a field in Drupal?

This project is not covered by Drupal's security advisory policy. Hide Content Field modules provides configuration or option to hide field from form display, field exist on a form in a hidden mode. To configure to "Hide Content Field", enable this module by drush en hide_content_field or by GUI in CMS first.

How do I hide a field in Drupal 8?

There are two steps to make a field hidden in drupal 8 entity forms. You can use https://www.drupal.org/project/field_hidden and by enabling this module, Select the 'Hidden field' widget for a field in the entity type's 'Manage form display' dialogue. Save this answer. Show activity on this post.

What are conditional fields?

Conditional fields allow you to manage sets of dependencies between fields. Such fields are available for editing and display only if the right condition is met. To create conditional fields, you must first create custom fields and then leverage such fields in forms.


3 Answers

For views 3 , drupal 7

  • Desired output - Show field1 if field2 is empty.

Add both fields to view in field1, field2 order and select field1 checkbox "exclude from display".

Go to field2 and configure "Show No results behavior". Add field1 replacement pattern e.g [field1] and select "Count the number 0 as empty" , "Hide if empty" and "Hide rewriting if empty".

Now this will produce if else functionality for these two fields.

like image 63
atyagi Avatar answered Sep 19 '22 19:09

atyagi


  1. Add the field that you want to show. Select "exclude from display".
  2. Add the field that you want to control if you show the previous one. Select "rewrite output". Use the token replacement for showing the previous field. Mark "Hide if empty"
  3. There is no three.

PS: Quoted option texts may be not the right ones (I'm looking at a translated site), but you get the idea.

like image 35
penyaskito Avatar answered Sep 20 '22 19:09

penyaskito


I would normally use the views_customfield module for this kind of thing. Add a third field to your view that is a php customfield, and exclude your second field from display. You can then write a really quick PHP snippet in the customfield that checks to see if the first field is empty and prints out the second field if it is.

like image 41
Finbarr Avatar answered Sep 16 '22 19:09

Finbarr