【问题标题】:Drupal 7, Not sure how to theme my output correctly from query dataDrupal 7,不确定如何从查询数据中正确主题化我的输出
【发布时间】:2011-09-03 13:28:51
【问题描述】:

我一直在使用视图来选择性地返回节点,但现在我想返回我的节点并将分类术语用作组标题。无论如何,我看不到让视图为我执行此操作,然后在一个页面上创建多个视图。

所以我想我会选择一个模块。我已经编写了 SQL 来返回正确的节点,但我不知道如何正确地将它们发送到主题引擎。我想要一些关于如何去做的建议,我的教程书有如下所示的构建列表的示例。

foreach ($result as $row2) {
$items[]  = l($row2->title,'node/'.$row2->nid.'/edit');
}
return array('#markup' => theme('item_list',array('items' => $items)));

现在我想在 Teaser 模式下返回我的节点附加图像文件,以及节点的标题,加上(我不想超越自己)我可能还想要几个附加节点字段附加到标题。应该很容易吧?我根本搞不定。

我已经通过使用我确定是一种看起来有点像这样的非 drupal 方法来解决它(有点),问题是我无法让我的输出与 ColorBox 模块一起使用,所以我'我在想如果我能得到官方的 Teaser 节点数据,它可能会更好地工作,而且我知道我正在以一种 Drupaly 的方式做事会感觉更好:)

foreach ($result as $row2) {
$items .= '<img title="'.$row2->title.' '.$row2->fielddata.'" alt="'.$row2->title.'" src="http://localhost/theme/sites/default/files/styles/thumbnail/public/field/image/'.$row2->filename .'"></a>';
$items .= '</div></div></div></div>';                       
}
return array('#markup' => $items);

非常感谢您花时间帮助我,并在此先感谢您。

【问题讨论】:

标签: drupal drupal-7 drupal-modules drupal-theming


【解决方案1】:

以下代码应该会有所帮助。如果您还没有它,请安装devel module,它为您提供了一个名为dpm() 的奇妙功能,它将将数组/对象的内容打印到消息区域。

// Get some nodes ids
$nids = db_query('SELECT nid FROM {node}')->fetchCol();

// Load up the node objects
$nodes = node_load_multiple($nids);

// This will print the node object out to the messages area so you can inspect it to find the specific fields you're looking for
dpm($nodes); 

// I guess you'll want to do something like this:
$terms = array();

foreach ($nodes as $node) {
  // Load the taxonomy term associated with this node. This will be found in a field as this is how taxonomy terms and nodes are related in D7
  $term = taxonomy_term_load($node->field_vocab_name['und'][0]['tid']);

  // Set up the array
  if (!isset($terms[$term->name])) {
    $terms[$term->name] = array();
  }

  // Create some markup for this node
  $markup = '<h3>' . l($node->title . ' ' . $node->field_other_field['und'][0]['value'], "node/$node->nid") . '</h3>';

  // Add an image
  $image = theme('image', array('path' => $node->field_image['und'][0]['uri'], 'alt' => $node->title));
  $markup.= $image;

  // Add the markup for this node to this taxonomy group's list
  $terms[$term->name][] = $markup;
}

// Make up the final page markup
$output = '';
foreach ($terms as $term_name => $node_list) {
  $output .= '<h2>' . check_plain($term_name) . '</h2>';
  $output .= theme('item_list', array('items' => $node_list));
}

return $output;

希望有帮助

【讨论】:

  • 谢谢,看起来非常有用(而且很复杂)我仍在努力适应 drupal 的工作方式。关于尝试这个的快速问题,图像以完整图像模式出现,我想要缩略图并且我希望它使用 ColorBox 样式。知道如果可能的话怎么办?干杯。
  • 您可以改用theme_image_style 并提供样式名称(例如缩略图),不过我不确定颜色框;我猜想给图像一个特定的 CSS 类可能会做到这一点?
【解决方案2】:

您可以获取视图以按您的分类术语对返回的节点进行分组。假设您使用的是field 视图类型,只需添加分类字段,然后在显示Format:Unformatted list | Settings 的位置单击右侧的设置,然后选择您的分类字段作为分组字段。

注意:如果您不使用field 视图类型,或者如果您不使用unformatted list,那么说明将是上述的变体。

【讨论】:

  • 嗨,感谢您提供有关视图的信息,我已经尝试过了,但有几个问题,1. 税收条款现在出现在每个输出节点上,我可以在 CSS 中使用 Display:none,但我可以使用视图将其删除吗? 2.每个组头都有一个1.前面的吗?不知道它来自哪里? 3. 有没有办法对组进行排序?我的标签是 1,2,3 和出来 1,3,2。干杯。
  • 在分类术语字段上添加排序条件以解决排序问题。在分类术语字段中选择exclude from display 选项。不知道你的组头。
  • 太棒了,谢谢,不知道我怎么错过了展示盒中的排除物,关于那个。这就是现在的所有工作:)
猜你喜欢
  • 2023-03-08
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 2012-01-30
  • 1970-01-01
  • 2020-12-03
  • 2018-02-10
  • 2013-11-01
相关资源
最近更新 更多