如何有条件地隐藏 Drupal 视图中的字段?
我有两个要渲染的字段,但我只想在第一个字段为空时显示第二个字段。如果无法覆盖模板文件中的字段,我可以使用内置视图功能中的功能来执行此操作吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于视图 3 ,drupal 7
添加两个字段以按 field1、field2 顺序查看,并选择 field1 复选框“从显示中排除”。
转到 field2 并配置“显示无结果行为”。添加field1替换模式,例如[field1],并选择“将数字0计为空”,“如果为空则隐藏”和“如果为空则隐藏重写”。
现在这将为这两个字段生成 if else 功能。
For views 3 , drupal 7
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.
PS:引用的选项文本可能不正确(我正在查看翻译的网站),但您明白了。
PS: Quoted option texts may be not the right ones (I'm looking at a translated site), but you get the idea.
我通常会使用 views_customfield 模块来处理这种事情。将第三个字段添加到您的视图中,即 php 自定义字段,并从显示中排除第二个字段。然后,您可以在自定义字段中编写一个非常快速的 PHP 片段,检查第一个字段是否为空,如果为空则打印出第二个字段。
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.
您可以通过以下步骤轻松完成此操作(我已在 drupal 7 中尝试过):
它应该有效,我希望它能与你一起工作
You can do this easily by the following steps (I've tried it in drupal 7):
It should work, I hope it will work with you
我的显示英语版本的 field_1 和翻译版本的 field_2 的解决方案:
使用面板:
通过向视图添加另一个显示器来制作视图的两个副本。
在原始视图显示中,您从显示中删除或排除
field_2
,在第二个显示中,您删除field_1
。在面板中,您将两个视图显示添加为面板窗格。
对于第一个视图显示窗格添加
可见性规则 ->用户:语言->英文
。对于第二个视图显示窗格添加
可见性规则 ->用户:语言-> Your_second_language
。它看起来很复杂,但对于熟悉面板和视图的人来说确实很简单。
该解决方案不需要更改代码,也不需要了解主题。
My solution for showing field_1 for English version and field_2 for translated version:
With use of panels:
Make two copies of the view by adding another display to the view.
In the original view display you remove or exclude from display
field_2
, in the second display you removefield_1
.In your panel you add both of your view displays as a panel panes.
For the first view display pane add
Visibility rule -> User:language -> English
.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: