【发布时间】:2011-06-15 08:40:06
【问题描述】:
我正在使用 Drupal 7.2 构建一个站点,并使用了几个有用的模块(视图、显示套件等 20 多个)。
我在内容类型(文章)中添加了一个图像字段,并将“字段设置”->“值的数量”设置为 5,这意味着用户可以为该字段上传 5 个图像。
在“完整内容”视图模式下,我想显示所有图像,但如何在“预告片”模式下仅显示一张图像?有没有模块可以做到这一点?
【问题讨论】:
我正在使用 Drupal 7.2 构建一个站点,并使用了几个有用的模块(视图、显示套件等 20 多个)。
我在内容类型(文章)中添加了一个图像字段,并将“字段设置”->“值的数量”设置为 5,这意味着用户可以为该字段上传 5 个图像。
在“完整内容”视图模式下,我想显示所有图像,但如何在“预告片”模式下仅显示一张图像?有没有模块可以做到这一点?
【问题讨论】:
我解决了这个问题,为此字段类型添加了一个新模板,例如 field--field_image.tpl.php 在我的例子中使用以下代码:
// Reduce image array to single image in teaser view mode
if ($element['#view_mode'] == 'teaser') {
$items = array(reset($items));
}
print render($items);
希望这会有所帮助。
编辑:这是(可能)正确的方法:
function MYTHEME_process_field(&$vars) {
$element = $vars['element'];
// Field type image
if ($element['#field_type'] == 'image') {
// Reduce number of images in teaser view mode to single image
if ($element['#view_mode'] == 'teaser') {
$item = reset($vars['items']);
$vars['items'] = array($item);
}
}
}
【讨论】:
它对我不起作用,我不得不稍微调整代码:
function MYTHEME_process_field(&$vars) {
$element = $vars['element'];
// Reduce number of images in teaser view mode to single image
if ($element['#view_mode'] == 'node_teaser' && $element['#field_type'] == 'image') {
$vars['items'] = array($vars['items'][0]);
}
}
【讨论】:
无需更改代码的一个选项是使用模块Field Multiple Limit。使用此模块,您可以选择在允许多个条目的字段的哪个视图中显示多少项目。
然后在该字段的预告视图中,您可以选择要显示或跳过的项目数。
刚刚看到Drupal Stack Exchange已经回答了这个问题
【讨论】:
function MYTHEME_process_field(&$vars) {
$element = $vars['element'];
// Reduce number of images in teaser view mode to single image
if ($element['#view_mode'] == 'teaser' && $element['#field_type'] == 'image') {
$vars['items'] = array($vars['items'][0]);
}
}
我已将 node_teaser 更改为 teaser,它对我有用。 Drupal v 7.19
附言不要忘记刷新缓存。
【讨论】: