【问题标题】:Drupal Views: Handle title field links differently depending on content type?Drupal Views:根据内容类型以不同方式处理标题字段链接?
【发布时间】:2012-07-12 20:01:40
【问题描述】:

我有一个从 3 种不同内容类型中提取标题的视图。其中一种内容类型的标题应该链接到外部网站,另外两种类型的标题链接到 Drupal 站点内的节点。有没有办法可以设置标题字段以根据标题来自的内容类型以不同方式处理链接?

感谢下面的弗拉德回答!! :)

这是我们在views-view-fields--news--block.tpl.php 模板中使用的工作代码..

<?php if ($fields['type']->content == 'Event'): ?>
  <a href="<?php print $fields['path']->content; ?>"><?php print $fields['title']->content; ?></a>
<?php endif; ?>

<?php if ($fields['type']->content == 'PATF News'): ?>
  <a href="<?php print $fields['path']->content; ?>"><?php print $fields['title']->content; ?></a>
<?php endif; ?>

<?php if ($fields['type']->content == 'News Link'): ?>
//This link goes to _blank
 <a href="<?php print $fields['field_link']->content; ?>" target="_blank"><?php print $fields['title']->content; ?></a>
<?php endif; ?>

【问题讨论】:

  • 您使用哪个版本的 Drupal?

标签: drupal drupal-7 views hyperlink drupal-views


【解决方案1】:

Drupal 6

  1. 在您的视图设置中,将Node: Type 添加到Fields
  2. Basic settings 组中单击Theme: Information 并单击Row style output
  3. Row style output 中的所有内容复制到主题文件夹中的主题文件(应命名为views-view-fields--viewsname.tpl.phpviews-view-fields--viewsname--viewsnamw.tpl.php)。
  4. 修改输出,您应该检查内容类型并进行不同的输出。

Drupal 7

你可以在Advanced 组中找到Theme: Information 并且你必须在Fields 组中添加Content: Type,这非常相似。

在您的 views-view-fields--xxx--xxx.tpl.php 文件中写下类似的内容:

if ($fields['type']->content == 'Page') {
  // print title linking to node
  print $fields['title']->content;
}
if ($fields['type']->content == 'News') {
  // print title linking to other website
  print 'http://example.com/'. $fields['title']->content;
}

改进的代码

$link = $fields['path']->content;
$title = $fields['title']->content;
$options = array();

if ($fields['type']->content == 'News Link') {
  $link = $fields['field_link']->content;
  $options['attributes']['target'] = '_blank';
}

print l($title, $link, $options);

【讨论】:

  • 如何检查内容类型?如果它有任何帮助,我的观点是一个障碍。
  • 试一试您的建议。似乎 if 语句有问题。可能是因为我们按内容类型过滤。内容类型不是字段。
  • 我写过你必须将它添加到你的视图中。提供更多关于您的视图设置的详细信息,我会给出更详细的答案。
  • 我已将视图的导出添加到我的帖子中。感谢您的帮助。
  • 您必须在您的Fields 组中添加Content: Type。之后,$fields['type']-&gt;content 将在您的 .tpl.php 文件中可用。
【解决方案2】:

我之前已通过以下步骤完成此操作:

  1. 包括内容标题内容链接和您的外部 链接
  2. 从视图中隐藏内容标题和内容链接。
  3. 内容链接的重写结果应设置为令牌 内容标题(都仍然隐藏)。
  4. 无结果行为为您的外部链接字段应设置为 内容链接的令牌。

这会在外部链接存在时显示它,并且在不存在时将回退到链接到原始内容的标题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 2011-08-14
    • 2017-10-11
    • 2012-12-23
    • 2021-11-15
    • 1970-01-01
    相关资源
    最近更新 更多