【问题标题】:Theming view for multiple value field多值字段的主题视图
【发布时间】:2011-09-22 16:16:45
【问题描述】:

我知道这可能是一个 n00b 问题,但我到处寻找答案并没有找到任何答案。

我有一个用于“功能”的 CCK 多值字段,其中可以为产品输入随机数量的多个功能。我正在编辑视图,以便可以在产品页面上设置功能输出的样式。

在我看来,现在我可以使用以下命令一次输出整个功能列表:

<?php print $fields['field_features_value']->content ?> 

这将为我列出产品的所有功能。但我想要做的是循环并拉出每个单独的功能并分别对其进行格式化/样式。我该怎么做呢?

【问题讨论】:

  • 好吧,我认为这将是一个常见问题,但我到处搜索,但仍然没有找到如何使用 Views 行主题来做到这一点。

标签: drupal drupal-6 drupal-views cck drupal-theming


【解决方案1】:

我昨天又遇到了这个问题,花了几个小时在谷歌上搜索语法,但无济于事。

我能够让它工作,但我必须承认它不是最好的方法。它重复了 Views 已经为我们完成的一些工作,应该被视为一种蛮力方法。

我的用例涉及对节点中的每个文件字段文件分别进行主题化,每个基于节点的行:

<?php
// $Id: views-view-field.tpl.php,v 1.1 2008/05/16 22:22:32 merlinofchaos Exp $
 /**
  * This template is used to print a single field in a view. It is not
  * actually used in default Views, as this is registered as a theme
  * function which has better performance. For single overrides, the
  * template is perfectly okay.
  *
  * Variables available:
  * - $view: The view object
  * - $field: The field handler object that can process the input
  * - $row: The raw SQL result that can be used
  * - $output: The processed output that will normally be used.
  *
  * When fetching output from the $row, this construct should be used:
  * $data = $row->{$field->field_alias}
  *
  * The above will guarantee that you'll always get the correct data,
  * regardless of any changes in the aliasing that might happen if
  * the view is modified.
  */
?>
<?php

$output = explode('|', $output); // I've rewritten the field output in Views like this: [field_portfolio_image-field_name]|[nid]
$paths = $output[0]; // I've set filefield to show file paths rather than the file
$nid = $output[1]; // The NID is all that's really needed for this approach

$node = node_load($nid);
$slots = $node->field_portfolio_image;

foreach($slots as $prop) {
        print '<a href="'.$prop[filepath].'" title="'.$prop[data][description].'" rel="gallery-'.$nid.'" class="colorbox hidden">'.$prop[data][description].'</a>';
    }

?>

我在这里大量使用了Devel 模块(附上此示例的图片参考),以获得我需要的嵌套值。

我知道有一种更好、更合适的方法来执行此操作,而不是重新加载节点数据,因为视图应该已经可以在页面加载时访问它。

【讨论】:

    【解决方案2】:

    当主题视图过于具体时,我会设置条件、关系、参数和所有视图内容字段除外。我使用的唯一字段是节点 ID。

    然后在做主题时我使用...

    $node = node_load($nid);
    

    ... 获取节点对象。您可以使用 devel 模块自带的 dpm 函数检查节点对象。

    dpm($node);
    

    这种“技术”适用于节点,当您不关心优化或速度时,因为如果您要使用 1000 个节点执行此操作,您应该批量加载节点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多