为Drupal Views列表分组添加唯一ID

时间:2012-03-06 18:30:25

标签: php drupal views grouping

我正在使用Drupal 7中的Views 3输出字段列表并使用分组字段生成单独的列表。我需要每个分组都有一个唯一的ID属性应用于< ul>但他们没有默认。

据我所知,我需要编辑views-view-list.tpl.php模板,但我不知道如何在每次迭代中获得唯一ID。

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

最简单的方法,我可以想到我的头脑......

<?php print $wrapper_prefix; ?>
  <?php if (!empty($title)) : ?>
    <h3><?php print $title; ?></h3>
  <?php endif; ?>
  <ul id="<?php echo uniqid(); ?>">
    <?php foreach ($rows as $id => $row): ?>
      <li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
    <?php endforeach; ?>
  </ul>
<?php print $wrapper_suffix; ?> 

将出现在您的views-view-list.tpl.php文件中。

答案 1 :(得分:0)

供将来参考: 在view-views-list.tpl.php中放置一个div。您可以(ab-)使用$ title生成唯一(但一致)的ID。

这样做:

<?php $id = str_replace('FOR ALL UNWANTED CHARS','',$title); ?>

<div id="<?php print strtolower($id); ?>">

答案 2 :(得分:0)

您可以使用$ view-&gt; dom_id变量。它是该视图实例的唯一ID。

在.tpl.php文件中:

<?php print $view->dom_id; ?>

来自modules \ views \ theme \ theme.inc中的评论:

<?php
    // It is true that the DIV wrapper has classes denoting the name of the view
    // and its display ID, but this is not enough to unequivocally match a view
    // with its HTML, because one view may appear several times on the page. So
    // we set up a hash with the current time, $dom_id, to issue a "unique" identifier for
    // each view. This identifier is written to both Drupal.settings and the DIV
    // wrapper.
?>