如何使用div类包装表格视图的某些选定字段


Answers:


11

使用Field覆盖非常简单。

首先,将(author) User: Picture(author) User: Name字段设置为“从显示中排除”。

然后打开Content: Post date字段的设置,然后打开“重写结果”字段集。

选中“重写此字段的输出”。这将显示另一个字段集“替换模式”,该字段集应为您显示当前字段上方所有字段的标记。您需要做的就是<div>在文本框中放置一个包装,然后在div标签之间复制所需的令牌。

在此处输入图片说明


1
还可以使用相同的方法查看此帖子mediacurrent.com/blog/ultimate-markup-control-in-views
Andrew Welch

3

您应该使用hook_views_pre_render(&$ views)或theme_process_views_view_field()

function YOUR_THEME_preprocess_views_view_fields(&$vars) {

  if ($vars['view']->name == 'view_name') {

    foreach( $vars['view']->result as $key => $value) { 
      $add_start_div = '<div class="add_div">';
      $markup_for_field_name_1 = $value->field_name1[0]['rendered']['#markup'] ; 
      $value->field_name1[0]['rendered']['#markup'] = $add_start_div . $markup_for_field_name_1;
      $markup_for_field_name_3 = $value->field_name3[0]['rendered']['#markup'];
      $add_end_div = '</div>';
      $value->field_name3[0]['rendered']['#markup'] = $value->field_name3[0]['rendered']['#markup'] . $add_end_div;
    }
  }
}

缺少几个结束语}}
安德鲁·韦尔奇

2

创建用于行样式输出的模板,例如:views-view-fields--frontpage-promoted-news-story--block.tpl.php。在视图->高级->主题中查找选项。复制并粘贴所需模板的名称,并使用该名称创建一个文件,然后运行“重新扫描”,以便Drupal查找新模板。

然后执行以下操作:

<?php
$field_vals = [];
foreach($fields as $id => $field){
    $field_vals[$id]= $field->content;
}
?>
<!-- layout fields -->
<div class="promoted-news-story">
<!-- image, title, date, body, link -->
    <div class="promoted-news-story__image-wrap">
        <?php print $field_vals['field_standard_header_image'];?>
    </div>
    <div class="promoted-news-story__content-wrap">
        <h2><?php print $field_vals['title'];?></h2>
        <?php print $field_vals['field_news_date'];?>
        <?php print $field_vals['body'];?>
        <?php print $field_vals['view_node'];?>
    </div>
</div>

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.