简体   繁体   English

Drupal视图输出图像字段作为可选链接

[英]Drupal Views Output an image field as an optional link

I have two normal fields in a view, one optional URL field and one image field. 我在视图中有两个普通字段,一个是可选URL字段,另一个是图像字段。 I want that if the optional URL field is empty, the image render as normal, but if the optional URL field contains an URL, then print the mage wrapped in an "A" tag with the optional URL. 我希望如果可选URL字段为空,则图像将正常显示,但是如果可选URL字段包含URL,则打印带有可选URL的包裹在“ A”标记中的图像。

I managed to make the part of the Image wrapped in the A tag, but I dont know how to make that part optional. 我设法将图像的一部分包裹在A标记中,但是我不知道如何使该部分成为可选部分。

Any help regarding this? 有什么帮助吗?

Install this http://drupal.org/project/views_customfield module(only for D6) and you can add a custom php field in views. 安装此http://drupal.org/project/views_customfield模块(仅适用于D6),您可以在视图中添加自定义php字段。

In Fields Section -> Choose Customfield -> and then add Customfield: PHP code 在字段部分->选择Customfield->,然后添加Customfield:PHP代码

Below is the sample code to get values 以下是获取值的示例代码

<?php
$static = $this->view->display_handler->get_handlers('field');
$x=$static['field_image']->last_render;
$y=$static['field_link']->last_render;

if(empty($y))
{
    print $x;
}
else
{
    print $y;
}
?>

first line is like declaration and 2nd & 3rd are to get values from the fields. 第一行就像声明,第二和第三则从字段中获取值。

replace field_image & field_link with your own fields(see Replacement patterns for your fields) 用您自己的字段替换field_image和field_link (请参阅字段的替换模式)

Using this sample code, change the conditions to your requirement. 使用此示例代码,将条件更改为您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM