开发者

How to conditionally hide a field in a drupal view?

开发者 https://www.devze.com 2023-01-28 19:18 出处:网络
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开发者_JAVA百科, can I do this using func

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开发者_JAVA百科, can I do this using functionality in the built in views functionality?


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.


  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.


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.


You can do this easily by the following steps (I've tried it in drupal 7):

  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 (example: [field_name]).

It should work, I hope it will work with you


My solution for showing field_1 for English version and field_2 for translated version:

With use of panels:

  1. Make two copies of the view by adding another display to the view.

  2. In the original view display you remove or exclude from display field_2, in the second display you remove field_1.

  3. In your panel you add both of your view displays as a panel panes.

  4. For the first view display pane add Visibility rule -> User:language -> English.

  5. For the second view display pane add Visibility rule -> User:language -> Your_second_language.

It looks complicated, but it's really simple for people familiar with panels and views. This solution requires no code changing nor knowledge of theming.


You can kind of massage it into working with contemplate. You could do something like this:

    <?php 
if(empty($node->field_foo[0]['view'])){ ?>
<?php print $node->field_bar[0]['view'] ?>
<?php } ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号